List of usage examples for io.netty.channel ChannelHandlerContext channel
Channel channel();
From source file:com.dinstone.jrpc.transport.netty4.NettyClientHandler.java
License:Apache License
/** * {@inheritDoc}/*from ww w . j a va 2 s. c o m*/ * * @see io.netty.channel.ChannelInboundHandlerAdapter#channelActive(io.netty.channel.ChannelHandlerContext) */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { SessionUtil.setResultFutureMap(ctx.channel()); }
From source file:com.dinstone.jrpc.transport.netty4.NettyClientHandler.java
License:Apache License
/** * {@inheritDoc}/*from w w w . ja va2 s. c om*/ * * @see io.netty.channel.ChannelInboundHandlerAdapter#channelInactive(io.netty.channel.ChannelHandlerContext) */ @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { Map<Integer, ResultFuture> futureMap = SessionUtil.getResultFutureMap(ctx.channel()); for (ResultFuture future : futureMap.values()) { future.setResult(new Result(400, "connection is closed")); } }
From source file:com.dinstone.jrpc.transport.netty4.NettyClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Map<Integer, ResultFuture> cfMap = SessionUtil.getResultFutureMap(ctx.channel()); Response response = (Response) msg; ResultFuture future = cfMap.remove(response.getMessageId()); if (future != null) { future.setResult(response.getResult()); }/* www .ja v a2 s. c o m*/ }
From source file:com.dinstone.netty.Server.java
License:Apache License
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ctx.channel().close().sync(); }
From source file:com.dinstone.rpc.netty.client.NettyClientHandler.java
License:Apache License
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Map<Integer, CallFuture> cfMap = SessionUtil.getCallFutureMap(ctx.channel()); RpcResponse response = (RpcResponse) msg; CallFuture future = cfMap.remove(response.getId()); if (future != null) { try {/*from www .j av a 2 s .co m*/ Result result = response.getResult(); if (result.getCode() != 200) { future.setException(new RpcException(result.getCode(), result.getMessage())); } else { future.setResult(result.getData()); } } catch (Exception e) { LOG.error("Unhandled Exception", e); future.setException(new RpcException(400, e.getMessage())); } } }
From source file:com.diwayou.hybrid.remoting.netty.NettyDecoder.java
License:Apache License
@Override public Object decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception { ByteBuf frame = null;//from ww w . j a v a 2 s . co m try { frame = (ByteBuf) super.decode(ctx, in); if (null == frame) { return null; } ByteBuffer byteBuffer = frame.nioBuffer(); return RemotingCommand.decode(byteBuffer); } catch (Exception e) { log.error("decode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); // ? pipelineclose??? RemotingUtil.closeChannel(ctx.channel()); } finally { if (null != frame) { frame.release(); } } return null; }
From source file:com.diwayou.hybrid.remoting.netty.NettyEncoder.java
License:Apache License
@Override public void encode(ChannelHandlerContext ctx, RemotingCommand remotingCommand, ByteBuf out) throws Exception { try {/*from w ww . j a v a2 s. co m*/ ByteBuffer header = remotingCommand.encodeHeader(); out.writeBytes(header); byte[] body = remotingCommand.getBody(); if (body != null) { out.writeBytes(body); } } catch (Exception e) { log.error("encode exception, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); if (remotingCommand != null) { log.error(remotingCommand.toString()); } // ? pipelineclose??? RemotingUtil.closeChannel(ctx.channel()); } }
From source file:com.diwayou.hybrid.remoting.netty.NettyRemotingAbstract.java
License:Apache License
public void processRequestCommand(final ChannelHandlerContext ctx, final RemotingCommand cmd) { final Pair<NettyRequestProcessor, ExecutorService> matched = this.processorTable.get(cmd.getCode()); final Pair<NettyRequestProcessor, ExecutorService> pair = null == matched ? this.defaultRequestProcessor : matched;/*from w ww. j a v a2 s.com*/ if (pair != null) { Runnable run = new Runnable() { @Override public void run() { try { RPCHook rpcHook = NettyRemotingAbstract.this.getRPCHook(); if (rpcHook != null) { rpcHook.doBeforeRequest(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd); } final RemotingCommand response = pair.getObject1().processRequest(ctx, cmd); if (rpcHook != null) { rpcHook.doAfterResponse(RemotingHelper.parseChannelRemoteAddr(ctx.channel()), cmd, response); } // Oneway? if (!cmd.isOnewayRPC()) { if (response != null) { response.setOpaque(cmd.getOpaque()); response.markResponseType(); try { ctx.writeAndFlush(response); } catch (Throwable e) { plog.error("process request over, but response failed", e); plog.error(cmd.toString()); plog.error(response.toString()); } } else { // ?processRequest? } } } catch (Throwable e) { plog.error("process request exception", e); plog.error(cmd.toString()); if (!cmd.isOnewayRPC()) { final RemotingCommand response = RemotingCommand.createResponseCommand( RemotingSysResponseCode.SYSTEM_ERROR, // RemotingHelper.exceptionSimpleDesc(e)); response.setOpaque(cmd.getOpaque()); ctx.writeAndFlush(response); } } } }; try { // ?????? pair.getObject2().submit(run); } catch (RejectedExecutionException e) { // ?10s? if ((System.currentTimeMillis() % 10000) == 0) { plog.warn(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) // + ", too many requests and system thread pool busy, RejectedExecutionException " // + pair.getObject2().toString() // + " request code: " + cmd.getCode()); } if (!cmd.isOnewayRPC()) { final RemotingCommand response = RemotingCommand.createResponseCommand( RemotingSysResponseCode.SYSTEM_BUSY, "too many requests and system thread pool busy, please try another server"); response.setOpaque(cmd.getOpaque()); ctx.writeAndFlush(response); } } } else { String error = " request type " + cmd.getCode() + " not supported"; final RemotingCommand response = RemotingCommand .createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error); response.setOpaque(cmd.getOpaque()); ctx.writeAndFlush(response); plog.error(RemotingHelper.parseChannelRemoteAddr(ctx.channel()) + error); } }
From source file:com.diwayou.hybrid.remoting.netty.NettyRemotingAbstract.java
License:Apache License
public void processResponseCommand(ChannelHandlerContext ctx, RemotingCommand cmd) { final ResponseFuture responseFuture = responseTable.get(cmd.getOpaque()); if (responseFuture != null) { responseFuture.setResponseCommand(cmd); responseFuture.release();/*from w ww . j a va 2 s . c o m*/ // if (responseFuture.getInvokeCallback() != null) { boolean runInThisThread = false; ExecutorService executor = this.getCallbackExecutor(); if (executor != null) { try { executor.submit(new Runnable() { @Override public void run() { try { responseFuture.executeInvokeCallback(); } catch (Throwable e) { plog.warn("excute callback in executor exception, and callback throw", e); } } }); } catch (Exception e) { runInThisThread = true; plog.warn("excute callback in executor exception, maybe executor busy", e); } } else { runInThisThread = true; } if (runInThisThread) { try { responseFuture.executeInvokeCallback(); } catch (Throwable e) { plog.warn("executeInvokeCallback Exception", e); } } } // ? else { responseFuture.putResponse(cmd); } } else { plog.warn("receive response, but not matched any request, " + RemotingHelper.parseChannelRemoteAddr(ctx.channel())); plog.warn(cmd.toString()); } responseTable.remove(cmd.getOpaque()); }
From source file:com.dwarf.netty.guide.factorial.FactorialClientHandler.java
License:Apache License
@Override public void messageReceived(ChannelHandlerContext ctx, final BigInteger msg) { receivedMessages++;//from w ww . j a va 2 s . co m if (receivedMessages == FactorialClient.COUNT) { // Offer the answer after closing the connection. ctx.channel().close().addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) { boolean offered = answer.offer(msg); assert offered; } }); } }