use of org.apache.camel.Producer in project camel by apache.
the class SendProcessor method doStart.
protected void doStart() throws Exception {
if (producerCache == null) {
// use a single producer cache as we need to only hold reference for one destination
// and use a regular HashMap as we do not want a soft reference store that may get re-claimed when low on memory
// as we want to ensure the producer is kept around, to ensure its lifecycle is fully managed,
// eg stopping the producer when we stop etc.
producerCache = new ProducerCache(this, camelContext, new HashMap<String, Producer>(1));
// do not add as service as we do not want to manage the producer cache
}
ServiceHelper.startService(producerCache);
// the destination could since have been intercepted by a interceptSendToEndpoint so we got to
// lookup this before we can use the destination
Endpoint lookup = camelContext.hasEndpoint(destination.getEndpointKey());
if (lookup instanceof InterceptSendToEndpoint) {
if (LOG.isDebugEnabled()) {
LOG.debug("Intercepted sending to {} -> {}", URISupport.sanitizeUri(destination.getEndpointUri()), URISupport.sanitizeUri(lookup.getEndpointUri()));
}
destination = lookup;
}
// warm up the producer by starting it so we can fail fast if there was a problem
// however must start endpoint first
ServiceHelper.startService(destination);
// this SendProcessor is used a lot in Camel (eg every .to in the route DSL) and therefore we
// want to optimize for regular producers, by using the producer directly instead of the ProducerCache
// Only for pooled and non singleton producers we have to use the ProducerCache as it supports these
// kind of producer better (though these kind of producer should be rare)
Producer producer = producerCache.acquireProducer(destination);
if (producer instanceof ServicePoolAware || !producer.isSingleton()) {
// no we cannot optimize it - so release the producer back to the producer cache
// and use the producer cache for sending
producerCache.releaseProducer(destination, producer);
} else {
// yes we can optimize and use the producer directly for sending
this.producer = AsyncProcessorConverterHelper.convert(producer);
}
}
use of org.apache.camel.Producer in project camel by apache.
the class BuilderWithScopesTest method runTest.
protected void runTest(RouteBuilder builder, List<String> expected, String header) throws Exception {
order.clear();
CamelContext container = new DefaultCamelContext();
container.disableJMX();
container.addRoutes(builder);
container.start();
Endpoint endpoint = container.getEndpoint("direct:a");
Exchange exchange = endpoint.createExchange();
if (header != null) {
exchange.getIn().setHeader("foo", header);
}
Producer producer = endpoint.createProducer();
producer.process(exchange);
log.debug("Invocation order:" + order);
assertEquals(expected, order);
container.stop();
}
use of org.apache.camel.Producer in project camel by apache.
the class RouteBuilderTest method assertSendToProcessor.
protected void assertSendToProcessor(Processor processor, String uri) {
if (!(processor instanceof Producer)) {
processor = unwrapErrorHandler(processor);
}
if (processor instanceof SendProcessor) {
assertSendTo(processor, uri);
} else {
Producer producer = assertIsInstanceOf(Producer.class, processor);
assertEquals("Endpoint URI", uri, producer.getEndpoint().getEndpointUri());
}
}
use of org.apache.camel.Producer in project camel by apache.
the class DefaultExchangeFormatterTest method testSendExchangeWithExceptionAndStackTrace.
public void testSendExchangeWithExceptionAndStackTrace() throws Exception {
Endpoint endpoint = resolveMandatoryEndpoint("log:org.apache.camel.TEST?showException=true&showStackTrace=true");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setBody("Hello World");
exchange.setException(new IllegalArgumentException("Damn"));
Producer producer = endpoint.createProducer();
producer.start();
producer.process(exchange);
producer.stop();
}
use of org.apache.camel.Producer in project camel by apache.
the class SedaRouteTest method testThatShowsEndpointResolutionIsNotConsistent.
public void testThatShowsEndpointResolutionIsNotConsistent() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
CamelContext context = new DefaultCamelContext();
// lets add some routes
context.addRoutes(new RouteBuilder() {
public void configure() {
from("seda:test.a").to("seda:test.b");
from("seda:test.b").process(new Processor() {
public void process(Exchange e) {
log.debug("Received exchange: " + e.getIn());
latch.countDown();
}
});
}
});
context.start();
// now lets fire in a message
Endpoint endpoint = context.getEndpoint("seda:test.a");
Exchange exchange = endpoint.createExchange();
exchange.getIn().setHeader("cheese", 123);
Producer producer = endpoint.createProducer();
producer.process(exchange);
// now lets sleep for a while
assertTrue(latch.await(5, TimeUnit.SECONDS));
context.stop();
}
Aggregations