List of usage examples for io.netty.channel ChannelFutureListener ChannelFutureListener
ChannelFutureListener
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void forward(final ChannelHandlerContext ctx, ByteBuf byteBuf) { if (inboundChannel.isActive()) { inboundChannel.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); }/*from w w w .j av a 2 s . c o m*/ } }); } else { } }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();/*from w ww .ja v a 2 s.c o m*/ if (!flags.ack()) { //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyBackendHandler.java
License:Apache License
private void handleWindowsUpdateFrame(final ChannelHandlerContext ctx) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();/*from w w w .ja v a2 s .c o m*/ // 00 00 04 08 00 00 00 00 00 00 0f 00 01 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x08); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0f); byteBuf.writeByte(0x00); byteBuf.writeByte(0x01); ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { ctx.channel().read(); } else { future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) { final Channel inboundChannel = ctx.channel(); // ??/*from w w w . ja va 2s . com*/ Bootstrap b = new Bootstrap(); b.group(inboundChannel.eventLoop()).channel(ctx.channel().getClass()) .handler(new GrpcProxyBackendHandler(inboundChannel)).option(ChannelOption.AUTO_READ, false); for (int i = 0; i < remoteHosts.length; i++) { final ChannelFuture f = b.connect(remoteHosts[i], remotePorts[i]); outboundChannels[i] = f.channel(); f.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // connection complete start to read first data inboundChannel.read(); // System.out.println(f.channel().remoteAddress() + ", " + f.channel().localAddress()); } else { // Close the connection if the connection attempt has failed. // System.out.println("channelActive close" + inboundChannel.remoteAddress() + ", " + inboundChannel.localAddress()); inboundChannel.close(); } } }); } }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void handleSettingFrame(final ChannelHandlerContext ctx, Http2Flags flags) { ByteBufAllocator alloc = ctx.alloc(); ByteBuf byteBuf = alloc.buffer();//from ww w.ja va 2 s . c o m if (!flags.ack()) { // System.out.println("********************* setting received ..."); //00 00 0c 04 00 00 00 00 00 00 03 7f ff ff ff 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x0c); byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x03); byteBuf.writeByte(0x7f); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0xff); byteBuf.writeByte(0x00); //04 00 10 00 00 byteBuf.writeByte(0x04); byteBuf.writeByte(0x00); byteBuf.writeByte(0x10); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } else { // System.out.println("********************* setting ack received ..."); //00 00 00 04 01 00 00 00 00 byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x04); byteBuf.writeByte(0x01); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); byteBuf.writeByte(0x00); } ctx.writeAndFlush(byteBuf).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println(" ...operationComplete isSuccess"); ctx.channel().read(); } else { // System.out.println("...operationComplete failure"); future.channel().close(); } } }); }
From source file:com.netty.grpc.proxy.demo.handler.GrpcProxyFrontendHandler.java
License:Apache License
private void forwardThisFrame(final ChannelHandlerContext ctx, final ByteBuf copy, int streamId, final int type) { if (selectedChannel == null) { int select = (counter.getAndIncrement()) % remoteHosts.length; selectedChannel = outboundChannels[select]; }/* w w w.jav a2 s . c o m*/ //int select = 0; // System.out.println("---------------------------------select:" + select + "," + ByteBufUtil.hexDump(copy)); final Channel inboundChannel = ctx.channel(); selectedChannel.writeAndFlush(copy).addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) { if (future.isSuccess()) { // System.out.println("forward success ------------------------------------------type=" + type); inboundChannel.read(); } else { // System.out.println("forward failure------------------------------------------"); inboundChannel.close(); } } }); }
From source file:com.newlandframework.avatarmq.netty.MessageConnectFactory.java
License:Apache License
public void connect() { Preconditions.checkNotNull(messageHandler, "Message's Handler is Null!"); try {//from w w w . ja v a 2 s. c o m init(); ChannelFuture channelFuture = bootstrap.connect(this.remoteAddr).sync(); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { Channel channel = future.channel(); messageChannel = channel; } }); System.out.println("ip address:" + this.remoteAddr.toString()); connected = true; } catch (InterruptedException e) { e.printStackTrace(); } }
From source file:com.newlandframework.avatarmq.netty.MessageProcessor.java
License:Apache License
public void sendAsynMessage(RequestMessage request, final NotifyCallback listener) { Channel channel = factory.getMessageChannel(); if (channel == null) { return;// w ww .j a v a 2 s . c o m } Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); invoker.join(new CallBackListener<Object>() { public void onCallBack(Object t) { ResponseMessage response = (ResponseMessage) t; listener.onEvent((ProducerAckMessage) response.getMsgParams()); } }); ChannelFuture channelFuture = channel.writeAndFlush(request); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); }
From source file:com.newlandframework.avatarmq.netty.MessageProcessor.java
License:Apache License
public Object sendAsynMessage(RequestMessage request) { Channel channel = factory.getMessageChannel(); if (channel == null) { return null; }/*from w w w .j a v a2s . co m*/ Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); ChannelFuture channelFuture = channel.writeAndFlush(request); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); try { Object result = invoker.getMessageResult(factory.getTimeOut(), TimeUnit.MILLISECONDS); callBackMap.remove(request.getMsgId()); return result; } catch (RuntimeException e) { e.printStackTrace(); return null; } }
From source file:com.newlandframework.avatarmq.netty.MessageProcessor.java
License:Apache License
public void sendSyncMessage(RequestMessage request) { Channel channel = factory.getMessageChannel(); if (channel == null) { return;/* w w w. j a v a 2 s . c o m*/ } Map<String, CallBackInvoker<Object>> callBackMap = factory.getCallBackMap(); CallBackInvoker<Object> invoker = new CallBackInvoker<Object>(); callBackMap.put(request.getMsgId(), invoker); invoker.setRequestId(request.getMsgId()); ChannelFuture channelFuture; try { channelFuture = channel.writeAndFlush(request).sync(); channelFuture.addListener(new ChannelFutureListener() { public void operationComplete(ChannelFuture future) throws Exception { if (!future.isSuccess()) { invoker.setReason(future.cause()); } } }); } catch (InterruptedException ex) { Logger.getLogger(MessageProcessor.class.getName()).log(Level.SEVERE, null, ex); } }