Search in sources :

Example 1 with SpringRouteBuilder

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

the class QueueToProcessorTransactionTest method testRollbackUsingXmlQueueToProcessor.

@Test
public void testRollbackUsingXmlQueueToProcessor() throws Exception {
    // configure routes and add to camel context
    context.addRoutes(new SpringRouteBuilder() {

        @Override
        public void configure() throws Exception {
            Policy required = lookup("PROPAGATION_REQUIRED_POLICY", SpringTransactionPolicy.class);
            from("activemq:queue:foo").policy(required).process(new ConditionalExceptionProcessor());
        }
    });
    assertResult();
}
Also used : SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy) Policy(org.apache.camel.spi.Policy) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy) Test(org.junit.Test)

Example 2 with SpringRouteBuilder

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

the class QueueToQueueRequestReplyTransactionTest method testRollbackUsingXmlQueueToQueueRequestReplyUsingDynamicMessageSelector.

@Test
public void testRollbackUsingXmlQueueToQueueRequestReplyUsingDynamicMessageSelector() throws Exception {
    final ConditionalExceptionProcessor cp = new ConditionalExceptionProcessor(5);
    context.addRoutes(new SpringRouteBuilder() {

        @Override
        public void configure() throws Exception {
            Policy required = lookup("PROPAGATION_REQUIRED_POLICY", SpringTransactionPolicy.class);
            from("activemq:queue:foo").policy(required).process(cp).to("activemq-1:queue:bar?replyTo=queue:bar.reply");
            from("activemq-1:queue:bar").process(new Processor() {

                public void process(Exchange e) {
                    String request = e.getIn().getBody(String.class);
                    Message out = e.getOut();
                    String selectorValue = e.getIn().getHeader("camelProvider", String.class);
                    if (selectorValue != null) {
                        out.setHeader("camelProvider", selectorValue);
                    }
                    out.setBody("Re: " + request);
                }
            });
        }
    });
    for (int i = 0; i < 5; ++i) {
        Object reply = template.requestBody("activemq:queue:foo", "blah" + i);
        assertTrue("Received unexpeced reply", reply.equals("Re: blah" + i));
        assertTrue(cp.getErrorMessage(), cp.getErrorMessage() == null);
    }
}
Also used : SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy) Policy(org.apache.camel.spi.Policy) Exchange(org.apache.camel.Exchange) Processor(org.apache.camel.Processor) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) Message(org.apache.camel.Message) SpringTransactionPolicy(org.apache.camel.spring.spi.SpringTransactionPolicy) Test(org.junit.Test)

Example 3 with SpringRouteBuilder

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

the class QueueToQueueTransactionWithoutDefineTransactionManagerTest method testNoTransactionRollbackUsingXmlQueueToQueue.

@Test
public void testNoTransactionRollbackUsingXmlQueueToQueue() throws Exception {
    // configure routes and add to camel context
    context.addRoutes(new SpringRouteBuilder() {

        @Override
        public void configure() throws Exception {
            errorHandler(noErrorHandler());
            from("activemq:queue:foo?transacted=false").process(new ConditionalExceptionProcessor()).to("activemq:queue:bar?transacted=false");
        }
    });
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();
    template.sendBody("activemq:queue:foo", "blah");
    notify.matchesMockWaitTime();
    assertTrue("Expected only 1 calls to process() (1 failure) but encountered " + getConditionalExceptionProcessor().getCount() + ".", getConditionalExceptionProcessor().getCount() == 1);
}
Also used : NotifyBuilder(org.apache.camel.builder.NotifyBuilder) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) Test(org.junit.Test)

Example 4 with SpringRouteBuilder

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

the class JpaIdempotentConsumerTest method testFailedExchangesNotAdded.

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

        @Override
        public void configure() throws Exception {
            errorHandler(deadLetterChannel("mock:error").maximumRedeliveries(0).redeliveryDelay(0).logStackTrace(false));
            from("direct:start").idempotentConsumer(header("messageId"), jpaMessageIdRepository(lookup(EntityManagerFactory.class), PROCESSOR_NAME)).process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    String id = exchange.getIn().getHeader("messageId", String.class);
                    if (id.equals("2")) {
                        throw new IllegalArgumentException("Damn I cannot handle id 2");
                    }
                }
            }).to("mock:result");
        }
    });
    context.start();
    // we send in 2 messages with id 2 that fails
    getMockEndpoint("mock:error").expectedMessageCount(2);
    resultEndpoint.expectedBodiesReceived("one", "three");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("2", "two");
    sendMessage("1", "one");
    sendMessage("3", "three");
    assertMockEndpointsSatisfied();
    // only message 1 and 3 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(2, ids.size());
    assertTrue("Should contain message 1", ids.contains("1"));
    assertTrue("Should contain message 3", ids.contains("3"));
}
Also used : Processor(org.apache.camel.Processor) Query(javax.persistence.Query) MessageProcessed(org.apache.camel.processor.idempotent.jpa.MessageProcessed) Exchange(org.apache.camel.Exchange) SpringRouteBuilder(org.apache.camel.spring.SpringRouteBuilder) EntityManagerFactory(javax.persistence.EntityManagerFactory) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 5 with SpringRouteBuilder

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

the class TransactionalClientDataSourceLookupTypeTest method createRouteBuilder.

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(SpringTransactionPolicy.class);
            // Notice: transactionErrorHandler is in SpringRouteBuilder
            if (useTransactionErrorHandler) {
                // 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)

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