Search in sources :

Example 1 with ConsumerTemplate

use of org.apache.camel.ConsumerTemplate in project camel by apache.

the class FileConsumePollEnrichFileUsingProcessorTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() throws Exception {
    return new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            from("file://target/enrich?move=.done").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String name = exchange.getIn().getHeader(Exchange.FILE_NAME_ONLY, String.class);
                    name = FileUtil.stripExt(name) + ".dat";
                    // use a consumer template to get the data file
                    Exchange data = null;
                    ConsumerTemplate con = exchange.getContext().createConsumerTemplate();
                    try {
                        // try to get the data file
                        data = con.receive("file://target/enrichdata?move=.done&fileName=" + name, 5000);
                    } finally {
                        // stop the consumer as it does not need to poll for files anymore
                        con.stop();
                    }
                    // if we found the data file then process it by sending it to the direct:data endpoint
                    if (data != null) {
                        template.send("direct:data", data);
                    } else {
                        // otherwise do a rollback
                        throw new CamelExchangeException("Cannot find the data file " + name, exchange);
                    }
                }
            }).to("mock:start");
            from("direct:data").to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) ConsumerTemplate(org.apache.camel.ConsumerTemplate) CamelExchangeException(org.apache.camel.CamelExchangeException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Example 2 with ConsumerTemplate

use of org.apache.camel.ConsumerTemplate in project camel by apache.

the class DefaultConsumerTemplateTest method testCacheConsumers.

public void testCacheConsumers() throws Exception {
    ConsumerTemplate template = new DefaultConsumerTemplate(context);
    template.setMaximumCacheSize(500);
    template.start();
    assertEquals("Size should be 0", 0, template.getCurrentCacheSize());
    // test that we cache at most 500 consumers to avoid it eating to much memory
    for (int i = 0; i < 503; i++) {
        Endpoint e = context.getEndpoint("direct:queue:" + i);
        template.receiveNoWait(e);
    }
    // the eviction is async so force cleanup
    template.cleanUp();
    assertEquals("Size should be 500", 500, template.getCurrentCacheSize());
    template.stop();
    // should be 0
    assertEquals("Size should be 0", 0, template.getCurrentCacheSize());
}
Also used : ConsumerTemplate(org.apache.camel.ConsumerTemplate) Endpoint(org.apache.camel.Endpoint) Endpoint(org.apache.camel.Endpoint)

Example 3 with ConsumerTemplate

use of org.apache.camel.ConsumerTemplate in project camel by apache.

the class DefaultConsumerTemplateTest method testCacheConsumersFromContext.

public void testCacheConsumersFromContext() throws Exception {
    ConsumerTemplate template = context.createConsumerTemplate(500);
    assertEquals("Size should be 0", 0, template.getCurrentCacheSize());
    // test that we cache at most 500 consumers to avoid it eating to much memory
    for (int i = 0; i < 503; i++) {
        Endpoint e = context.getEndpoint("direct:queue:" + i);
        template.receiveNoWait(e);
    }
    // the eviction is async so force cleanup
    template.cleanUp();
    assertEquals("Size should be 500", 500, template.getCurrentCacheSize());
    template.stop();
    // should be 0
    assertEquals("Size should be 0", 0, template.getCurrentCacheSize());
}
Also used : ConsumerTemplate(org.apache.camel.ConsumerTemplate) Endpoint(org.apache.camel.Endpoint) Endpoint(org.apache.camel.Endpoint)

Example 4 with ConsumerTemplate

use of org.apache.camel.ConsumerTemplate in project camel by apache.

the class CamelAutoConfiguration method consumerTemplate.

/**
     * Default consumer template for the bootstrapped Camel context.
     */
@Bean(initMethod = "", destroyMethod = "")
// Camel handles the lifecycle of this bean
@ConditionalOnMissingBean(ConsumerTemplate.class)
ConsumerTemplate consumerTemplate(CamelContext camelContext, CamelConfigurationProperties config) throws Exception {
    final ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate(config.getConsumerTemplateCacheSize());
    camelContext.addService(consumerTemplate);
    return consumerTemplate;
}
Also used : ConsumerTemplate(org.apache.camel.ConsumerTemplate) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 5 with ConsumerTemplate

use of org.apache.camel.ConsumerTemplate in project camel by apache.

the class ConsumerTemplateAlreadyExistTest method testHasExistingTemplate.

@Test
public void testHasExistingTemplate() {
    assertNotNull("Should have injected a consumer template", template);
    ConsumerTemplate lookup = context.getRegistry().lookupByNameAndType("myConsumerTemplate", ConsumerTemplate.class);
    assertNotNull("Should lookup consumer template", lookup);
    ConsumerTemplate lookup2 = context.getRegistry().lookupByNameAndType("consumerTemplate", ConsumerTemplate.class);
    assertNull("Should not be able to lookup consumer template", lookup2);
}
Also used : ConsumerTemplate(org.apache.camel.ConsumerTemplate) Test(org.junit.Test)

Aggregations

ConsumerTemplate (org.apache.camel.ConsumerTemplate)17 Test (org.junit.Test)11 Endpoint (org.apache.camel.Endpoint)4 ProducerTemplate (org.apache.camel.ProducerTemplate)3 Exchange (org.apache.camel.Exchange)2 CamelContext (org.apache.camel.CamelContext)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 Processor (org.apache.camel.Processor)1 RouteBuilder (org.apache.camel.builder.RouteBuilder)1 DefaultCamelContext (org.apache.camel.impl.DefaultCamelContext)1 DefaultConsumerTemplate (org.apache.camel.impl.DefaultConsumerTemplate)1 Before (org.junit.Before)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)1 Bean (org.springframework.context.annotation.Bean)1