Search in sources :

Example 26 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class NettyProducer method process.

public boolean process(final Exchange exchange, AsyncCallback callback) {
    if (!isRunAllowed()) {
        if (exchange.getException() == null) {
            exchange.setException(new RejectedExecutionException());
        }
        callback.done(true);
        return true;
    }
    Object body;
    try {
        body = getRequestBody(exchange);
        if (body == null) {
            noReplyLogger.log("No payload to send for exchange: " + exchange);
            callback.done(true);
            return true;
        }
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // set the exchange encoding property
    if (getConfiguration().getCharsetName() != null) {
        exchange.setProperty(Exchange.CHARSET_NAME, IOHelper.normalizeCharset(getConfiguration().getCharsetName()));
    }
    if (LOG.isTraceEnabled()) {
        LOG.trace("Pool[active={}, idle={}]", pool.getNumActive(), pool.getNumIdle());
    }
    // get a channel from the pool
    Channel existing;
    try {
        existing = pool.borrowObject();
        if (existing != null) {
            LOG.trace("Got channel from pool {}", existing);
        }
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // we must have a channel
    if (existing == null) {
        exchange.setException(new CamelExchangeException("Cannot get channel from pool", exchange));
        callback.done(true);
        return true;
    }
    if (exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT) != null) {
        long timeoutInMs = exchange.getIn().getHeader(NettyConstants.NETTY_REQUEST_TIMEOUT, Long.class);
        ChannelHandler oldHandler = existing.getPipeline().get("timeout");
        ReadTimeoutHandler newHandler = new ReadTimeoutHandler(getEndpoint().getTimer(), timeoutInMs, TimeUnit.MILLISECONDS);
        if (oldHandler == null) {
            existing.getPipeline().addBefore("handler", "timeout", newHandler);
        } else {
            existing.getPipeline().replace(oldHandler, "timeout", newHandler);
        }
    }
    // need to declare as final
    final Channel channel = existing;
    final AsyncCallback producerCallback = new NettyProducerCallback(channel, callback);
    // setup state as attachment on the channel, so we can access the state later when needed
    channel.setAttachment(new NettyCamelState(producerCallback, exchange));
    InetSocketAddress remoteAddress = null;
    if (!isTcp()) {
        // Need to specify the remoteAddress for udp connection
        remoteAddress = new InetSocketAddress(configuration.getHost(), configuration.getPort());
    }
    // write body
    NettyHelper.writeBodyAsync(LOG, channel, remoteAddress, body, exchange, new ChannelFutureListener() {

        public void operationComplete(ChannelFuture channelFuture) throws Exception {
            LOG.trace("Operation complete {}", channelFuture);
            if (!channelFuture.isSuccess()) {
                // no success then exit, (any exception has been handled by ClientChannelHandler#exceptionCaught)
                return;
            }
            // if we do not expect any reply then signal callback to continue routing
            if (!configuration.isSync()) {
                try {
                    // 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) {
                        if (LOG.isTraceEnabled()) {
                            LOG.trace("Closing channel when complete at address: {}", getEndpoint().getConfiguration().getAddress());
                        }
                        NettyHelper.close(channel);
                    }
                } finally {
                    // signal callback to continue routing
                    producerCallback.done(false);
                }
            }
        }
    });
    // continue routing asynchronously
    return false;
}
Also used : ChannelFuture(org.jboss.netty.channel.ChannelFuture) SucceededChannelFuture(org.jboss.netty.channel.SucceededChannelFuture) CamelExchangeException(org.apache.camel.CamelExchangeException) InetSocketAddress(java.net.InetSocketAddress) Channel(org.jboss.netty.channel.Channel) AsyncCallback(org.apache.camel.AsyncCallback) ChannelHandler(org.jboss.netty.channel.ChannelHandler) ChannelFutureListener(org.jboss.netty.channel.ChannelFutureListener) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) CamelException(org.apache.camel.CamelException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) ConnectException(java.net.ConnectException) CamelExchangeException(org.apache.camel.CamelExchangeException) ReadTimeoutHandler(org.jboss.netty.handler.timeout.ReadTimeoutHandler)

Example 27 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class ClientChannelHandler method channelClosed.

@Override
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
    if (LOG.isTraceEnabled()) {
        LOG.trace("Channel closed: {}", ctx.getChannel());
    }
    Exchange exchange = getExchange(ctx);
    AsyncCallback callback = getAsyncCallback(ctx);
    // remove state
    producer.removeState(ctx.getChannel());
    // to keep track of open sockets
    producer.getAllChannels().remove(ctx.getChannel());
    // this channel is maybe closing graceful and the exchange is already done
    // and if so we should not trigger an exception
    boolean doneUoW = exchange.getUnitOfWork() == null;
    if (producer.getConfiguration().isSync() && !doneUoW && !messageReceived && !exceptionHandled) {
        // To avoid call the callback.done twice 
        exceptionHandled = true;
        // and could not return a response. We should count down to stop waiting for a response
        if (LOG.isDebugEnabled()) {
            LOG.debug("Channel closed but no message received from address: {}", producer.getConfiguration().getAddress());
        }
        exchange.setException(new CamelExchangeException("No response received from remote server: " + producer.getConfiguration().getAddress(), exchange));
        // signal callback
        callback.done(false);
    }
    // make sure the event can be processed by other handlers
    super.channelClosed(ctx, e);
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) AsyncCallback(org.apache.camel.AsyncCallback)

Example 28 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class OpenShiftProducer method doStart.

protected void doStart(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        app.start();
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Example 29 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class OpenShiftProducer method doRemoveEnvironmentVariable.

protected void doRemoveEnvironmentVariable(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String variableName = exchange.getIn().getHeader(OpenShiftConstants.ENVIRONMENT_VARIABLE_NAME, getEndpoint().getApplication(), String.class);
        if (!app.canGetEnvironmentVariables()) {
            throw new CamelExchangeException("The application with id " + name + " can't get Environment Variables", exchange);
        }
        if (ObjectHelper.isNotEmpty(variableName)) {
            app.removeEnvironmentVariable(variableName);
            exchange.getIn().setBody(variableName);
        } else {
            throw new CamelExchangeException("Environment variable name not specified", exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Example 30 with CamelExchangeException

use of org.apache.camel.CamelExchangeException in project camel by apache.

the class OpenShiftProducer method doGetDeploymentType.

protected void doGetDeploymentType(Exchange exchange, IDomain domain) throws CamelExchangeException {
    String name = exchange.getIn().getHeader(OpenShiftConstants.APPLICATION, getEndpoint().getApplication(), String.class);
    if (name == null) {
        throw new CamelExchangeException("Application not specified", exchange);
    }
    IApplication app = domain.getApplicationByName(name);
    if (app == null) {
        throw new CamelExchangeException("Application with id " + name + " not found.", exchange);
    } else {
        String deploymentType = app.getDeploymentType();
        exchange.getIn().setBody(deploymentType);
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) IApplication(com.openshift.client.IApplication)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3