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);
}
}
}
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;
}
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;
}
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);
}
}
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);
}
}
Aggregations