Search in sources :

Example 76 with Processor

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

the class ChoiceProcessor method process.

public boolean process(final Exchange exchange, final AsyncCallback callback) {
    Iterator<Processor> processors = next().iterator();
    // callback to restore existing FILTER_MATCHED property on the Exchange
    final Object existing = exchange.getProperty(Exchange.FILTER_MATCHED);
    final AsyncCallback choiceCallback = new AsyncCallback() {

        @Override
        public void done(boolean doneSync) {
            if (existing != null) {
                exchange.setProperty(Exchange.FILTER_MATCHED, existing);
            } else {
                exchange.removeProperty(Exchange.FILTER_MATCHED);
            }
            callback.done(doneSync);
        }
    };
    // and if not, we just continue without using any processor
    while (processors.hasNext()) {
        // get the next processor
        Processor processor = processors.next();
        // evaluate the predicate on filter predicate early to be faster
        // and avoid issues when having nested choices
        // as we should only pick one processor
        boolean matches = false;
        if (processor instanceof FilterProcessor) {
            FilterProcessor filter = (FilterProcessor) processor;
            try {
                matches = filter.matches(exchange);
                // as we have pre evaluated the predicate then use its processor directly when routing
                processor = filter.getProcessor();
            } catch (Throwable e) {
                exchange.setException(e);
            }
        } else {
            // its the otherwise processor, so its a match
            notFiltered++;
            matches = true;
        }
        // check for error if so we should break out
        if (!continueProcessing(exchange, "so breaking out of choice", LOG)) {
            break;
        }
        // if we did not match then continue to next filter
        if (!matches) {
            continue;
        }
        // okay we found a filter or its the otherwise we are processing
        AsyncProcessor async = AsyncProcessorConverterHelper.convert(processor);
        return async.process(exchange, choiceCallback);
    }
    // when no filter matches and there is no otherwise, then just continue
    choiceCallback.done(true);
    return true;
}
Also used : Processor(org.apache.camel.Processor) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncProcessor(org.apache.camel.AsyncProcessor) AsyncCallback(org.apache.camel.AsyncCallback)

Example 77 with Processor

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

the class BeanMethodWithExchangeTest method testBeanWithAnnotationAndExchangeTest.

public void testBeanWithAnnotationAndExchangeTest() throws Exception {
    Exchange result = template.request("direct:start1", new Processor() {

        public void process(Exchange exchange) throws Exception {
            Message m = exchange.getIn();
            m.addAttachment("attachment", new DataHandler(new FileDataSource("src/test/org/apache/camel/component/bean/BeanWithAttachmentAnnotationTest.java")));
        }
    });
    assertTrue(result.getOut().getAttachmentObjects().containsKey("attachment2"));
    assertTrue(result.getOut().getAttachments().containsKey("attachment1"));
    assertEquals("attachmentValue1", result.getOut().getAttachmentObjects().get("attachment1").getHeader("attachmentHeader1"));
    assertFalse(result.getOut().getAttachments().containsKey("attachment"));
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) FileDataSource(javax.activation.FileDataSource) DataHandler(javax.activation.DataHandler)

Example 78 with Processor

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

the class BeanInfoSelectMethodTest method testFailure.

public void testFailure() throws Exception {
    getMockEndpoint("mock:result").expectedBodiesReceived("Failure");
    template.send("direct:b", new Processor() {

        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setBody("Hello");
            exchange.setException(new IllegalArgumentException("Forced by unit test"));
        }
    });
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) ExchangeException(org.apache.camel.ExchangeException)

Example 79 with Processor

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

the class DirectoryCreateIssueTest method testFileCreatedAsDir.

public void testFileCreatedAsDir() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:result");
    mock.expectedMessageCount(numFiles);
    template.send("seda:testFileCreatedAsDir", new Processor() {

        public void process(Exchange exchange) {
            Message in = exchange.getIn();
            in.setBody("Contents of test file");
        }
    });
    assertMockEndpointsSatisfied();
    // wait a little while for the files to settle down
    Thread.sleep(200);
    for (int i = 0; i < numFiles; i++) {
        assertTrue((new File(path + "/file" + i + ".txt")).isFile());
    }
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Message(org.apache.camel.Message) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 80 with Processor

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

the class FileConsumerAutoCreateDirectoryTest method testDoNotCreateDirectory.

public void testDoNotCreateDirectory() throws Exception {
    deleteDirectory("target/file/foo");
    Endpoint endpoint = context.getEndpoint("file://target/file/foo?autoCreate=false");
    Consumer consumer = endpoint.createConsumer(new Processor() {

        public void process(Exchange exchange) throws Exception {
        // noop
        }
    });
    consumer.start();
    consumer.stop();
    // the directory should NOT exists
    File dir = new File("target/file/foo");
    assertFalse("Directory should NOT be created", dir.exists());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) Endpoint(org.apache.camel.Endpoint) Consumer(org.apache.camel.Consumer) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException)

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