List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.ibasco.agql.core.transport.udp.NettyBasicUdpTransport.java
License:Open Source License
@Override public CompletableFuture<Channel> getChannel(M message) { final CompletableFuture<Channel> cf = new CompletableFuture<>(); //lazy initialization if (channel == null || !channel.isOpen()) { bind(0).addListener((ChannelFuture future) -> { if (future.isSuccess()) { channel = (NioDatagramChannel) future.channel(); channel.closeFuture()// w ww. j a v a 2 s . c o m .addListener((ChannelFuture f) -> log.debug( "CHANNEL CLOSED: {}, Is Open: {}, For Address: {}, Cause: {}", f.channel().id(), f.channel().isOpen(), message.recipient(), f.cause())); cf.complete(channel); } else { channel = null; cf.completeExceptionally(future.cause()); } }); } else { cf.complete(channel); } return cf; }
From source file:com.ibasco.agql.protocols.valve.source.query.logger.SourceLogListenService.java
License:Open Source License
/** * Start listening for log messages/*from w ww .j a v a 2 s . com*/ */ public void listen() throws InterruptedException { final ChannelFuture bindFuture = bootstrap.localAddress(listenAddress).bind().await(); bindFuture.addListener((ChannelFuture future) -> { String hostAddress = ((InetSocketAddress) future.channel().localAddress()).getAddress() .getHostAddress(); int port = ((InetSocketAddress) future.channel().localAddress()).getPort(); log.debug("Log Service listening on port {} via address {}", port, hostAddress); future.channel().closeFuture().addListener(future1 -> log.debug("Service Shutting Down")); }); }
From source file:com.ibm.crail.datanode.netty.server.NettyServer.java
License:Apache License
public void run() { /* start the netty server */ EventLoopGroup acceptGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//www . j a v a2s . c o m ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, workerGroup); /* we use sockets */ boot.channel(NioServerSocketChannel.class); /* for new incoming connection */ boot.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { LOG.info("TID: " + Thread.currentThread().getId() + " , a new client connection has arrived from : " + ch.remoteAddress().toString()); /* incoming pipeline */ ch.pipeline().addLast(new RdmaDecoderRx(), /* this makes full RDMA messages */ new IncomingRequestHandler(ch, dataNode)); /* outgoing pipeline */ //ch.pipeline().addLast(new RdmaEncoderTx()); } }); /* general optimization settings */ boot.option(ChannelOption.SO_BACKLOG, 1024); boot.childOption(ChannelOption.SO_KEEPALIVE, true); /* now we bind the server and start */ ChannelFuture f = boot.bind(this.inetSocketAddress.getAddress(), this.inetSocketAddress.getPort()) .sync(); LOG.info("Datanode binded to : " + this.inetSocketAddress); /* at this point we are binded and ready */ f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); acceptGroup.shutdownGracefully(); LOG.info("Datanode at " + this.inetSocketAddress + " is shutdown"); } }
From source file:com.ibm.crail.namenode.rpc.netty.NettyNameNode.java
License:Apache License
public void run(final RpcNameNodeService service) { /* here we run the incoming RPC service */ InetSocketAddress inetSocketAddress = CrailUtils.getNameNodeAddress(); LOG.info("Starting the NettyNamenode service at : " + inetSocketAddress); /* start the netty server */ EventLoopGroup acceptGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {//w ww . ja va2 s. c om ServerBootstrap boot = new ServerBootstrap(); boot.group(acceptGroup, workerGroup); /* we use sockets */ boot.channel(NioServerSocketChannel.class); /* for new incoming connection */ boot.childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { LOG.info("A new connection has arrived from : " + ch.remoteAddress().toString()); /* incoming pipeline */ ch.pipeline().addLast("RequestDecoder", new RequestDecoder()); ch.pipeline().addLast("NNProcessor", new NamenodeProcessor(service)); /* outgoing pipeline */ ch.pipeline().addLast("ResponseEncoder", new ResponseEncoder()); } }); /* general optimization settings */ boot.option(ChannelOption.SO_BACKLOG, 1024); boot.childOption(ChannelOption.SO_KEEPALIVE, true); /* now we bind the server and start */ ChannelFuture f = boot.bind(inetSocketAddress.getAddress(), inetSocketAddress.getPort()).sync(); /* at this point we are binded and ready */ f.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { workerGroup.shutdownGracefully(); acceptGroup.shutdownGracefully(); LOG.info("Netty namenode at " + inetSocketAddress + " is shutdown"); } }
From source file:com.ict.dtube.remoting.netty.NettyRemotingServer.java
License:Apache License
@Override public void start() { this.defaultEventExecutorGroup = new DefaultEventExecutorGroup(// nettyServerConfig.getServerWorkerThreads(), // new ThreadFactory() { private AtomicInteger threadIndex = new AtomicInteger(0); @Override// ww w . ja va2 s .c om public Thread newThread(Runnable r) { return new Thread(r, "NettyServerWorkerThread_" + this.threadIndex.incrementAndGet()); } }); ServerBootstrap childHandler = // this.serverBootstrap.group(this.eventLoopGroupBoss, this.eventLoopGroupWorker) .channel(NioServerSocketChannel.class) // .option(ChannelOption.SO_BACKLOG, 1024) // .option(ChannelOption.SO_REUSEADDR, true) // .childOption(ChannelOption.TCP_NODELAY, true) // .childOption(ChannelOption.SO_SNDBUF, NettySystemConfig.SocketSndbufSize) // .childOption(ChannelOption.SO_RCVBUF, NettySystemConfig.SocketRcvbufSize) .localAddress(new InetSocketAddress(this.nettyServerConfig.getListenPort())) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast( // defaultEventExecutorGroup, // new NettyEncoder(), // new NettyDecoder(), // new IdleStateHandler(0, 0, nettyServerConfig.getServerChannelMaxIdleTimeSeconds()), // new NettyConnetManageHandler(), // new NettyServerHandler()); } }); if (NettySystemConfig.NettyPooledByteBufAllocatorEnable) { // ???? childHandler.childOption(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT)// ; } try { ChannelFuture sync = this.serverBootstrap.bind().sync(); InetSocketAddress addr = (InetSocketAddress) sync.channel().localAddress(); this.port = addr.getPort(); } catch (InterruptedException e1) { throw new RuntimeException("this.serverBootstrap.bind().sync() InterruptedException", e1); } if (this.channelEventListener != null) { this.nettyEventExecuter.start(); } // ?1?? this.timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { NettyRemotingServer.this.scanResponseTable(); } catch (Exception e) { log.error("scanResponseTable exception", e); } } }, 1000 * 3, 1000); }
From source file:com.imaginarycode.minecraft.bungeejson.impl.httpserver.NettyBootstrap.java
License:Open Source License
public void initialize() { group = new NioEventLoopGroup(5, factory); int port = 7432; // CONFIG ServerBootstrap b = new ServerBootstrap(); b.group(group).channel(NioServerSocketChannel.class) .option(ChannelOption.ALLOCATOR, PooledByteBufAllocator.DEFAULT) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/* ww w . j av a 2s . c om*/ public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("messageCodec", new HttpServerCodec()); pipeline.addLast("messageHandler", new HttpServerHandler()); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); channelFuture = b.bind(port).addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture channelFuture) throws Exception { BungeeJSONPlugin.getPlugin().getLogger() .info("BungeeJSON server started on " + channelFuture.channel().localAddress()); } }); }
From source file:com.jansegre.jwar.webapi.ApiServer.java
License:Open Source License
@Override public void start() { // Reference: http://netty.io/wiki/user-guide-for-4.x.html EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {/*from ww w. j ava2s . co m*/ ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // Add the text line codec combination first, pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // the encoder and decoder are static as these are sharable pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // and then business logic. pipeline.addLast("handler", apiSocket); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); // Bind and start to accept incoming connections. ChannelFuture f = b.bind(apiPort).syncUninterruptibly(); log.info("ApiSocket started at port: {}", apiPort); // Also start parent super.start(); // Wait until the server socket is closed. f.channel().closeFuture().syncUninterruptibly(); } finally { workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } }
From source file:com.jason.netty.file.FileServer.java
License:Apache License
public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w . j a va 2s.com ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).childHandler(new ChannelInitializer<SocketChannel>() { /* * (non-Javadoc) * * @see * io.netty.channel.ChannelInitializer#initChannel(io * .netty.channel.Channel) */ public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast(new StringEncoder(CharsetUtil.UTF_8), new LineBasedFrameDecoder(1024), new StringDecoder(CharsetUtil.UTF_8), new FileServerHandler()); } }); ChannelFuture f = b.bind(port).sync(); System.out.println("Server start at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter10.xml.HttpXmlClient.java
License:Apache License
public void connect(int port) throws Exception { // ?NIO?//from w ww . j a v a 2s .c o m EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true) .handler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpResponseDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); // XML?? ch.pipeline().addLast("xml-decoder", new HttpXmlResponseDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpRequestEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlRequestEncoder()); ch.pipeline().addLast("xmlClientHandler", new HttpXmlClientHandle()); } }); // ?? ChannelFuture f = b.connect(new InetSocketAddress(port)).sync(); // ? f.channel().closeFuture().sync(); } finally { // ?NIO? group.shutdownGracefully(); } }
From source file:com.jjzhk.Chapter10.xml.HttpXmlServer.java
License:Apache License
public void run(final int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {// w w w . ja v a 2s .c om ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("xml-decoder", new HttpXmlRequestDecoder(Order.class, true)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("xml-encoder", new HttpXmlResponseEncoder()); ch.pipeline().addLast("xmlServerHandler", new HttpXmlServerHandler()); } }); ChannelFuture future = b.bind(new InetSocketAddress(port)).sync(); System.out.println("HTTP???? : " + "http://localhost:" + port); future.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }