use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class ExchangeHelper method createCorrelatedCopy.
/**
* Creates a new instance and copies from the current message exchange so that it can be
* forwarded to another destination as a new instance. Unlike regular copy this operation
* will not share the same {@link org.apache.camel.spi.UnitOfWork} so its should be used
* for async messaging, where the original and copied exchange are independent.
*
* @param exchange original copy of the exchange
* @param handover whether the on completion callbacks should be handed over to the new copy.
* @param useSameMessageId whether to use same message id on the copy message.
* @param filter whether to handover the on completion
*/
public static Exchange createCorrelatedCopy(Exchange exchange, boolean handover, boolean useSameMessageId, Predicate<Synchronization> filter) {
String id = exchange.getExchangeId();
// make sure to do a safe copy as the correlated copy can be routed independently of the source.
Exchange copy = exchange.copy(true);
// do not reuse message id on copy
if (!useSameMessageId) {
if (copy.hasOut()) {
copy.getOut().setMessageId(null);
}
copy.getIn().setMessageId(null);
}
// do not share the unit of work
copy.setUnitOfWork(null);
// do not reuse the message id
// hand over on completion to the copy if we got any
UnitOfWork uow = exchange.getUnitOfWork();
if (handover && uow != null) {
uow.handoverSynchronization(copy, filter);
}
// set a correlation id so we can track back the original exchange
copy.setProperty(Exchange.CORRELATION_ID, id);
return copy;
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class DefaultManagementLifecycleStrategy method getManagedObjectForService.
@SuppressWarnings("unchecked")
private Object getManagedObjectForService(CamelContext context, Service service, Route route) {
// skip channel, UoW and dont double wrap instrumentation
if (service instanceof Channel || service instanceof UnitOfWork || service instanceof InstrumentationProcessor) {
return null;
}
// skip non managed services
if (service instanceof NonManagedService) {
return null;
}
Object answer = null;
if (service instanceof ManagementAware) {
return ((ManagementAware<Service>) service).getManagedObject(service);
} else if (service instanceof Tracer) {
// special for tracer
Tracer tracer = (Tracer) service;
ManagedTracer mt = managedTracers.get(tracer);
if (mt == null) {
mt = new ManagedTracer(context, tracer);
mt.init(getManagementStrategy());
managedTracers.put(tracer, mt);
}
return mt;
} else if (service instanceof BacklogTracer) {
// special for backlog tracer
BacklogTracer backlogTracer = (BacklogTracer) service;
ManagedBacklogTracer mt = managedBacklogTracers.get(backlogTracer);
if (mt == null) {
mt = new ManagedBacklogTracer(context, backlogTracer);
mt.init(getManagementStrategy());
managedBacklogTracers.put(backlogTracer, mt);
}
return mt;
} else if (service instanceof BacklogDebugger) {
// special for backlog debugger
BacklogDebugger backlogDebugger = (BacklogDebugger) service;
ManagedBacklogDebugger md = managedBacklogDebuggers.get(backlogDebugger);
if (md == null) {
md = new ManagedBacklogDebugger(context, backlogDebugger);
md.init(getManagementStrategy());
managedBacklogDebuggers.put(backlogDebugger, md);
}
return md;
} else if (service instanceof DataFormat) {
answer = getManagementObjectStrategy().getManagedObjectForDataFormat(context, (DataFormat) service);
} else if (service instanceof Producer) {
answer = getManagementObjectStrategy().getManagedObjectForProducer(context, (Producer) service);
} else if (service instanceof Consumer) {
answer = getManagementObjectStrategy().getManagedObjectForConsumer(context, (Consumer) service);
} else if (service instanceof Processor) {
// special for processors as we need to do some extra work
return getManagedObjectForProcessor(context, (Processor) service, route);
} else if (service instanceof ThrottlingInflightRoutePolicy) {
answer = new ManagedThrottlingInflightRoutePolicy(context, (ThrottlingInflightRoutePolicy) service);
} else if (service instanceof ThrottlingExceptionRoutePolicy) {
answer = new ManagedThrottlingExceptionRoutePolicy(context, (ThrottlingExceptionRoutePolicy) service);
} else if (service instanceof ConsumerCache) {
answer = new ManagedConsumerCache(context, (ConsumerCache) service);
} else if (service instanceof ProducerCache) {
answer = new ManagedProducerCache(context, (ProducerCache) service);
} else if (service instanceof DefaultEndpointRegistry) {
answer = new ManagedEndpointRegistry(context, (DefaultEndpointRegistry) service);
} else if (service instanceof TypeConverterRegistry) {
answer = new ManagedTypeConverterRegistry(context, (TypeConverterRegistry) service);
} else if (service instanceof RestRegistry) {
answer = new ManagedRestRegistry(context, (RestRegistry) service);
} else if (service instanceof InflightRepository) {
answer = new ManagedInflightRepository(context, (InflightRepository) service);
} else if (service instanceof AsyncProcessorAwaitManager) {
answer = new ManagedAsyncProcessorAwaitManager(context, (AsyncProcessorAwaitManager) service);
} else if (service instanceof RuntimeEndpointRegistry) {
answer = new ManagedRuntimeEndpointRegistry(context, (RuntimeEndpointRegistry) service);
} else if (service instanceof StreamCachingStrategy) {
answer = new ManagedStreamCachingStrategy(context, (StreamCachingStrategy) service);
} else if (service instanceof EventNotifier) {
answer = getManagementObjectStrategy().getManagedObjectForEventNotifier(context, (EventNotifier) service);
} else if (service instanceof TransformerRegistry) {
answer = new ManagedTransformerRegistry(context, (TransformerRegistry) service);
} else if (service instanceof ValidatorRegistry) {
answer = new ManagedValidatorRegistry(context, (ValidatorRegistry) service);
} else if (service instanceof RuntimeCamelCatalog) {
answer = new ManagedRuntimeCamelCatalog(context, (RuntimeCamelCatalog) service);
} else if (service != null) {
// fallback as generic service
answer = getManagementObjectStrategy().getManagedObjectForService(context, service);
}
if (answer != null && answer instanceof ManagedService) {
ManagedService ms = (ManagedService) answer;
ms.setRoute(route);
ms.init(getManagementStrategy());
}
return answer;
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class ExchangeHelper method getOriginalInMessage.
/**
* Gets the original IN {@link Message} this Unit of Work was started with.
* <p/>
* The original message is only returned if the option {@link org.apache.camel.RuntimeConfiguration#isAllowUseOriginalMessage()}
* is enabled. If its disabled, then <tt>null</tt> is returned.
*
* @return the original IN {@link Message}, or <tt>null</tt> if using original message is disabled.
*/
public static Message getOriginalInMessage(Exchange exchange) {
Message answer = null;
// try parent first
UnitOfWork uow = exchange.getProperty(Exchange.PARENT_UNIT_OF_WORK, UnitOfWork.class);
if (uow != null) {
answer = uow.getOriginalInMessage();
}
// fallback to the current exchange
if (answer == null) {
uow = exchange.getUnitOfWork();
if (uow != null) {
answer = uow.getOriginalInMessage();
}
}
return answer;
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class CachedOutputStreamTest method setUp.
protected void setUp() throws Exception {
super.setUp();
deleteDirectory("target/cachedir");
createDirectory("target/cachedir");
exchange = new DefaultExchange(context);
UnitOfWork uow = new DefaultUnitOfWork(exchange);
exchange.setUnitOfWork(uow);
}
use of org.apache.camel.spi.UnitOfWork in project camel by apache.
the class BridgeExceptionHandlerToErrorHandler method handleException.
@Override
public void handleException(String message, Exchange exchange, Throwable exception) {
if (exchange == null) {
exchange = consumer.getEndpoint().createExchange();
}
// set the caused exception
exchange.setException(exception);
// and the message
exchange.getIn().setBody(message);
// and mark as redelivery exhausted as we cannot do redeliveries
exchange.setProperty(Exchange.REDELIVERY_EXHAUSTED, Boolean.TRUE);
// wrap in UoW
UnitOfWork uow = null;
try {
uow = consumer.createUoW(exchange);
bridge.process(exchange);
} catch (Exception e) {
fallback.handleException("Error handling exception " + exception.getMessage(), exchange, e);
} finally {
UnitOfWorkHelper.doneUow(uow, exchange);
}
}
Aggregations