Search in sources :

Example 11 with SpringRouteBuilder

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

the class TransactionalClientDataSourceWithOnExceptionRollbackTest 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));
            // on exception is also supported
            onException(IllegalArgumentException.class).handled(false).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)

Example 12 with SpringRouteBuilder

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

the class TransactionalClientDataSourceWithOnExceptionTest 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));
            // 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").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)

Example 13 with SpringRouteBuilder

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

the class JpaRouteEndpointTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new SpringRouteBuilder() {

        public void configure() throws Exception {
            JpaEndpoint jpa = new JpaEndpoint();
            jpa.setCamelContext(context);
            jpa.setEntityType(SendEmail.class);
            jpa.setEntityManagerFactory(context.getRegistry().lookupByNameAndType("entityManagerFactory", EntityManagerFactory.class));
            from("direct:start").to(jpa).to("mock:result");
        }
    };
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) EntityManagerFactory(javax.persistence.EntityManagerFactory) JpaEndpoint(org.apache.camel.component.jpa.JpaEndpoint)

Example 14 with SpringRouteBuilder

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

the class JpaIdempotentConsumerTest method testDuplicateMessagesAreFilteredOut.

@SuppressWarnings("unchecked")
@Test
public void testDuplicateMessagesAreFilteredOut() throws Exception {
    context.addRoutes(new SpringRouteBuilder() {

        @Override
        public void configure() throws Exception {
            // START SNIPPET: idempotent
            from("direct:start").idempotentConsumer(header("messageId"), jpaMessageIdRepository(lookup(EntityManagerFactory.class), PROCESSOR_NAME)).to("mock:result");
        // END SNIPPET: idempotent
        }
    });
    context.start();
    resultEndpoint.expectedBodiesReceived("one", "two", "three");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
    // all 3 messages should be in jpa repo
    Set<String> ids = new HashSet<String>();
    Query query = entityManager.createQuery(SELECT_ALL_STRING);
    query.setParameter(1, PROCESSOR_NAME);
    List<MessageProcessed> list = query.getResultList();
    for (MessageProcessed item : list) {
        ids.add(item.getMessageId());
    }
    assertEquals(3, ids.size());
    assertTrue("Should contain message 1", ids.contains("1"));
    assertTrue("Should contain message 2", ids.contains("2"));
    assertTrue("Should contain message 3", ids.contains("3"));
}
Also used : SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) Query(javax.persistence.Query) MessageProcessed(org.apache.camel.processor.idempotent.jpa.MessageProcessed) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with SpringRouteBuilder

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

the class JpaPollingConsumerLockEntityTest method createRouteBuilder.

@Override
protected RouteBuilder createRouteBuilder() {
    return new SpringRouteBuilder() {

        public void configure() {
            AggregationStrategy enrichStrategy = new AggregationStrategy() {

                @Override
                public Exchange aggregate(Exchange originalExchange, Exchange jpaExchange) {
                    Customer customer = jpaExchange.getIn().getBody(Customer.class);
                    customer.setOrderCount(customer.getOrderCount() + 1);
                    return jpaExchange;
                }
            };
            onException(Exception.class).setBody().simple("${exception}").to("mock:error").handled(true);
            from("direct:locked").onException(OptimisticLockException.class).redeliveryDelay(60).maximumRedeliveries(2).end().pollEnrich().simple("jpa://" + Customer.class.getName() + "?lockModeType=OPTIMISTIC_FORCE_INCREMENT&query=select c from Customer c where c.name like '${header.name}'").aggregationStrategy(enrichStrategy).to("jpa://" + Customer.class.getName()).setBody().simple("orders: ${body.orderCount}").to("mock:locked");
            from("direct:not-locked").pollEnrich().simple("jpa://" + Customer.class.getName() + "?query=select c from Customer c where c.name like '${header.name}'").aggregationStrategy(enrichStrategy).to("jpa://" + Customer.class.getName()).setBody().simple("orders: ${body.orderCount}").to("mock:not-locked");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) Customer(org.apache.camel.examples.Customer) AggregationStrategy(org.apache.camel.processor.aggregate.AggregationStrategy)

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