List of usage examples for org.springframework.core.task TaskExecutor TaskExecutor
TaskExecutor
From source file:io.dyn.net.tcp.TcpServer.java
@SuppressWarnings({ "unchecked" }) @Override//from w w w . j ava 2s.c o m public T start() { Tasks.execute(new Runnable() { @Override public void run() { if (!started.get()) { on(Lifecycle.STOP, new CompletionHandler() { @Override protected void complete() { channel.close(); started.set(false); } }); bootstrap.setOption("backlog", backlog); bootstrap.setOption("child.keepAlive", keepAlive); bootstrap.setOption("child.reuseAddress", reuseAddress); bootstrap.setOption("child.receiveBufferSize", Buffer.SMALL_BUFFER_SIZE); bootstrap.setPipelineFactory(new ChannelPipelineFactory() { @Override public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline pipeline = Channels.pipeline(); if (ssl) { SSLEngine engine; try { engine = SSL.sslContext(sslConfig).createSSLEngine(); engine.setUseClientMode(false); pipeline.addLast("ssl", new SslHandler(engine)); } catch (Exception e) { event(Events.classToEventExpression(e.getClass()), e); } } pipeline.addLast("channelHandler", new SimpleChannelUpstreamHandler() { @Override public void channelConnected(final ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { log.debug("channel connected: " + ctx.getChannel()); Tasks.currentExecutor(new TaskExecutor() { @Override public void execute(Runnable task) { ctx.getPipeline().execute(task); } }); Dyn<Channel> dyn = Dyn.wrap(ctx.getChannel()); ctx.getChannel().setAttachment(dyn); event(NioEvents.CONNECTED, dyn); } @Override public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { log.debug("channel disconnected: " + ctx.getChannel()); Tasks.currentExecutor(null); event(NioEvents.DISCONNECTED, ctx.getChannel().getAttachment()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws Exception { event(Events.classToEventExpression(e.getCause().getClass()), e.getCause()); } }); if (null != protocolHandler) { pipeline.addLast("protocol", new SimpleChannelUpstreamHandler() { Protocol protocol = TcpServer.this.protocolHandler().newInstance(); @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (e.getMessage() instanceof ChannelBuffer) { ChannelBuffer cb = (ChannelBuffer) e.getMessage(); int available = cb.readableBytes(); byte[] bs = new byte[available]; cb.readBytes(bs); protocol.decode(Buffer.wrap(bs), (Evented) ctx.getChannel().getAttachment()); } } }); } configurePipeline(pipeline); return pipeline; } }); try { channel = bootstrap.bind(new InetSocketAddress(InetAddress.getByName(host), port)); started.set(true); //LOG.info("Listening on port %s...", port); event(Lifecycle.START); } catch (UnknownHostException e) { event(Events.classToEventExpression(e.getClass()), e); } } } }, executor); return (T) this; }