Search in sources :

Example 6 with FailedToCreateProducerException

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

the class JmsProducer method initReplyManager.

protected void initReplyManager() {
    if (!started.get()) {
        synchronized (this) {
            if (started.get()) {
                return;
            }
            // must use the classloader from the application context when creating reply manager,
            // as it should inherit the classloader from app context and not the current which may be
            // a different classloader
            ClassLoader current = Thread.currentThread().getContextClassLoader();
            ClassLoader ac = endpoint.getCamelContext().getApplicationContextClassLoader();
            try {
                if (ac != null) {
                    Thread.currentThread().setContextClassLoader(ac);
                }
                // validate that replyToType and replyTo is configured accordingly
                if (endpoint.getReplyToType() != null) {
                    // setting temporary with a fixed replyTo is not supported
                    if (endpoint.getReplyTo() != null && endpoint.getReplyToType().equals(ReplyToType.Temporary.name())) {
                        throw new IllegalArgumentException("ReplyToType " + ReplyToType.Temporary + " is not supported when replyTo " + endpoint.getReplyTo() + " is also configured.");
                    }
                }
                if (endpoint.getReplyTo() != null) {
                    replyManager = createReplyManager(endpoint.getReplyTo());
                    if (LOG.isDebugEnabled()) {
                        LOG.debug("Using JmsReplyManager: {} to process replies from: {}", replyManager, endpoint.getReplyTo());
                    }
                } else {
                    replyManager = createReplyManager();
                    LOG.debug("Using JmsReplyManager: {} to process replies from temporary queue", replyManager);
                }
            } catch (Exception e) {
                throw new FailedToCreateProducerException(endpoint, e);
            } finally {
                if (ac != null) {
                    Thread.currentThread().setContextClassLoader(current);
                }
            }
            started.set(true);
        }
    }
}
Also used : FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) JMSException(javax.jms.JMSException) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) RuntimeExchangeException(org.apache.camel.RuntimeExchangeException)

Example 7 with FailedToCreateProducerException

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

the class RoutingSlip method processExchange.

protected boolean processExchange(final Endpoint endpoint, final Exchange exchange, final Exchange original, final AsyncCallback callback, final RoutingSlipIterator iter) {
    // this does the actual processing so log at trace level
    log.trace("Processing exchangeId: {} >>> {}", exchange.getExchangeId(), exchange);
    boolean sync = producerCache.doInAsyncProducer(endpoint, exchange, null, callback, new AsyncProducerCallback() {

        public boolean doInAsyncProducer(Producer producer, AsyncProcessor asyncProducer, final Exchange exchange, ExchangePattern exchangePattern, final AsyncCallback callback) {
            // rework error handling to support fine grained error handling
            RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
            AsyncProcessor target = createErrorHandler(routeContext, exchange, asyncProducer, endpoint);
            // set property which endpoint we send to
            exchange.setProperty(Exchange.TO_ENDPOINT, endpoint.getEndpointUri());
            exchange.setProperty(Exchange.SLIP_ENDPOINT, endpoint.getEndpointUri());
            boolean answer = target.process(exchange, new AsyncCallback() {

                public void done(boolean doneSync) {
                    // we only have to handle async completion of the routing slip
                    if (doneSync) {
                        callback.done(doneSync);
                        return;
                    }
                    // continue processing the routing slip asynchronously
                    Exchange current = prepareExchangeForRoutingSlip(exchange, endpoint);
                    while (iter.hasNext(current)) {
                        // we ignore some kind of exceptions and allow us to continue
                        if (isIgnoreInvalidEndpoints()) {
                            FailedToCreateProducerException e = current.getException(FailedToCreateProducerException.class);
                            if (e != null) {
                                if (log.isDebugEnabled()) {
                                    log.debug("Endpoint uri is invalid: " + endpoint + ". This exception will be ignored.", e);
                                }
                                current.setException(null);
                            }
                        }
                        // check for error if so we should break out
                        if (!continueProcessing(current, "so breaking out of the routing slip", log)) {
                            break;
                        }
                        Endpoint endpoint;
                        try {
                            endpoint = resolveEndpoint(iter, exchange);
                            // if no endpoint was resolved then try the next
                            if (endpoint == null) {
                                continue;
                            }
                        } catch (Exception e) {
                            // error resolving endpoint so we should break out
                            exchange.setException(e);
                            break;
                        }
                        // prepare and process the routing slip
                        boolean sync = processExchange(endpoint, current, original, callback, iter);
                        current = prepareExchangeForRoutingSlip(current, endpoint);
                        if (!sync) {
                            log.trace("Processing exchangeId: {} is continued being processed asynchronously", original.getExchangeId());
                            return;
                        }
                    }
                    // logging nextExchange as it contains the exchange that might have altered the payload and since
                    // we are logging the completion if will be confusing if we log the original instead
                    // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots
                    log.trace("Processing complete for exchangeId: {} >>> {}", original.getExchangeId(), current);
                    // copy results back to the original exchange
                    ExchangeHelper.copyResults(original, current);
                    if (target instanceof DeadLetterChannel) {
                        Processor deadLetter = ((DeadLetterChannel) target).getDeadLetter();
                        try {
                            ServiceHelper.stopService(deadLetter);
                        } catch (Exception e) {
                            log.warn("Error stopping DeadLetterChannel error handler on routing slip. This exception is ignored.", e);
                        }
                    }
                    callback.done(false);
                }
            });
            // stop error handler if we completed synchronously
            if (answer) {
                if (target instanceof DeadLetterChannel) {
                    Processor deadLetter = ((DeadLetterChannel) target).getDeadLetter();
                    try {
                        ServiceHelper.stopService(deadLetter);
                    } catch (Exception e) {
                        log.warn("Error stopping DeadLetterChannel error handler on routing slip. This exception is ignored.", e);
                    }
                }
            }
            return answer;
        }
    });
    return sync;
}
Also used : Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) RouteContext(org.apache.camel.spi.RouteContext) Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) AsyncProducerCallback(org.apache.camel.AsyncProducerCallback) Producer(org.apache.camel.Producer) Endpoint(org.apache.camel.Endpoint) ExchangePattern(org.apache.camel.ExchangePattern) AsyncProcessor(org.apache.camel.AsyncProcessor)

