List of usage examples for io.netty.channel ChannelFuture channel
Channel channel();
From source file:com.athena.meerkat.agent.netty.MeerkatClientListener.java
License:Apache License
public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.sync();/* w ww .jav a 2 s.c om*/ } else { // ? ? 5 ?? . final EventLoop loop = future.channel().eventLoop(); loop.schedule(new Runnable() { public void run() { LOGGER.debug("Attempt to reconnect within 5 seconds."); client.createBootstrap(new Bootstrap(), loop, host); } }, 5L, TimeUnit.SECONDS); } }
From source file:com.athena.peacock.agent.netty.PeacockClientListener.java
License:Apache License
@Override public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { future.sync();/*from w w w . j a va2s . c o m*/ } else { // ? ? 5 ?? . final EventLoop loop = future.channel().eventLoop(); loop.schedule(new Runnable() { @Override public void run() { logger.debug("Attempt to reconnect within 5 seconds."); client.createBootstrap(new Bootstrap(), loop, host); } }, 5L, TimeUnit.SECONDS); } }
From source file:com.avanza.astrix.netty.server.NettyRemotingServer.java
License:Apache License
public void start() { bossGroup = new NioEventLoopGroup(1); workerGroup = new NioEventLoopGroup(); ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_REUSEADDR, false).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override//from ww w . j ava 2 s. co m public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new ObjectEncoder(), new ObjectDecoder(ClassResolvers.cacheDisabled(null)), new NettyRemotingServerHandler(serviceActivator)); } }); // Bind and start to accept incoming connections. Binds to all interfaces // TODO: Allow specifying a bind port range. Attempt to bind to each port in range and use first successfully bound port ChannelFuture channel = b.bind(port); try { if (channel.await(2, TimeUnit.SECONDS)) { if (channel.isSuccess()) { port = InetSocketAddress.class.cast(channel.channel().localAddress()).getPort(); log.info("NettyRemotingServer started listening on port={}", port); return; } } } catch (InterruptedException e) { } throw new IllegalStateException("Failed to start netty remoting server. Can't bind to port: " + port); }
From source file:com.baidu.jprotobuf.pbrpc.management.HttpServer.java
License:Apache License
public void start(int port) { serverBootstrap = new ServerBootstrap(); serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override/* www . ja v a 2s. co m*/ public void initChannel(SocketChannel ch) throws Exception { // server??httpResponse?HttpResponseEncoder? ch.pipeline().addLast(new HttpResponseEncoder()); // serverhttpRequest?HttpRequestDecoder? ch.pipeline().addLast(new HttpRequestDecoder()); ch.pipeline().addLast(new HttpServerInboundHandler(rpcServer)); } }).option(ChannelOption.SO_BACKLOG, 128).childOption(ChannelOption.SO_KEEPALIVE, true); serverBootstrap.bind(port).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { channel = future.channel(); // TODO notifyStarted(); } else { // TODO notifyFailed(future.cause()); } } }); LOG.log(Level.INFO, "Http starting at port: " + port); }
From source file:com.baidu.jprotobuf.pbrpc.transport.RpcChannelFutureListener.java
License:Apache License
public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { LOG.log(Level.WARNING, "build channel:" + future.channel() + " failed"); conn.setIsConnected(false);//from w w w . jav a2 s. co m return; } RpcClientCallState requestState = null; while (null != (requestState = conn.consumeRequest())) { LOG.log(Level.FINEST, "[correlationId:" + requestState.getDataPackage().getRpcMeta().getCorrelationId() + "] send over from queue"); conn.getFuture().channel().writeAndFlush(requestState.getDataPackage()); } }
From source file:com.baidu.jprotobuf.pbrpc.transport.RpcServer.java
License:Apache License
public void start(InetSocketAddress sa) { LOG.log(Level.INFO, "RPC starting at: " + sa); this.bind(sa).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (future.isSuccess()) { channel = future.channel(); // TODO notifyStarted(); } else { // TODO notifyFailed(future.cause()); }/*from w ww . j av a 2 s . co m*/ } }); this.inetSocketAddress = sa; startTime = System.currentTimeMillis(); // check if need start http server if (rpcServerOptions.getHttpServerPort() > 0) { httpServer = new HttpServer(this); httpServer.start(rpcServerOptions.getHttpServerPort()); } }
From source file:com.baidu.rigel.biplatform.ma.file.client.monitor.FileServerMonitor.java
License:Open Source License
/** * ?//from w w w . j a v a2 s . c o m */ public void start() throws Exception { EventLoopGroup work = new NioEventLoopGroup(); Bootstrap strap = new Bootstrap(); try { strap.group(work).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel chl) throws Exception { // ?? chl.pipeline().addLast(new ObjectDecoder(1024, ClassResolvers.cacheDisabled(FileServerMonitor.class.getClassLoader()))); chl.pipeline().addLast(new ObjectEncoder()); chl.pipeline().addLast(new FileServerMonitor()); } }); ChannelFuture future = strap.connect(new InetSocketAddress(host, port)); future.channel().closeFuture().sync(); } finally { executor.execute(new Runnable() { @Override public void run() { try { TimeUnit.SECONDS.sleep(3); ChannelFuture future = strap.connect("localhost", 9090).sync(); future.channel().closeFuture().sync(); } catch (InterruptedException e) { logger.error(e.getMessage(), e); } } }); } }
From source file:com.baidu.rigel.biplatform.ma.file.client.service.impl.FileServerClient.java
License:Open Source License
/** * ?/*from www . j a v a 2 s . c o m*/ * * @param server * ?? * @param port * ?? * @param request * * @return */ public Response doRequest(String server, int port, final Request request) { EventLoopGroup work = new NioEventLoopGroup(1); String message = null; try { final Response rs = new Response(ResponseStatus.FAIL, "failed", null); ChannelHandlerAdapter requestHandler = new ChannelHandlerAdapter() { /** * {@inheritDoc} */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { logger.info("successfully connect to file server"); ctx.write(request); ctx.flush(); } /** * {@inheritDoc} */ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { logger.info("successfuly recieve message from file server {}", msg); Response tmpRs = (Response) msg; rs.setDatas(tmpRs.getDatas()); rs.setMessage(tmpRs.getMessage()); rs.setStatus(tmpRs.getStatus()); ctx.close(); } /** * {@inheritDoc} */ @Override public void channelReadComplete(ChannelHandlerContext ctx) throws Exception { ctx.flush(); } /** * {@inheritDoc} */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { logger.error(cause.getMessage()); rs.setMessage(cause.getMessage()); rs.setStatus(ResponseStatus.FAIL); ctx.close(); } }; Bootstrap strap = new Bootstrap(); strap.group(work).option(ChannelOption.TCP_NODELAY, true).channel(NioSocketChannel.class) .handler(new ChannelInitializer<NioSocketChannel>() { @Override protected void initChannel(NioSocketChannel chl) throws Exception { // ?? chl.pipeline().addLast(new ObjectDecoder( ClassResolvers.cacheDisabled(requestHandler.getClass().getClassLoader()))); chl.pipeline().addLast(new ObjectEncoder()); chl.pipeline().addLast(requestHandler); } }); long begin = System.currentTimeMillis(); logger.debug("Begin invoke do file operation request ... ..."); ChannelFuture future = strap.connect(server, port); future.channel().closeFuture().sync(); logger.debug( "Success execute request option cost time: " + (System.currentTimeMillis() - begin) + "ms"); return rs; } catch (InterruptedException e) { logger.error(e.getMessage(), e); message = e.getMessage(); } finally { work.shutdownGracefully(); } Response rs = new Response(ResponseStatus.FAIL, message, null); return rs; }
From source file:com.baidu.rigel.biplatform.ma.file.serv.FileServer.java
License:Open Source License
private static void startServer(String location, int port) { fileLocation = new FileLocation(location); service = new LocalFileOperationServiceImpl(fileLocation); EventLoopGroup bossGroup = new NioEventLoopGroup(10); EventLoopGroup workGroup = new NioEventLoopGroup(50); try {/* ww w . j av a2 s . c o m*/ ServerBootstrap strap = new ServerBootstrap(); strap.group(bossGroup, workGroup).channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)) .childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel channel) throws Exception { // ?? channel.pipeline().addLast(new ObjectDecoder(ClassResolvers .weakCachingConcurrentResolver(FileServer.class.getClassLoader()))); channel.pipeline().addLast(new ObjectEncoder()); // channel.pipeline().addLast("HeartBeatHandler", new HeartBeatRespHandler()); channel.pipeline().addLast(new FileServer()); } }); ChannelFuture future = strap.bind(port).sync(); LOGGER.info("start file server successfully"); LOGGER.info("port : " + port); LOGGER.info("location : " + location); future.channel().closeFuture().sync(); } catch (Exception e) { LOGGER.error(e.getMessage(), e); LOGGER.error("can not start file server with [port : {}] and fileLocation {{}}", port, location); printUsage(); System.exit(-1); } finally { bossGroup.shutdownGracefully(); workGroup.shutdownGracefully(); } }
From source file:com.baoxue.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 v a 2 s . c om 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("Start file server at port : " + port); f.channel().closeFuture().sync(); } finally { // ? bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } }