Search in sources :

Example 81 with Processor

use of org.apache.camel.Processor 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 82 with Processor

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

the class TwoSchedulerConcurrentTasksOneRouteTest method createRouteBuilder.

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

        public void configure() {
            // number of concurrent task a thread pool should have
            SchedulerComponent comp = context.getComponent("scheduler", SchedulerComponent.class);
            comp.setConcurrentTasks(2);
            // let this route scheduler use all 2 concurrent tasks at the same time
            from("scheduler://foo?delay=250&scheduler.concurrentTasks=2").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    if (sleep.compareAndSet(true, false)) {
                        log.info("Thread is sleeping");
                        Thread.sleep(1000);
                        log.info("Thread is done sleeping");
                    }
                }
            }).to("log:done").to("mock:done");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Example 83 with Processor

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

the class RefComponentTest method bindToRegistry.

private void bindToRegistry(JndiRegistry jndi) throws Exception {
    Component comp = new DirectComponent();
    comp.setCamelContext(context);
    Endpoint slow = comp.createEndpoint("direct:somename");
    Consumer consumer = slow.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
            template.send("mock:result", exchange);
        }
    });
    consumer.start();
    // bind our endpoint to the registry for ref to lookup
    jndi.bind("foo", slow);
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) Consumer(org.apache.camel.Consumer) DirectComponent(org.apache.camel.component.direct.DirectComponent) Component(org.apache.camel.Component) DirectComponent(org.apache.camel.component.direct.DirectComponent)

Example 84 with Processor

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

the class SedaEndpointTest method testSedaEndpoint.

public void testSedaEndpoint() throws Exception {
    SedaEndpoint seda = new SedaEndpoint("seda://foo", context.getComponent("seda"), queue);
    assertNotNull(seda);
    assertEquals(1000, seda.getSize());
    assertSame(queue, seda.getQueue());
    assertEquals(1, seda.getConcurrentConsumers());
    Producer prod = seda.createProducer();
    seda.onStarted((SedaProducer) prod);
    assertEquals(1, seda.getProducers().size());
    Consumer cons = seda.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // do nothing
        }
    });
    seda.onStarted((SedaConsumer) cons);
    assertEquals(1, seda.getConsumers().size());
    assertEquals(0, seda.getExchanges().size());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) Consumer(org.apache.camel.Consumer)

Example 85 with Processor

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

the class SedaEndpointTest method testSedaEndpointUnboundedQueue.

public void testSedaEndpointUnboundedQueue() throws Exception {
    BlockingQueue<Exchange> unbounded = new LinkedBlockingQueue<Exchange>();
    SedaEndpoint seda = new SedaEndpoint("seda://foo", context.getComponent("seda"), unbounded);
    assertNotNull(seda);
    assertEquals(Integer.MAX_VALUE, seda.getSize());
    assertSame(unbounded, seda.getQueue());
    assertEquals(1, seda.getConcurrentConsumers());
    Producer prod = seda.createProducer();
    seda.onStarted((SedaProducer) prod);
    assertEquals(1, seda.getProducers().size());
    Consumer cons = seda.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // do nothing
        }
    });
    seda.onStarted((SedaConsumer) cons);
    assertEquals(1, seda.getConsumers().size());
    assertEquals(0, seda.getExchanges().size());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Producer(org.apache.camel.Producer) Consumer(org.apache.camel.Consumer) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue)

Aggregations

Processor (org.apache.camel.Processor)1467 Exchange (org.apache.camel.Exchange)1368 Test (org.junit.Test)634 RouteBuilder (org.apache.camel.builder.RouteBuilder)543 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)164 Message (org.apache.camel.Message)119 ArrayList (java.util.ArrayList)65 HashMap (java.util.HashMap)64 IOException (java.io.IOException)55 CamelExecutionException (org.apache.camel.CamelExecutionException)52 Endpoint (org.apache.camel.Endpoint)46 Map (java.util.Map)45 File (java.io.File)38 List (java.util.List)34 Producer (org.apache.camel.Producer)33 DefaultExchange (org.apache.camel.impl.DefaultExchange)29 SendProcessor (org.apache.camel.processor.SendProcessor)26 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)26 CountDownLatch (java.util.concurrent.CountDownLatch)24 Expression (org.apache.camel.Expression)24