Search in sources :

Example 1 with MessageProcessed

use of org.apache.camel.processor.idempotent.jpa.MessageProcessed 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 2 with MessageProcessed

use of org.apache.camel.processor.idempotent.jpa.MessageProcessed 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)

Aggregations

HashSet (java.util.HashSet)2 Query (javax.persistence.Query)2 MessageProcessed (org.apache.camel.processor.idempotent.jpa.MessageProcessed)2 SpringRouteBuilder (org.apache.camel.spring.SpringRouteBuilder)2 Test (org.junit.Test)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)1 Exchange (org.apache.camel.Exchange)1 Processor (org.apache.camel.Processor)1