@Override public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { Attribute<Unmarshaller> attr = ctx.channel().attr(UNMARSHALLER); Unmarshaller unmarshaller = attr.get(); if (unmarshaller == null) { unmarshaller = super.getUnmarshaller(ctx); attr.set(unmarshaller); } return unmarshaller; } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { LOG.warn("Exception caught", cause); NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); if (cnxn != null) { if (LOG.isDebugEnabled()) { LOG.debug("Closing {}", cnxn); } cnxn.close(); } }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { // Store a ref to the count of current inflight requests onto this channel. So that // other code can query it using getInflightRequestCountFromChannel(). ctx.channel().attr(ATTR_CURRENT_REQS).set(currentRequests); super.channelActive(ctx); }
@Override public Unmarshaller getUnmarshaller(ChannelHandlerContext ctx) throws Exception { Attribute<Unmarshaller> attr = ctx.channel().attr(UNMARSHALLER); Unmarshaller unmarshaller = attr.get(); if (unmarshaller == null) { unmarshaller = super.getUnmarshaller(ctx); attr.set(unmarshaller); } return unmarshaller; } }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive {}", ctx.channel()); } allChannels.remove(ctx.channel()); NettyServerCnxn cnxn = ctx.channel().attr(CONNECTION_ATTRIBUTE).getAndSet(null); if (cnxn != null) { if (LOG.isTraceEnabled()) { LOG.trace("Channel inactive caused close {}", cnxn); } cnxn.close(); } }
protected static boolean fireCompleteEventIfNotAlready(ChannelHandlerContext ctx, CompleteReason reason) { // Only allow this method to run once per request. Attribute<State> attr = ctx.channel().attr(ATTR_STATE); State state = attr.get(); if (state == null || state != State.STARTED) return false; attr.set(State.COMPLETED); HttpRequest request = ctx.channel().attr(ATTR_HTTP_REQ).get(); HttpResponse response = ctx.channel().attr(ATTR_HTTP_RESP).get(); // Cleanup channel attributes. ctx.channel().attr(ATTR_HTTP_REQ).set(null); ctx.channel().attr(ATTR_HTTP_RESP).set(null); // Fire the event to whole pipeline. ctx.pipeline().fireUserEventTriggered(new CompleteEvent(reason, request, response)); return true; }