}
public static void main(String[] args) {
LOG.info("starting echo server");
NioTcpServer server = new NioTcpServer();
MetricRegistry metrics = new MetricRegistry();
JmxReporter reporter = JmxReporter.forRegistry(metrics).build();
reporter.start();
server.getSessionConfig().setIdleTimeInMillis(IdleStatus.READ_IDLE, 60 * 600 * 1000);
server.getSessionConfig().setIdleTimeInMillis(IdleStatus.WRITE_IDLE, 60 * 600 * 1000);
// create the filter chain for this service
server.setFilters(new MonitoringFilter(metrics), new LoggingFilter("LoggingFilter1"),
((NioTcpEchoServerWithMonitoring) server).new TcpEchoFilter());
server.setIoHandler(new AbstractIoHandler() {
@Override
public void sessionOpened(final IoSession session) {
LOG.info("session opened {}", session);
final String welcomeStr = "welcome\n";
final ByteBuffer bf = ByteBuffer.allocate(welcomeStr.length());
bf.put(welcomeStr.getBytes());
bf.flip();
session.write(bf);
}
@Override
public void exceptionCaught(IoSession session, Exception cause) {
cause.printStackTrace();
}
});
try {
final SocketAddress address = new InetSocketAddress(51000);
server.bind(address);
LOG.debug("Running the server for 25 sec");
Thread.sleep(25000 * 1000);
LOG.debug("Unbinding the UDP port");
server.unbind();
} catch (final InterruptedException e) {
LOG.error("Interrupted exception", e);
} finally {
reporter.stop();
}