我正在使用以下内容来启动netty jax rs服务器:
NettyJaxrsServer netty = new NettyJaxrsServer();
netty.setHostname(HOST);
netty.setPort(port);
netty.setDeployment(resteasyDeployment);
// Some optional extra configuration
netty.setKeepAlive(true);
netty.setRootResourcePath("/");
netty.setSecurityDomain(null);
netty.setIoWorkerCount(16);
netty.setExecutorThreadCount(16);
LOGGER.info("Starting REST server on " + System.getenv("HOSTNAME"));
// Start the server
//("Starting REST server on " + System.getenv("HOSTNAME"));
netty.start();
LOGGER.info("Started!");
这工作正常,但无法检查服务器是否实际启动并且可以接受 REST 请求。我一直在对 REST 接口使用尝试/捕获请求,如果出现异常,请重试,并在 catch 块中等待。它可以工作,但有点混乱:
private void bringUpInterface(SlaveRestInterface slaveAgent, Target localTarget) {
LOGGER.info("Bringing up interface on " + localTarget.getHostName());
Response r = null;
long startTime = System.currentTimeMillis();
boolean isReady = false;
long wait = 50;
int count = 0;
try {
while (!isReady) {
try {
r = slaveAgent.ping();
r.close();
isReady = true;
long endTime = System.currentTimeMillis();
SYS_LOGGER.info(" [OK]" + " (" + (endTime - startTime + "ms") + ")");
} catch (ProcessingException ce) {
if(r != null) {
r.close();
}
if (count == DEFAULT_TIMEOUT_SEC) {
throw new TimeoutException("Failed to start REST interface in " + DEFAULT_TIMEOUT_SEC
+ "second(s)");
}
Thread.sleep(wait);
wait *= 2;
count++;
} catch (Exception e) {
isReady = false;
throw new FatalException("Couldn't connect to REST interface on " + localTarget.getHostName());
}
}
} catch (InterruptedException | TimeoutException ie) {
isReady = false;
throw new FatalException("REST interface did not come up on " + localTarget.getHostName());
}
}
我想知道我是否可以覆盖 start() 方法,并添加代码以使用 HttpRequest 对端点进行 ping 操作。似乎没有任何编程方法来查看服务器是否已启动。
如果你能连接到Netty,请拿起你的ServerBootstrap
,执行以下操作:
ChannelFuture bindFuture = serverBootstrap.bind(port);
//Wait for port to be bound
Channel channel = bindFuture.sync().channel();
//Bound here - start your tests
//Wait for closure (optional)
channel.closeFuture().sync();