Example 8 with FailedToCreateProducerException

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

the class RoutingSlip method doRoutingSlipWithExpression.

private boolean doRoutingSlipWithExpression(final Exchange exchange, final Expression expression, final AsyncCallback callback) {
    Exchange current = exchange;
    RoutingSlipIterator iter;
    try {
        iter = createRoutingSlipIterator(exchange, expression);
    } catch (Exception e) {
        exchange.setException(e);
        callback.done(true);
        return true;
    }
    // ensure the slip is empty when we start
    if (current.hasProperties()) {
        current.setProperty(Exchange.SLIP_ENDPOINT, null);
    }
    while (iter.hasNext(current)) {
        Endpoint endpoint;
        try {
            endpoint = resolveEndpoint(iter, exchange);
            // if no endpoint was resolved then try the next
            if (endpoint == null) {
                continue;
            }
        } catch (Exception e) {
            // error resolving endpoint so we should break out
            current.setException(e);
            break;
        }
        //process and prepare the routing slip
        boolean sync = processExchange(endpoint, current, exchange, callback, iter);
        current = prepareExchangeForRoutingSlip(current, endpoint);
        if (!sync) {
            log.trace("Processing exchangeId: {} is continued being processed asynchronously", exchange.getExchangeId());
            // so we break out now, then the callback will be invoked which then continue routing from where we left here
            return false;
        }
        log.trace("Processing exchangeId: {} is continued being processed synchronously", exchange.getExchangeId());
        // we ignore some kind of exceptions and allow us to continue
        if (isIgnoreInvalidEndpoints()) {
            FailedToCreateProducerException e = current.getException(FailedToCreateProducerException.class);
            if (e != null) {
                if (log.isDebugEnabled()) {
                    log.debug("Endpoint uri is invalid: " + endpoint + ". This exception will be ignored.", e);
                }
                current.setException(null);
            }
        }
        // check for error if so we should break out
        if (!continueProcessing(current, "so breaking out of the routing slip", log)) {
            break;
        }
    }
    // logging nextExchange as it contains the exchange that might have altered the payload and since
    // we are logging the completion if will be confusing if we log the original instead
    // we could also consider logging the original and the nextExchange then we have *before* and *after* snapshots
    log.trace("Processing complete for exchangeId: {} >>> {}", exchange.getExchangeId(), current);
    // copy results back to the original exchange
    ExchangeHelper.copyResults(exchange, current);
    callback.done(true);
    return true;
}
Also used : Exchange(org.apache.camel.Exchange) DefaultExchange(org.apache.camel.impl.DefaultExchange) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) Endpoint(org.apache.camel.Endpoint) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 9 with FailedToCreateProducerException

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

