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"));
}
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"));
}
Aggregations