use of org.redkale.util.AnyValue.DefaultAnyValue in project redkale by redkale.
the class Application method runServers.
// private void clearPersistData() {
// File cachedir = new File(home, "cache");
// if (!cachedir.isDirectory()) return;
// File[] lfs = cachedir.listFiles();
// if (lfs != null) {
// for (File file : lfs) {
// if (file.getName().startsWith("persist-")) file.delete();
// }
// }
// }
// private void signalHandle() {
// //http://www.comptechdoc.org/os/linux/programming/linux_pgsignals.html
// String[] sigs = new String[]{"HUP", "TERM", "INT", "QUIT", "KILL", "TSTP", "USR1", "USR2", "STOP"};
// List<sun.misc.Signal> list = new ArrayList<>();
// for (String sig : sigs) {
// try {
// list.add(new sun.misc.Signal(sig));
// } catch (Exception e) {
// }
// }
// sun.misc.SignalHandler handler = new sun.misc.SignalHandler() {
//
// private volatile boolean runed;
//
// @Override
// public void handle(Signal sig) {
// if (runed) return;
// runed = true;
// logger.info(Application.this.getClass().getSimpleName() + " stoped\r\n");
// System.exit(0);
// }
// };
// for (Signal sig : list) {
// try {
// Signal.handle(sig, handler);
// } catch (Exception e) {
// }
// }
// }
@SuppressWarnings("unchecked")
private void runServers(CountDownLatch timecd, final List<AnyValue> serconfs) throws Exception {
this.servicecdl = new CountDownLatch(serconfs.size());
CountDownLatch sercdl = new CountDownLatch(serconfs.size());
final AtomicBoolean inited = new AtomicBoolean(false);
final Map<String, Class<? extends NodeServer>> nodeClasses = new HashMap<>();
for (final AnyValue serconf : serconfs) {
Thread thread = new Thread() {
{
setName("Redkale-" + serconf.getValue("protocol", "Server").toUpperCase().replaceFirst("\\..+", "") + ":" + serconf.getIntValue("port") + "-Thread");
this.setDaemon(true);
}
@Override
public void run() {
try {
// Thread ctd = Thread.currentThread();
// ctd.setContextClassLoader(new URLClassLoader(new URL[0], ctd.getContextClassLoader()));
final String protocol = serconf.getValue("protocol", "").replaceFirst("\\..+", "").toUpperCase();
NodeServer server = null;
if ("SNCP".equals(protocol)) {
server = NodeSncpServer.createNodeServer(Application.this, serconf);
} else if ("WATCH".equalsIgnoreCase(protocol)) {
DefaultAnyValue serconf2 = (DefaultAnyValue) serconf;
DefaultAnyValue rest = (DefaultAnyValue) serconf2.getAnyValue("rest");
if (rest == null) {
rest = new DefaultAnyValue();
serconf2.addValue("rest", rest);
}
rest.setValue("base", WatchServlet.class.getName());
server = new NodeWatchServer(Application.this, serconf);
} else if ("HTTP".equalsIgnoreCase(protocol)) {
server = new NodeHttpServer(Application.this, serconf);
} else {
if (!inited.get()) {
synchronized (nodeClasses) {
if (!inited.getAndSet(true)) {
// 加载自定义的协议,如:SOCKS
ClassFilter profilter = new ClassFilter(classLoader, NodeProtocol.class, NodeServer.class, (Class[]) null);
ClassFilter.Loader.load(home, classLoader, ((excludelibs != null ? (excludelibs + ";") : "") + serconf.getValue("excludelibs", "")).split(";"), profilter);
final Set<FilterEntry<NodeServer>> entrys = profilter.getFilterEntrys();
for (FilterEntry<NodeServer> entry : entrys) {
final Class<? extends NodeServer> type = entry.getType();
NodeProtocol pros = type.getAnnotation(NodeProtocol.class);
String p = pros.value().toUpperCase();
if ("SNCP".equals(p) || "HTTP".equals(p))
continue;
final Class<? extends NodeServer> old = nodeClasses.get(p);
if (old != null && old != type) {
throw new RuntimeException("Protocol(" + p + ") had NodeServer-Class(" + old.getName() + ") but repeat NodeServer-Class(" + type.getName() + ")");
}
nodeClasses.put(p, type);
}
}
}
}
Class<? extends NodeServer> nodeClass = nodeClasses.get(protocol);
if (nodeClass != null)
server = NodeServer.create(nodeClass, Application.this, serconf);
}
if (server == null) {
logger.log(Level.SEVERE, "Not found Server Class for protocol({0})", serconf.getValue("protocol"));
System.exit(0);
}
servers.add(server);
server.init(serconf);
if (!singletonMode && !compileMode) {
server.start();
} else if (compileMode) {
server.getServer().getPrepareServlet().init(server.getServer().getContext(), serconf);
}
timecd.countDown();
sercdl.countDown();
} catch (Exception ex) {
logger.log(Level.WARNING, serconf + " runServers error", ex);
Application.this.serversLatch.countDown();
}
}
};
thread.start();
}
sercdl.await();
}
use of org.redkale.util.AnyValue.DefaultAnyValue in project redkale by redkale.
the class TransportWatchService method addNode.
@RestMapping(name = "addnode", auth = false, comment = "动态增加指定Group的Node节点")
public RetResult addNode(@RestParam(name = "group", comment = "Group节点名") final String group, @RestParam(name = "addr", comment = "节点IP") final String addr, @RestParam(name = "port", comment = "节点端口") final int port) throws IOException {
InetSocketAddress address;
try {
address = new InetSocketAddress(addr, port);
AsynchronousSocketChannel channel = AsynchronousSocketChannel.open();
// 连接超时2秒
channel.connect(address).get(2, TimeUnit.SECONDS);
channel.close();
} catch (Exception e) {
return new RetResult(RET_TRANSPORT_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is illegal or cannot connect");
}
if (transportFactory.findGroupName(address) != null)
return new RetResult(RET_TRANSPORT_ADDR_ILLEGAL, "InetSocketAddress(addr=" + addr + ", port=" + port + ") is exists");
synchronized (this) {
if (transportFactory.findGroupInfo(group) == null) {
return new RetResult(RET_TRANSPORT_GROUP_NOT_EXISTS, "not found group (" + group + ")");
}
transportFactory.addGroupInfo(group, address);
for (Service service : transportFactory.getServices()) {
if (!Sncp.isSncpDyn(service))
continue;
SncpClient client = Sncp.getSncpClient(service);
if (Sncp.isRemote(service)) {
if (client.getRemoteGroups() != null && client.getRemoteGroups().contains(group)) {
client.getRemoteGroupTransport().addRemoteAddresses(address);
}
}
}
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();
}
use of org.redkale.util.AnyValue.DefaultAnyValue in project redkale by redkale.
the class HttpPrepareServlet method init.
@Override
@SuppressWarnings("unchecked")
public void init(HttpContext context, AnyValue config) {
// 必须要执行
super.init(context, config);
this.context = context;
context.lazyHeaders = lazyHeaders;
Collection<HttpServlet> servlets = getServlets();
servlets.forEach(s -> {
s.preInit(application, context, getServletConf(s));
if (application == null || !application.isCompileMode())
s.init(context, getServletConf(s));
});
{
// 设置ResourceServlet
AnyValue resConfig = config.getAnyValue("resource-servlet");
if ((resConfig instanceof DefaultAnyValue) && resConfig.getValue("webroot", "").isEmpty()) {
((DefaultAnyValue) resConfig).addValue("webroot", config.getValue("root"));
}
if (resConfig == null) {
// 主要用于嵌入式的HttpServer初始化
DefaultAnyValue dresConfig = new DefaultAnyValue();
dresConfig.addValue("webroot", config.getValue("root"));
dresConfig.addValue("ranges", config.getValue("ranges"));
dresConfig.addValue("cache", config.getAnyValue("cache"));
AnyValue[] rewrites = config.getAnyValues("rewrite");
if (rewrites != null) {
for (AnyValue rewrite : rewrites) {
dresConfig.addValue("rewrite", rewrite);
}
}
resConfig = dresConfig;
}
String resServlet = resConfig.getValue("servlet", HttpResourceServlet.class.getName());
try {
Class resClazz = Thread.currentThread().getContextClassLoader().loadClass(resServlet);
RedkaleClassLoader.putReflectionDeclaredConstructors(resClazz, resClazz.getName());
this.resourceHttpServlet = (HttpServlet) resClazz.getDeclaredConstructor().newInstance();
} catch (Throwable e) {
this.resourceHttpServlet = new HttpResourceServlet();
logger.log(Level.WARNING, "init HttpResourceSerlvet(" + resServlet + ") error", e);
}
{
// 获取render的suffixs
AnyValue renderConfig = config.getAnyValue("render");
if (renderConfig != null) {
String[] suffixs = renderConfig.getValue("suffixs", ".htel").toLowerCase().split(";");
((HttpResourceServlet) this.resourceHttpServlet).renderSuffixs = suffixs;
}
}
context.getResourceFactory().inject(this.resourceHttpServlet);
if (application == null || !application.isCompileMode())
this.resourceHttpServlet.init(context, resConfig);
}
}
Aggregations