Search in sources :

Example 91 with Processor

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

the class ChangeHeaderCaseIssueTest method createRouteBuilder.

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

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:result").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // change the case of the header
                    Object value = exchange.getIn().removeHeader("SOAPAction");
                    exchange.getIn().setHeader("SoapAction", value);
                }
            });
            from("direct:start").to("mock:result");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder)

Example 92 with Processor

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

the class ExceptionThrownFromOnExceptionTest method testNoExceptionThrownFromOnExceptionAndHandledWithDeadLetterChannel.

public void testNoExceptionThrownFromOnExceptionAndHandledWithDeadLetterChannel() throws Exception {
    RETRY.set(0);
    ON_EXCEPTION_RETRY.set(0);
    context.addRoutes(new RouteBuilder() {

        @Override
        public void configure() throws Exception {
            // DLC
            deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3);
            // on exception to catch all IO exceptions and handle them specially
            onException(IOException.class).maximumRedeliveries(3).handled(true).to("mock:b").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    ON_EXCEPTION_RETRY.incrementAndGet();
                // no exception is thrown this time
                }
            }).to("mock:c");
            from("direct:start").to("direct:intermediate").to("mock:result");
            from("direct:intermediate").to("mock:a").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    RETRY.incrementAndGet();
                    throw new IOException("IO error");
                }
            }).to("mock:end");
        }
    });
    context.start();
    getMockEndpoint("mock:a").expectedMessageCount(1);
    getMockEndpoint("mock:b").expectedMessageCount(1);
    getMockEndpoint("mock:c").expectedMessageCount(1);
    getMockEndpoint("mock:result").expectedMessageCount(0);
    getMockEndpoint("mock:end").expectedMessageCount(0);
    // the exception is handled by onException so it goes not in DLC
    getMockEndpoint("mock:error").expectedMessageCount(0);
    // and this time there was no exception thrown from onException,
    // and the exception is handled so the caller should not fail
    template.sendBody("direct:start", "Hello World");
    assertMockEndpointsSatisfied();
    assertEquals("Should try 4 times (1 first, 3 retry)", 4, RETRY.get());
    assertEquals("Should only invoke onException once", 1, ON_EXCEPTION_RETRY.get());
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) IOException(java.io.IOException) CamelExecutionException(org.apache.camel.CamelExecutionException) IOException(java.io.IOException)

Example 93 with Processor

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

the class AdviceWithTwoRoutesOnExceptionIssueTest method testAdviceWith.

public void testAdviceWith() throws Exception {
    context.getRouteDefinition("a").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:a").skipSendToOriginalEndpoint().to("mock:error");
        }
    });
    context.getRouteDefinition("b").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:b").skipSendToOriginalEndpoint().to("mock:error");
        }
    });
    getMockEndpoint("mock:error").whenAnyExchangeReceived(new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            String body = exchange.getIn().getBody(String.class);
            throw new IllegalArgumentException("Forced " + body);
        }
    });
    getMockEndpoint("mock:a").expectedMessageCount(0);
    getMockEndpoint("mock:b").expectedMessageCount(0);
    // whenAnyExchange is invoked after the mock receive the exchange
    getMockEndpoint("mock:error").expectedBodiesReceived("A", "B");
    // the onException should handle and send the message to this mock
    getMockEndpoint("mock:handled").expectedBodiesReceived("Handling Forced A", "Handling Forced B");
    Object outA = template.requestBody("direct:a", "A");
    assertEquals("Handling Forced A", outA);
    Object outB = template.requestBody("direct:b", "B");
    assertEquals("Handling Forced B", outB);
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 94 with Processor

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

the class AdviceWithOnExceptionMultipleIssueTest method testMultipleAdvice.

public void testMultipleAdvice() throws Exception {
    context.addRoutes(createRouteBuilder());
    context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:resultA").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                    throw new Exception("my exception");
                }
            });
        }
    });
    context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
        }
    });
    context.start();
    getMockEndpoint("mock:resultA").expectedMessageCount(0);
    template.sendBody("direct:startA", "a trigger");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

Example 95 with Processor

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

the class AdviceWithOnExceptionMultipleIssueTest method testSimpleMultipleAdvice.

public void testSimpleMultipleAdvice() throws Exception {
    context.addRoutes(createRouteBuilder());
    context.getRouteDefinition("RouteA").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
            interceptSendToEndpoint("mock:resultA").process(new Processor() {

                @Override
                public void process(Exchange exchange) throws Exception {
                }
            });
        }
    });
    context.getRouteDefinition("RouteB").adviceWith(context, new AdviceWithRouteBuilder() {

        @Override
        public void configure() throws Exception {
        }
    });
    context.start();
    getMockEndpoint("mock:resultA").expectedMessageCount(1);
    template.sendBody("direct:startA", "a trigger");
    assertMockEndpointsSatisfied();
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) AdviceWithRouteBuilder(org.apache.camel.builder.AdviceWithRouteBuilder)

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