the class CamelProxyFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    if (endpoint == null) {
        getCamelContext();
        if (getServiceUrl() == null && getServiceRef() == null) {
            throw new IllegalArgumentException("serviceUrl or serviceRef must be specified.");
        }
        if (getServiceInterface() == null) {
            throw new IllegalArgumentException("serviceInterface must be specified.");
        }
        // lookup endpoint or we have the url for it
        if (getServiceRef() != null) {
            endpoint = getCamelContext().getRegistry().lookupByNameAndType(getServiceRef(), Endpoint.class);
        } else {
            endpoint = getCamelContext().getEndpoint(getServiceUrl());
        }
        if (endpoint == null) {
            throw new IllegalArgumentException("Could not resolve endpoint: " + getServiceUrl());
        }
    }
    // binding is enabled by default
    boolean bind = getBinding() != null ? getBinding() : true;
    try {
        // need to start endpoint before we create producer
        ServiceHelper.startService(endpoint);
        producer = endpoint.createProducer();
        // add and start producer
        getCamelContext().addService(producer, true, true);
        Class<?> clazz = blueprintContainer.loadClass(getServiceInterface());
        serviceProxy = ProxyHelper.createProxy(endpoint, bind, producer, clazz);
    } catch (Exception e) {
        throw new FailedToCreateProducerException(endpoint, e);
    }
}
Also used : FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) Endpoint(org.apache.camel.Endpoint) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Example 10 with FailedToCreateProducerException

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

the class XmlProxyFactoryBean method create.

@Override
public T create(CreationalContext<T> creationalContext) {
    try {
        CamelContext context = isNotEmpty(proxy.getCamelContextId()) ? getReferenceByName(manager, proxy.getCamelContextId(), CamelContext.class).get() : getReference(manager, CamelContext.class, this.context);
        Endpoint endpoint;
        if (isNotEmpty(proxy.getServiceRef())) {
            endpoint = context.getRegistry().lookupByNameAndType(proxy.getServiceRef(), Endpoint.class);
        } else {
            if (isNotEmpty(proxy.getServiceUrl())) {
                endpoint = context.getEndpoint(proxy.getServiceUrl());
            } else {
                throw new IllegalStateException("serviceUrl or serviceRef must not be empty!");
            }
        }
        if (endpoint == null) {
            throw new UnsatisfiedResolutionException("Could not resolve endpoint: " + (isNotEmpty(proxy.getServiceRef()) ? proxy.getServiceRef() : proxy.getServiceUrl()));
        }
        // binding is enabled by default
        boolean bind = proxy.getBinding() != null ? proxy.getBinding() : true;
        try {
            // Start the endpoint before we create the producer
            startService(endpoint);
            Producer producer = endpoint.createProducer();
            // Add and start the producer
            context.addService(producer, true, true);
            return createProxy(endpoint, bind, producer, (Class<T>) proxy.getServiceInterface());
        } catch (Exception cause) {
            throw new FailedToCreateProducerException(endpoint, cause);
        }
    } catch (Exception cause) {
        throw new CreationException("Error while creating instance for " + this, cause);
    }
}
Also used : CamelContext(org.apache.camel.CamelContext) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException) UnsatisfiedResolutionException(javax.enterprise.inject.UnsatisfiedResolutionException) CreationException(javax.enterprise.inject.CreationException) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException)

Aggregations

FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)11 Endpoint (org.apache.camel.Endpoint)5 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)3 Producer (org.apache.camel.Producer)3 JMSException (javax.jms.JMSException)2 Exchange (org.apache.camel.Exchange)2 RuntimeExchangeException (org.apache.camel.RuntimeExchangeException)2 RouteBuilder (org.apache.camel.builder.RouteBuilder)2 DefaultExchange (org.apache.camel.impl.DefaultExchange)2 Test (org.junit.Test)2 HttpException (com.meterware.httpunit.HttpException)1 IOException (java.io.IOException)1 CreationException (javax.enterprise.inject.CreationException)1 UnsatisfiedResolutionException (javax.enterprise.inject.UnsatisfiedResolutionException)1 Connection (javax.jms.Connection)1 AsyncCallback (org.apache.camel.AsyncCallback)1 AsyncProcessor (org.apache.camel.AsyncProcessor)1 AsyncProducerCallback (org.apache.camel.AsyncProducerCallback)1 CamelContext (org.apache.camel.CamelContext)1 ExchangePattern (org.apache.camel.ExchangePattern)1