Search in sources :

Example 11 with CamelExchangeException

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

the class ExceptionBuilderTest method createRouteBuilder.

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

        @SuppressWarnings("unchecked")
        public void configure() throws Exception {
            errorHandler(deadLetterChannel("mock:error").redeliveryDelay(0).maximumRedeliveries(3));
            // START SNIPPET: exceptionBuilder1
            onException(NullPointerException.class).maximumRedeliveries(0).setHeader(MESSAGE_INFO, constant("Damm a NPE")).to(ERROR_QUEUE);
            onException(IOException.class).redeliveryDelay(100L).maximumRedeliveries(3).maximumRedeliveryDelay(30 * 1000L).backOffMultiplier(1.0).useExponentialBackOff().setHeader(MESSAGE_INFO, constant("Damm somekind of IO exception")).to(ERROR_QUEUE);
            onException(Exception.class).redeliveryDelay(100L).maximumRedeliveries(2).setHeader(MESSAGE_INFO, constant("Damm just exception")).to(ERROR_QUEUE);
            onException(MyBaseBusinessException.class).redeliveryDelay(100L).maximumRedeliveries(3).setHeader(MESSAGE_INFO, constant("Damm my business is not going to well")).to(BUSINESS_ERROR_QUEUE);
            onException(GeneralSecurityException.class, KeyException.class).maximumRedeliveries(1).setHeader(MESSAGE_INFO, constant("Damm some security error")).to(SECURITY_ERROR_QUEUE);
            onException(InstantiationException.class, IllegalAccessException.class, ClassNotFoundException.class).maximumRedeliveries(0).setHeader(MESSAGE_INFO, constant("Damm some access error")).to(ERROR_QUEUE);
            // END SNIPPET: exceptionBuilder1
            from("direct:a").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String s = exchange.getIn().getBody(String.class);
                    if ("Hello NPE".equals(s)) {
                        throw new NullPointerException();
                    } else if ("Hello IO".equals(s)) {
                        throw new ConnectException("Forced for testing - cannot connect to remote server");
                    } else if ("Hello Exception".equals(s)) {
                        throw new CamelExchangeException("Forced for testing", exchange);
                    } else if ("Hello business".equals(s)) {
                        throw new MyBusinessException();
                    } else if ("I am not allowed to do this".equals(s)) {
                        throw new KeyManagementException();
                    } else if ("I am not allowed to access this".equals(s)) {
                        throw new IllegalAccessException();
                    }
                    exchange.getOut().setBody("Hello World");
                }
            }).to("mock:result");
        }
    };
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) Processor(org.apache.camel.Processor) GeneralSecurityException(java.security.GeneralSecurityException) IOException(java.io.IOException) KeyException(java.security.KeyException) KeyManagementException(java.security.KeyManagementException) Exchange(org.apache.camel.Exchange) ConnectException(java.net.ConnectException)

Example 12 with CamelExchangeException

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

the class SplitterParallelStopOnExceptionTest method testSplitParallelStopOnExceptionStop.

public void testSplitParallelStopOnExceptionStop() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:split");
    mock.expectedMinimumMessageCount(0);
    mock.allMessages().body().isNotEqualTo("Kaboom");
    try {
        template.sendBody("direct:start", "Hello World,Goodday World,Kaboom,Bye World");
        fail("Should thrown an exception");
    } catch (CamelExecutionException e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        assertTrue(cause.getMessage().startsWith("Parallel processing failed for number "));
        assertEquals("Forced", cause.getCause().getMessage());
        String body = cause.getExchange().getIn().getBody(String.class);
        assertTrue(body.contains("Kaboom"));
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 13 with CamelExchangeException

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

the class SplitterStopOnExceptionTest method testSplitStopOnExceptionStop.

public void testSplitStopOnExceptionStop() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:split");
    // we do stop so we stop splitting when the exception occurs and thus we only receive 1 message
    mock.expectedBodiesReceived("Hello World");
    try {
        template.sendBody("direct:start", "Hello World,Kaboom,Bye World");
        fail("Should thrown an exception");
    } catch (CamelExecutionException e) {
        CamelExchangeException cause = assertIsInstanceOf(CamelExchangeException.class, e.getCause());
        assertTrue(cause.getMessage().startsWith("Sequential processing failed for number 1."));
        assertEquals("Forced", cause.getCause().getMessage());
    }
    assertMockEndpointsSatisfied();
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 14 with CamelExchangeException

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

the class EnricherAsyncUnhandledExceptionTest method testInOutWithRequestBody.

@Test
public void testInOutWithRequestBody() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:pickedUp");
    mock.expectedMessageCount(1);
    // this direct endpoint should receive an exception
    try {
        Future<Object> obj = template.asyncRequestBody("direct:in", "Hello World");
        // wait five seconds at most; else, let's assume something went wrong
        obj.get(5000, TimeUnit.MILLISECONDS);
    } catch (Exception e) {
        // if we receive an exception, the async routing engine is working correctly
        // before the Enricher was fixed for cases where routing was async and the AggregationStrategy 
        // threw an exception, the call to requestBody would stall indefinitely
        // unwrap the exception chain
        assertTrue(e instanceof ExecutionException);
        assertTrue(e.getCause() instanceof CamelExecutionException);
        assertTrue(e.getCause().getCause() instanceof CamelExchangeException);
        assertTrue(e.getCause().getCause().getCause() instanceof RuntimeException);
        assertTrue(e.getCause().getCause().getCause().getMessage().equals("Bang! Unhandled exception"));
        mock.assertIsSatisfied();
        return;
    }
    fail("Expected an RuntimeException");
}
Also used : CamelExecutionException(org.apache.camel.CamelExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) CamelExecutionException(org.apache.camel.CamelExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CamelExecutionException(org.apache.camel.CamelExecutionException) ExecutionException(java.util.concurrent.ExecutionException) CamelExchangeException(org.apache.camel.CamelExchangeException) Test(org.junit.Test)

Example 15 with CamelExchangeException

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

the class DefaultExceptionPolicyStrategyTest method testDirectMatch1.

public void testDirectMatch1() {
    setupPolicies();
    OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new CamelExchangeException("", null));
    assertEquals(type1, result);
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition)

Aggregations

CamelExchangeException (org.apache.camel.CamelExchangeException)82 IApplication (com.openshift.client.IApplication)23 Exchange (org.apache.camel.Exchange)17 IOException (java.io.IOException)10 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)8 IEnvironmentVariable (com.openshift.client.IEnvironmentVariable)5 InputStream (java.io.InputStream)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 AsyncCallback (org.apache.camel.AsyncCallback)5 CamelExecutionException (org.apache.camel.CamelExecutionException)5 Message (org.apache.camel.Message)5 File (java.io.File)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 Processor (org.apache.camel.Processor)4 RouteBuilder (org.apache.camel.builder.RouteBuilder)4 IEmbeddedCartridge (com.openshift.client.cartridge.IEmbeddedCartridge)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Serializable (java.io.Serializable)3 URI (java.net.URI)3 List (java.util.List)3