Search in sources :

Example 6 with SpringRouteBuilder

use of org.apache.camel.spring.SpringRouteBuilder in project camel by apache.

the class TransactionalClientDataSourceMDCTest method createRouteBuilder.

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

        public void configure() throws Exception {
            context.setUseMDCLogging(true);
            from("direct:okay").routeId("route-a").transacted().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("route-a", MDC.get("camel.routeId"));
                    assertEquals(exchange.getExchangeId(), MDC.get("camel.exchangeId"));
                    assertNotNull(MDC.get("camel.transactionKey"));
                }
            }).to("log:foo").setBody(constant("Tiger in Action")).bean("bookService").to("log:bar").setBody(constant("Elephant in Action")).bean("bookService");
            // marks this route as transacted that will use the single policy defined in the registry
            from("direct:fail").routeId("route-b").transacted().process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    assertEquals("route-b", MDC.get("camel.routeId"));
                    assertEquals(exchange.getExchangeId(), MDC.get("camel.exchangeId"));
                    assertNotNull(MDC.get("camel.transactionKey"));
                }
            }).to("log:foo2").setBody(constant("Tiger in Action")).bean("bookService").to("log:bar2").setBody(constant("Donkey in Action")).bean("bookService");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder)

Example 7 with SpringRouteBuilder

use of org.apache.camel.spring.SpringRouteBuilder in project camel by apache.

the class TransactionalClientDataSourceTest method createRouteBuilder.

// END SNIPPET: e4
protected RouteBuilder createRouteBuilder() throws Exception {
    // the standard RouteBuilder
    return new SpringRouteBuilder() {

        public void configure() throws Exception {
            // START SNIPPET: e1
            // lookup the transaction policy
            SpringTransactionPolicy required = lookup("PROPAGATION_REQUIRED", SpringTransactionPolicy.class);
            // Notice: transactionErrorHandler is in SpringRouteBuilder
            if (isUseTransactionErrorHandler()) {
                // useTransactionErrorHandler is only used for unit testing to reuse code
                // for doing a 2nd test without this transaction error handler, so ignore
                // this. For spring based transaction, end users are encouraged to use the
                // transaction error handler instead of the default DeadLetterChannel.
                errorHandler(transactionErrorHandler(required));
            }
            // END SNIPPET: e1
            // START SNIPPET: e2
            // set the required policy for this route
            from("direct:okay").policy(required).setBody(constant("Tiger in Action")).bean("bookService").setBody(constant("Elephant in Action")).bean("bookService");
            // set the required policy for this route
            from("direct:fail").policy(required).setBody(constant("Tiger in Action")).bean("bookService").setBody(constant("Donkey in Action")).bean("bookService");
        // END SNIPPET: e2
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy)

Example 8 with SpringRouteBuilder

use of org.apache.camel.spring.SpringRouteBuilder in project camel by apache.

the class TransactionalClientWithRollbackTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() throws Exception {
    // the standard RouteBuilder
    return new SpringRouteBuilder() {

        public void configure() throws Exception {
            // setup the transaction policy
            SpringTransactionPolicy required = lookup("PROPAGATION_REQUIRED", SpringTransactionPolicy.class);
            // use transaction error handler
            errorHandler(transactionErrorHandler(required));
            // must setup policy for each route
            from("direct:okay").policy(required).setBody(constant("Tiger in Action")).bean("bookService").setBody(constant("Elephant in Action")).bean("bookService");
            // must setup policy for each route
            from("direct:fail").policy(required).setBody(constant("Tiger in Action")).bean("bookService").rollback();
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy)

Example 9 with SpringRouteBuilder

use of org.apache.camel.spring.SpringRouteBuilder in project camel by apache.

the class TransactionalClientDataSourceAsyncTest method createRouteBuilder.

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

        public void configure() throws Exception {
            context.addComponent("async", new MyAsyncComponent());
            // use required as transaction policy
            SpringTransactionPolicy required = lookup("PROPAGATION_REQUIRED", SpringTransactionPolicy.class);
            // configure to use transaction error handler and pass on the required as it will fetch
            // the transaction manager from it that it needs
            errorHandler(transactionErrorHandler(required));
            // on exception is also supported
            onException(IllegalArgumentException.class).handled(false).to("mock:error");
            from("direct:okay").policy(required).setBody(constant("Tiger in Action")).bean("bookService").log("Before thread ${threadName}").to("async:bye:camel").log("After thread ${threadName}").setBody(constant("Elephant in Action")).bean("bookService");
            from("direct:fail").policy(required).setBody(constant("Tiger in Action")).bean("bookService").log("Before thread ${threadName}").to("async:bye:camel").log("After thread ${threadName}").setBody(constant("Donkey in Action")).bean("bookService");
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy) MyAsyncComponent(org.apache.camel.processor.async.MyAsyncComponent)

Example 10 with SpringRouteBuilder

use of org.apache.camel.spring.SpringRouteBuilder in project camel by apache.

the class TransactionalClientDataSourceWithOnExceptionHandledAndRollbackTest method createRouteBuilder.

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

        public void configure() throws Exception {
            // use required as transaction policy
            SpringTransactionPolicy required = lookup("PROPAGATION_REQUIRED", SpringTransactionPolicy.class);
            // configure to use transaction error handler and pass on the required as it will fetch
            // the transaction manager from it that it needs
            errorHandler(transactionErrorHandler(required));
            onException(IllegalArgumentException.class).handled(true).to("mock:error").rollback();
            from("direct:okay").policy(required).setBody(constant("Tiger in Action")).bean("bookService").setBody(constant("Elephant in Action")).bean("bookService");
            from("direct:fail").policy(required).setBody(constant("Tiger in Action")).bean("bookService").setBody(constant("Donkey in Action")).bean("bookService");
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy)

Aggregations

SpringRouteBuilder (org.apache.camel.spring.SpringRouteBuilder)16 SpringTransactionPolicy (org.apache.camel.spring.spi.SpringTransactionPolicy)10 Test (org.junit.Test)6 Exchange (org.apache.camel.Exchange)4 Processor (org.apache.camel.Processor)3 Policy (org.apache.camel.spi.Policy)3 HashSet (java.util.HashSet)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 Query (javax.persistence.Query)2 MessageProcessed (org.apache.camel.processor.idempotent.jpa.MessageProcessed)2 Message (org.apache.camel.Message)1 NotifyBuilder (org.apache.camel.builder.NotifyBuilder)1 JpaEndpoint (org.apache.camel.component.jpa.JpaEndpoint)1 Customer (org.apache.camel.examples.Customer)1 AggregationStrategy (org.apache.camel.processor.aggregate.AggregationStrategy)1 MyAsyncComponent (org.apache.camel.processor.async.MyAsyncComponent)1