private void sendResponse(Object message, ChannelHandlerContext ctx, Exchange exchange) throws Exception { Object body = getResponseBody(exchange); if (body == null) { noReplyLogger.log("No payload to send as reply for exchange: " + exchange); if (consumer.getConfiguration().isDisconnectOnNoReply()) { // must close session if no data to write otherwise client will never receive a response // and wait forever (if not timing out) if (LOG.isTraceEnabled()) { LOG.trace("Closing channel as no payload to send as reply at address: {}", ctx.channel().remoteAddress()); } NettyHelper.close(ctx.channel()); } } else { // if textline enabled then covert to a String which must be used for textline if (consumer.getConfiguration().isTextline()) { body = NettyHelper.getTextlineBody(body, exchange, consumer.getConfiguration().getDelimiter(), consumer.getConfiguration().isAutoAppendDelimiter()); } // we got a body to write ChannelFutureListener listener = createResponseFutureListener(consumer, exchange, ctx.channel().remoteAddress()); if (consumer.getConfiguration().isTcp()) { NettyHelper.writeBodyAsync(LOG, ctx.channel(), null, body, exchange, listener); } else { NettyHelper.writeBodyAsync(LOG, ctx.channel(), exchange.getProperty(NettyConstants.NETTY_REMOTE_ADDRESS, SocketAddress.class), body, exchange, listener); } } }
@Override public void operationComplete(ChannelFuture future) throws Exception { Channel channel = future.channel(); if (channel.isOpen()) { NettyHelper.close(channel); } allChannels.remove(channel); } });
NettyHelper.writeBodyAsync(LOG, channel, remoteAddress, body, exchange, new ChannelFutureListener() { public void operationComplete(ChannelFuture channelFuture) throws Exception { LOG.trace("Operation complete {}", channelFuture);
/** * Gets the object we want to use as the request object for sending to netty. * * @param exchange the exchange * @return the object to use as request * @throws Exception is thrown if error getting the request body */ protected Object getRequestBody(Exchange exchange) throws Exception { Object body = NettyPayloadHelper.getIn(getEndpoint(), exchange); if (body == null) { return null; } // if textline enabled then covert to a String which must be used for textline if (getConfiguration().isTextline()) { body = NettyHelper.getTextlineBody(body, exchange, getConfiguration().getDelimiter(), getConfiguration().isAutoAppendDelimiter()); } return body; }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // only close if we are still allowed to run if (consumer.isRunAllowed()) { // let the exception handler deal with it consumer.getExceptionHandler().handleException("Closing channel as an exception was thrown from Netty", cause); // close channel in case an exception was thrown NettyHelper.close(ctx.channel()); } }
@Override public void onComplete(Exchange exchange) { // should channel be closed after complete? Boolean close; if (ExchangeHelper.isOutCapable(exchange)) { close = exchange.getOut().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class); } else { close = exchange.getIn().getHeader(NettyConstants.NETTY_CLOSE_CHANNEL_WHEN_COMPLETE, Boolean.class); } // should we disconnect, the header can override the configuration boolean disconnect = getConfiguration().isDisconnect(); if (close != null) { disconnect = close; } if (disconnect) { LOG.trace("Closing channel {} as routing the Exchange is done", channel); NettyHelper.close(channel); } releaseChannel(channelFuture); } });
NettyHelper.close(ctx.channel());
LOG.trace("Closing channel when complete at address: {}", remoteAddress); NettyHelper.close(future.channel());
LOG.trace("Closing channel when complete at address: {}", getEndpoint().getConfiguration().getAddress()); NettyHelper.close(channel);
LOG.trace("Closing channel when complete at address: {}", producer.getConfiguration().getAddress()); NettyHelper.close(ctx.channel());