use of org.apache.camel.spi.RouteContext in project camel by apache.
the class EndpointReferenceTest method testReferenceEndpointFromOtherCamelContext.
public void testReferenceEndpointFromOtherCamelContext() throws Exception {
CamelContext context = applicationContext.getBean("camel2", CamelContext.class);
RouteContext routeContext = new DefaultRouteContext(context);
try {
routeContext.resolveEndpoint(null, "endpoint1");
fail("Should have thrown exception");
} catch (NoSuchEndpointException exception) {
assertTrue("Get a wrong exception message", exception.getMessage().contains("make sure the endpoint has the same camel context as the route does"));
}
}
use of org.apache.camel.spi.RouteContext in project camel by apache.
the class DefaultRuntimeEndpointRegistry method getRouteId.
private String getRouteId(Exchange exchange) {
String answer = null;
UnitOfWork uow = exchange.getUnitOfWork();
RouteContext rc = uow != null ? uow.getRouteContext() : null;
if (rc != null) {
answer = rc.getRoute().getId();
}
if (answer == null) {
// fallback and get from route id on the exchange
answer = exchange.getFromRouteId();
}
return answer;
}
use of org.apache.camel.spi.RouteContext 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.spi.RouteContext in project camel by apache.
the class MulticastProcessor method createProcessorExchangePairs.
protected Iterable<ProcessorExchangePair> createProcessorExchangePairs(Exchange exchange) throws Exception {
List<ProcessorExchangePair> result = new ArrayList<ProcessorExchangePair>(processors.size());
StreamCache streamCache = null;
if (isParallelProcessing() && exchange.getIn().getBody() instanceof StreamCache) {
// in parallel processing case, the stream must be copied, therefore get the stream
streamCache = (StreamCache) exchange.getIn().getBody();
}
int index = 0;
for (Processor processor : processors) {
// copy exchange, and do not share the unit of work
Exchange copy = ExchangeHelper.createCorrelatedCopy(exchange, false);
if (streamCache != null) {
if (index > 0) {
// copy it otherwise parallel processing is not possible,
// because streams can only be read once
StreamCache copiedStreamCache = streamCache.copy(copy);
if (copiedStreamCache != null) {
copy.getIn().setBody(copiedStreamCache);
}
}
}
// if it is not already set.
if (copy.getProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK) == null) {
copy.setProperty(Exchange.STREAM_CACHE_UNIT_OF_WORK, exchange.getUnitOfWork());
}
// if we share unit of work, we need to prepare the child exchange
if (isShareUnitOfWork()) {
prepareSharedUnitOfWork(copy, exchange);
}
// and add the pair
RouteContext routeContext = exchange.getUnitOfWork() != null ? exchange.getUnitOfWork().getRouteContext() : null;
result.add(createProcessorExchangePair(index++, processor, copy, routeContext));
}
if (exchange.getException() != null) {
// before returning the answer;
throw exchange.getException();
}
return result;
}
use of org.apache.camel.spi.RouteContext in project camel by apache.
the class TemporalRule method getOverdueAction.
public Processor getOverdueAction() throws Exception {
if (overdueAction == null && overdueProcessors != null) {
RouteDefinition route = new RouteDefinition();
RouteContext routeContext = new DefaultRouteContext(first.getBuilder().getProcessBuilder().getContext(), route, null, new ArrayList<Route>());
overdueAction = overdueProcessors.createOutputsProcessor(routeContext);
}
return overdueAction;
}
Aggregations