This commit is contained in:
@@ -296,6 +296,10 @@ public final class Application {
|
|||||||
return startTime;
|
return startTime;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AnyValue getAppConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
public void init() throws Exception {
|
public void init() throws Exception {
|
||||||
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "" + Runtime.getRuntime().availableProcessors() * 4);
|
System.setProperty("java.util.concurrent.ForkJoinPool.common.parallelism", "" + Runtime.getRuntime().availableProcessors() * 4);
|
||||||
System.setProperty("convert.bson.tiny", "true");
|
System.setProperty("convert.bson.tiny", "true");
|
||||||
@@ -441,6 +445,16 @@ public final class Application {
|
|||||||
//------------------------------------------------------------------------
|
//------------------------------------------------------------------------
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void restoreConfig() throws IOException {
|
||||||
|
synchronized (this) {
|
||||||
|
File confFile = new File(this.home, "conf/application.xml");
|
||||||
|
confFile.renameTo(new File(this.home, "conf/application_" + String.format("%1$tY%1$tm%1$td%1$tH%1$tM%1$tS", System.currentTimeMillis()) + ".xml"));
|
||||||
|
final PrintStream ps = new PrintStream(new FileOutputStream(confFile));
|
||||||
|
ps.append(config.toXML("application"));
|
||||||
|
ps.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void startSelfServer() throws Exception {
|
private void startSelfServer() throws Exception {
|
||||||
final Application application = this;
|
final Application application = this;
|
||||||
new Thread() {
|
new Thread() {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.redkale.boot.watch;
|
package org.redkale.boot.watch;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.*;
|
import java.net.*;
|
||||||
import java.nio.channels.AsynchronousSocketChannel;
|
import java.nio.channels.AsynchronousSocketChannel;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -15,7 +16,8 @@ import org.redkale.net.*;
|
|||||||
import org.redkale.net.http.*;
|
import org.redkale.net.http.*;
|
||||||
import org.redkale.net.sncp.*;
|
import org.redkale.net.sncp.*;
|
||||||
import org.redkale.service.*;
|
import org.redkale.service.*;
|
||||||
import org.redkale.util.Comment;
|
import org.redkale.util.*;
|
||||||
|
import org.redkale.util.AnyValue.DefaultAnyValue;
|
||||||
import org.redkale.watch.WatchService;
|
import org.redkale.watch.WatchService;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -49,7 +51,7 @@ public class TransportWatchService implements WatchService {
|
|||||||
public RetResult addNode(
|
public RetResult addNode(
|
||||||
@RestParam(name = "group", comment = "Group节点名") final String group,
|
@RestParam(name = "group", comment = "Group节点名") final String group,
|
||||||
@RestParam(name = "addr", comment = "节点IP") final String addr,
|
@RestParam(name = "addr", comment = "节点IP") final String addr,
|
||||||
@RestParam(name = "port", comment = "节点端口") final int port) {
|
@RestParam(name = "port", comment = "节点端口") final int port) throws IOException {
|
||||||
InetSocketAddress address;
|
InetSocketAddress address;
|
||||||
try {
|
try {
|
||||||
address = new InetSocketAddress(addr, port);
|
address = new InetSocketAddress(addr, port);
|
||||||
@@ -57,8 +59,7 @@ public class TransportWatchService implements WatchService {
|
|||||||
channel.connect(address).get(2, TimeUnit.SECONDS); //连接超时2秒
|
channel.connect(address).get(2, TimeUnit.SECONDS); //连接超时2秒
|
||||||
channel.close();
|
channel.close();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is illegal or cannot connect");
|
||||||
return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is illegal");
|
|
||||||
}
|
}
|
||||||
if (transportFactory.findGroupName(address) != null) return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is exists");
|
if (transportFactory.findGroupName(address) != null) return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is exists");
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
@@ -84,6 +85,14 @@ public class TransportWatchService implements WatchService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DefaultAnyValue node = DefaultAnyValue.create("addr", addr).addValue("port", port);
|
||||||
|
for (AnyValue groupconf : application.getAppConfig().getAnyValue("resources").getAnyValues("group")) {
|
||||||
|
if (group.equals(groupconf.getValue("name"))) {
|
||||||
|
((DefaultAnyValue) groupconf).addValue("node", node);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
application.restoreConfig();
|
||||||
}
|
}
|
||||||
return RetResult.success();
|
return RetResult.success();
|
||||||
}
|
}
|
||||||
@@ -92,7 +101,7 @@ public class TransportWatchService implements WatchService {
|
|||||||
public RetResult removeNode(
|
public RetResult removeNode(
|
||||||
@RestParam(name = "group", comment = "Group节点名") final String group,
|
@RestParam(name = "group", comment = "Group节点名") final String group,
|
||||||
@RestParam(name = "addr", comment = "节点IP") final String addr,
|
@RestParam(name = "addr", comment = "节点IP") final String addr,
|
||||||
@RestParam(name = "port", comment = "节点端口") final int port) {
|
@RestParam(name = "port", comment = "节点端口") final int port) throws IOException {
|
||||||
if (group == null) return new RetResult(RET_NO_GROUP, "not found group (" + group + ")");
|
if (group == null) return new RetResult(RET_NO_GROUP, "not found group (" + group + ")");
|
||||||
final InetSocketAddress address = new InetSocketAddress(addr, port);
|
final InetSocketAddress address = new InetSocketAddress(addr, port);
|
||||||
if (!group.equals(transportFactory.findGroupName(address))) return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") not belong to group(" + group + ")");
|
if (!group.equals(transportFactory.findGroupName(address))) return new RetResult(RET_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") not belong to group(" + group + ")");
|
||||||
@@ -119,6 +128,13 @@ public class TransportWatchService implements WatchService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
for (AnyValue groupconf : application.getAppConfig().getAnyValue("resources").getAnyValues("group")) {
|
||||||
|
if (group.equals(groupconf.getValue("name"))) {
|
||||||
|
((DefaultAnyValue) groupconf).removeValue("node", DefaultAnyValue.create("addr", addr).addValue("port", port));
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
application.restoreConfig();
|
||||||
}
|
}
|
||||||
return RetResult.success();
|
return RetResult.success();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -302,6 +302,18 @@ public abstract class AnyValue {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public DefaultAnyValue removeValue(String name, AnyValue value) {
|
||||||
|
if (name == null || value == null || this.anyEntrys == null) return this;
|
||||||
|
this.anyEntrys = Utility.remove(this.anyEntrys, (t) -> name.equals(((Entry) t).name) && ((Entry) t).getValue().equals(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public DefaultAnyValue removeValue(String name, String value) {
|
||||||
|
if (name == null || value == null || this.stringEntrys == null) return this;
|
||||||
|
this.stringEntrys = Utility.remove(this.stringEntrys, (t) -> name.equals(((Entry) t).name) && ((Entry) t).getValue().equals(value));
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AnyValue getAnyValue(String name) {
|
public AnyValue getAnyValue(String name) {
|
||||||
for (Entry<DefaultAnyValue> en : this.anyEntrys) {
|
for (Entry<DefaultAnyValue> en : this.anyEntrys) {
|
||||||
@@ -533,4 +545,51 @@ public abstract class AnyValue {
|
|||||||
return value == null ? defaultValue : value;
|
return value == null ? defaultValue : value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object other) {
|
||||||
|
if (!(other instanceof AnyValue)) return false;
|
||||||
|
AnyValue conf = (AnyValue) other;
|
||||||
|
if (!equals(this.getStringEntrys(), conf.getStringEntrys())) return false;
|
||||||
|
return equals(this.getAnyEntrys(), conf.getAnyEntrys());
|
||||||
|
}
|
||||||
|
|
||||||
|
private static <T> boolean equals(Entry<? extends T>[] entry1, Entry<T>[] entry2) {
|
||||||
|
if ((entry1 == null || entry1.length == 0) || (entry2 == null || entry2.length == 0)) return false;
|
||||||
|
if (entry1.length != entry2.length) return false;
|
||||||
|
for (int i = 0; i < entry1.length; i++) {
|
||||||
|
if (!entry1[i].equals(entry2[i])) return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
int hash = 7;
|
||||||
|
hash = 19 * hash + Arrays.deepHashCode(this.getStringEntrys());
|
||||||
|
hash = 19 * hash + Arrays.deepHashCode(this.getAnyEntrys());
|
||||||
|
return hash;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String toXML(String rootName) {
|
||||||
|
return toXMLString(new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n\r\n"), rootName, this, 0).toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static StringBuilder toXMLString(StringBuilder sb, String nodeName, AnyValue conf, int indent) { //indent: 缩进长度
|
||||||
|
if (indent < 0) indent = 0;
|
||||||
|
char[] chars = new char[indent];
|
||||||
|
Arrays.fill(chars, ' ');
|
||||||
|
final String space = new String(chars);
|
||||||
|
Entry<AnyValue>[] anys = conf.getAnyEntrys();
|
||||||
|
sb.append(space).append('<').append(nodeName);
|
||||||
|
for (Entry<String> en : conf.getStringEntrys()) {
|
||||||
|
sb.append(' ').append(en.name).append("=\"").append(en.value).append("\"");
|
||||||
|
}
|
||||||
|
if (anys == null || anys.length == 0) return sb.append("/>\r\n\r\n");
|
||||||
|
sb.append(">\r\n\r\n");
|
||||||
|
for (Entry<AnyValue> en : conf.getAnyEntrys()) {
|
||||||
|
toXMLString(sb, en.name, en.getValue(), indent + 4);
|
||||||
|
}
|
||||||
|
return sb.append(space).append("</").append(nodeName).append(">\r\n\r\n");
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user