public Consumer createConsumer(Processor processor) throws Exception { Consumer answer = new NettyConsumer(this, processor, configuration); configureConsumer(answer); return answer; }
private void processSynchronously(final Exchange exchange, final ChannelHandlerContext ctx, final Object message) { try { consumer.getProcessor().process(exchange); if (consumer.getConfiguration().isSync()) { sendResponse(message, ctx, exchange); } } catch (Throwable e) { consumer.getExceptionHandler().handleException(e); } finally { consumer.doneUoW(exchange); } }
private void processAsynchronously(final Exchange exchange, final ChannelHandlerContext ctx, final Object message) { consumer.getAsyncProcessor().process(exchange, new AsyncCallback() { @Override public void done(boolean doneSync) { // send back response if the communication is synchronous try { if (consumer.getConfiguration().isSync()) { sendResponse(message, ctx, exchange); } } catch (Throwable e) { consumer.getExceptionHandler().handleException(e); } finally { consumer.doneUoW(exchange); } } }); }
@Override public void done(boolean doneSync) { // send back response if the communication is synchronous try { if (consumer.getConfiguration().isSync()) { sendResponse(message, ctx, exchange); } } catch (Throwable e) { consumer.getExceptionHandler().handleException(e); } finally { consumer.doneUoW(exchange); } } });
public DefaultServerInitializerFactory(NettyConsumer consumer) { this.consumer = consumer; try { this.sslContext = createSSLContext(consumer.getContext(), consumer.getConfiguration()); } catch (Exception e) { throw ObjectHelper.wrapRuntimeCamelException(e); } if (sslContext != null) { LOG.info("Created SslContext {}", sslContext); } }
public NettyConsumer(NettyEndpoint nettyEndpoint, Processor processor, NettyConfiguration configuration) { super(nettyEndpoint, processor); this.context = this.getEndpoint().getCamelContext(); this.configuration = configuration; setNettyServerBootstrapFactory(configuration.getNettyServerBootstrapFactory()); setExceptionHandler(new NettyConsumerExceptionHandler(this)); }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Object in = msg; if (LOG.isDebugEnabled()) { LOG.debug("Channel: {} received body: {}", ctx.channel(), in); } // create Exchange and let the consumer process it final Exchange exchange = consumer.getEndpoint().createExchange(ctx, msg); if (consumer.getConfiguration().isSync()) { exchange.setPattern(ExchangePattern.InOut); } // set the exchange charset property for converting if (consumer.getConfiguration().getCharsetName() != null) { exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(consumer.getConfiguration().getCharsetName())); } if (consumer.getConfiguration().isReuseChannel()) { exchange.setProperty(NettyConstants.NETTY_CHANNEL, ctx.channel()); } // we want to handle the UoW consumer.createUoW(exchange); beforeProcess(exchange, ctx, msg); // process accordingly to endpoint configuration if (consumer.getEndpoint().isSynchronous()) { processSynchronously(exchange, ctx, msg); } else { processAsynchronously(exchange, ctx, msg); } }
@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()); } }
consumer.getExceptionHandler().handleException(e); boolean disconnect = consumer.getConfiguration().isDisconnect(); if (close != null) { disconnect = close;
List<ChannelHandler> encoders = consumer.getConfiguration().getEncoders(); for (int x = 0; x < encoders.size(); x++) { ChannelHandler encoder = encoders.get(x); List<ChannelHandler> decoders = consumer.getConfiguration().getDecoders(); for (int x = 0; x < decoders.size(); x++) { ChannelHandler decoder = decoders.get(x); if (consumer.getConfiguration().isUsingExecutorService()) { EventExecutorGroup applicationExecutor = consumer.getEndpoint().getComponent().getExecutorService(); addToPipeline("handler", channelPipeline, applicationExecutor, new ServerChannelHandler(consumer));
public NettyConsumerExceptionHandler(NettyConsumer consumer) { this.logger = new CamelLogger(LOG, consumer.getConfiguration().getServerExceptionCaughtLogLevel()); this.closedLoggingLevel = consumer.getConfiguration().getServerClosedChannelExceptionCaughtLogLevel(); }
@Override public void channelActive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel open: {}", ctx.channel()); } // to keep track of open sockets consumer.getNettyServerBootstrapFactory().addChannel(ctx.channel()); super.channelActive(ctx); }
/** * Gets the object we want to use as the response object for sending to netty. * * @param exchange the exchange * @return the object to use as response * @throws Exception is thrown if error getting the response body */ protected Object getResponseBody(Exchange exchange) throws Exception { // if there was an exception then use that as response body boolean exception = exchange.getException() != null && !consumer.getEndpoint().getConfiguration().isTransferExchange(); if (exception) { return exchange.getException(); } if (exchange.hasOut()) { return NettyPayloadHelper.getOut(consumer.getEndpoint(), exchange); } else { return NettyPayloadHelper.getIn(consumer.getEndpoint(), exchange); } }
private SslHandler configureServerSSLOnDemand() throws Exception { if (!consumer.getConfiguration().isSsl()) { return null; } if (consumer.getConfiguration().getSslHandler() != null) { return consumer.getConfiguration().getSslHandler(); } else if (sslContext != null) { SSLEngine engine = sslContext.createSSLEngine(); engine.setUseClientMode(consumer.getConfiguration().isClientMode()); engine.setNeedClientAuth(consumer.getConfiguration().isNeedClientAuth()); if (consumer.getConfiguration().getSslContextParameters() == null) { // just set the enabledProtocols if the SslContextParameter doesn't set engine.setEnabledProtocols(consumer.getConfiguration().getEnabledProtocols().split(",")); } return new SslHandler(engine); } return null; }
@Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { if (LOG.isTraceEnabled()) { LOG.trace("Channel closed: {}", ctx.channel()); } // to keep track of open sockets consumer.getNettyServerBootstrapFactory().removeChannel(ctx.channel()); super.channelInactive(ctx); }
public ServerChannelHandler(NettyConsumer consumer) { this.consumer = consumer; this.noReplyLogger = new CamelLogger(LOG, consumer.getConfiguration().getNoReplyLogLevel()); }
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); } } }