Search in sources :

Example 61 with Producer

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);
    }
}
Also used : InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Endpoint(org.apache.camel.Endpoint) InterceptSendToEndpoint(org.apache.camel.impl.InterceptSendToEndpoint) Producer(org.apache.camel.Producer) HashMap(java.util.HashMap) ServicePoolAware(org.apache.camel.ServicePoolAware) ProducerCache(org.apache.camel.impl.ProducerCache)

Example 62 with 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();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Example 63 with Producer

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());
    }
}
Also used : Producer(org.apache.camel.Producer) SendProcessor(org.apache.camel.processor.SendProcessor)

Example 64 with Producer

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();
}
Also used : Exchange(org.apache.camel.Exchange) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer)

Example 65 with Producer

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();
}
Also used : CamelContext(org.apache.camel.CamelContext) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext) Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) Endpoint(org.apache.camel.Endpoint) Producer(org.apache.camel.Producer) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultCamelContext(org.apache.camel.impl.DefaultCamelContext)

Aggregations

Producer (org.apache.camel.Producer)198 Endpoint (org.apache.camel.Endpoint)140 Exchange (org.apache.camel.Exchange)138 Test (org.junit.Test)72 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)69 Processor (org.apache.camel.Processor)34 RouteBuilder (org.apache.camel.builder.RouteBuilder)23 Message (org.apache.camel.Message)21 CountDownLatch (java.util.concurrent.CountDownLatch)16 File (java.io.File)12 CamelContext (org.apache.camel.CamelContext)12 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)10 DefaultExchange (org.apache.camel.impl.DefaultExchange)9 Mockito.anyLong (org.mockito.Mockito.anyLong)9 Consumer (org.apache.camel.Consumer)8 FileDataSource (javax.activation.FileDataSource)7 AsyncProcessor (org.apache.camel.AsyncProcessor)7 DataHandler (javax.activation.DataHandler)6 Field (java.lang.reflect.Field)5 ExchangePattern (org.apache.camel.ExchangePattern)5