public void start() throws JBNodeException {
if(host == null || port == null || "".equals(host.trim()) || port == 0) {
throw new JBNodeException("Both host and port must be specified.");
}
ServerBootstrap bootstrap = new ServerBootstrap(
new NioServerSocketChannelFactory(
Executors.newCachedThreadPool(),
Executors.newCachedThreadPool(), 10));
ChannelPipelineFactory channelPipelineFactory = new ChannelPipelineFactory() {
public ChannelPipeline getPipeline() throws Exception {
return Channels.pipeline(
new RequestDecoder(),
new ServerHandler() {
@Override
protected Object getService(String serviceName) {
return serviceConfiguration.getService(serviceName);
}
@Override
protected Method getServiceMethod(String serviceName, String methodName) {
return serviceConfiguration.getServiceMethod(serviceName, methodName);
}
@Override
protected byte[] executeMethod(String serviceName, String methodName, Object service,
Method serviceMethod, byte[] data) throws Exception {
Object result = serviceMethod.invoke(service, data);
if (result != null) {
return (byte[]) result;
} else {
return new byte[]{};
}
}
@Override
protected boolean isDebugMode() {
return isDebugModeEnabled();
}
},
new ResponseEncoder()
);
}
};
bootstrap.setPipelineFactory(channelPipelineFactory);
bootstrap.setOption("tcpNoDelay", isTcpNoDelay());
bootstrap.setOption("keepAlive", isKeepAlive());
try {
logger.log(Level.INFO, "Binding to [" + getHost() + ":" + getPort() + "]...");
Channel channel = bootstrap.bind(new InetSocketAddress(InetAddress.getByName(host), port));
logger.log(Level.INFO, "Listening on [" + getHost() + ":" + getPort() + "].");
channelGroup.add(channel);
} catch (UnknownHostException e) {
throw new JBNodeException("Unknown host '" + host + "'", e);