@Test public void testOrderClientInvalid() throws Exception { NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); Calendar cal = Calendar.getInstance(Locale.US); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.HOUR_OF_DAY, 7); cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 58); Date date = cal.getTime(); OrderClient client = new OrderClient("tcp://localhost:61616"); // when using customer id 999 we force an invalid order client.sendOrder(999, date, "5555", "2222"); boolean matches = notify.matches(5, TimeUnit.SECONDS); assertTrue(matches); // should be one message in confirm queue BrowsableEndpoint be = context.getEndpoint("activemq:queue:invalid", BrowsableEndpoint.class); List<Exchange> list = be.getExchanges(); assertEquals(1, list.size()); assertEquals("999,2010-04-20T07:47:58,5555,2222", list.get(0).getIn().getBody(String.class)); }
@Test public void testOrderClientValid() throws Exception { // notify when one message is done NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); // setup order information Calendar cal = Calendar.getInstance(Locale.US); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.HOUR_OF_DAY, 7); cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 58); Date date = cal.getTime(); // send an order using the client OrderClient client = new OrderClient("tcp://localhost:61616"); client.sendOrder(123, date, "4444", "5555"); // use the notifier to wait for Camel to process the message // wait at most 5 seconds to avoid blocking forever if something goes wrong boolean matches = notify.matches(5, TimeUnit.SECONDS); // true means the notifier condition matched (= 1 message is done) assertTrue(matches); // should be one message in confirm queue BrowsableEndpoint be = context.getEndpoint("activemq:queue:confirm", BrowsableEndpoint.class); List<Exchange> list = be.getExchanges(); assertEquals(1, list.size()); assertEquals("OK,123,2010-04-20T07:47:58,4444,5555", list.get(0).getIn().getBody(String.class)); }
@Test public void testWithDonkey() throws Exception { // we should have 1 original message + 6 redelivery attempts NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); context.start(); // there should be 0 row in the database when we start assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookorders", Long.class)); template.sendBody("activemq:queue:inbox", "Donkey in Action"); String reply = consumer.receiveBody("activemq:queue:order", 10000, String.class); assertNull("There should be no reply", reply); // wait for the route to complete assertTrue(notify.matches(10, TimeUnit.SECONDS)); // and then message failed and was moved into the DLQ by the ActiveMQ broker reply = consumer.receiveBody("activemq:queue:ActiveMQ.DLQ", 10000, String.class); assertNotNull("It should have been moved to DLQ", reply); // there should be 0 row in the database with the order assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookorders", Long.class)); // there should be 1 + 6 redelivery attempt row in the database with the audit-log assertEquals( Long.valueOf(1), jdbc.queryForObject( "select count(*) from bookaudit where order_redelivery = 'false'", Long.class)); assertEquals( Long.valueOf(6), jdbc.queryForObject( "select count(*) from bookaudit where order_redelivery = 'true'", Long.class)); // print the SQL log.info("The following orders was recorded in the orders ..."); List<Map<String, Object>> rows = jdbc.queryForList("select * from bookorders"); for (Map<String, Object> row : rows) { log.info("Book order[id={}, book={}]", row.get("order_id"), row.get("order_book")); } log.info("The following orders was recorded in the audit-log ..."); rows = jdbc.queryForList("select * from bookaudit"); for (Map<String, Object> row : rows) { log.info( "Book audit-log[id={}, book={}, redelivery={}]", row.get("order_id"), row.get("order_book"), row.get("order_redelivery")); } }
@Test public void testBigFile() throws Exception { // when the first exchange is done NotifyBuilder notify = new NotifyBuilder(context).whenDoneByIndex(0).create(); long start = System.currentTimeMillis(); System.out.println("Waiting to be done with 2 min timeout (use ctrl + c to stop)"); notify.matches(2 * 60, TimeUnit.SECONDS); long delta = System.currentTimeMillis() - start; System.out.println("Took " + delta / 1000 + " seconds"); }
@Test public void testRollback() throws Exception { // we should keep failing so wait for 5 or more failures NotifyBuilder notify = new NotifyBuilder(context).whenFailed(5).create(); // should take a bit before we process 5 or more, because we use backoff on error assertTrue(notify.matches(20, TimeUnit.SECONDS)); // and there should be 1 rows in the database as we keep rolling back int rows = jdbc.queryForObject("select count(*) from partner_metric", Integer.class); assertEquals(1, rows); context.stop(); }
@Test public void testWithCamel() throws Exception { // we should have 1 original message NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create(); context.start(); // there should be 0 row in the database when we start assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookorders", Long.class)); assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookaudit", Long.class)); template.sendBody("activemq:queue:inbox", "Camel in Action"); // wait for the route to complete assertTrue(notify.matches(10, TimeUnit.SECONDS)); // and the message was sent to the order queue String reply = consumer.receiveBody("activemq:queue:order", 10000, String.class); assertEquals("Camel in Action", reply); // the database need a little sleep time before commits are visible Thread.sleep(1000); // there should be 1 row in the database with the order, and 0 in the audit-log as it was marked // to only rollback assertEquals( Long.valueOf(1), jdbc.queryForObject("select count(*) from bookorders", Long.class)); assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookaudit", Long.class)); // print the SQL log.info("The following orders was recorded in the orders ..."); List<Map<String, Object>> rows = jdbc.queryForList("select * from bookorders"); for (Map<String, Object> row : rows) { log.info("Book order[id={}, book={}]", row.get("order_id"), row.get("order_book")); } log.info("The following orders was recorded in the audit-log ..."); rows = jdbc.queryForList("select * from bookaudit"); for (Map<String, Object> row : rows) { log.info( "Book audit-log[id={}, book={}, redelivery={}]", row.get("order_id"), row.get("order_book"), row.get("order_redelivery")); } }
public void testRetryUntilRecipientNotFailAndFail() throws Exception { invoked.set(0); NotifyBuilder event = event().whenDone(1).create(); getMockEndpoint("mock:result").expectedMessageCount(0); getMockEndpoint("mock:foo").expectedMinimumMessageCount(0); template.sendBodyAndHeader("seda:start", "Hello World", "recipientListHeader", "not-fail,fail"); assertMockEndpointsSatisfied(); // wait until its done before we stop and check that retry was invoked boolean matches = event.matches(10, TimeUnit.SECONDS); assertTrue(matches); context.stop(); assertEquals(3, invoked.get()); }
@Test public void testOrderClientFailure() throws Exception { // now we expect the message to fail so we use whenFailed NotifyBuilder notify = new NotifyBuilder(context).whenFailed(1).create(); Calendar cal = Calendar.getInstance(Locale.US); cal.set(Calendar.YEAR, 2010); cal.set(Calendar.MONTH, Calendar.APRIL); cal.set(Calendar.DAY_OF_MONTH, 20); cal.set(Calendar.HOUR_OF_DAY, 7); cal.set(Calendar.MINUTE, 47); cal.set(Calendar.SECOND, 58); Date date = cal.getTime(); OrderClient client = new OrderClient("tcp://localhost:61616"); // by using 9999 as the last item id we force an exception to occur client.sendOrder(123, date, "4444", "9999"); boolean matches = notify.matches(5, TimeUnit.SECONDS); assertTrue(matches); }
@Test public void testAuditLogFail() throws Exception { // we should have 1 original message + 6 redelivery attempts NotifyBuilder notify = new NotifyBuilder(context).whenDone(1 + 6).create(); // simulate the audit-log will fail context .getRouteDefinition("audit") .adviceWith( context, new AdviceWithRouteBuilder() { @Override public void configure() throws Exception { // simulate error connecting to database interceptSendToEndpoint("bean:auditLogService") .skipSendToOriginalEndpoint() .throwException(new IOException("Cannot connect to database")); } }); context.start(); // there should be 0 row in the database when we start assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookorders", Long.class)); assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookaudit", Long.class)); template.sendBody("activemq:queue:inbox", "Camel in Action"); // wait for the route to complete assertTrue(notify.matches(10, TimeUnit.SECONDS)); // the database need a little sleep time before commits are visible Thread.sleep(1000); // there should be 0 row in the database with the order assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookorders", Long.class)); assertEquals( Long.valueOf(0), jdbc.queryForObject("select count(*) from bookaudit", Long.class)); // and then message failed and was moved into the DLQ by the ActiveMQ broker String reply = consumer.receiveBody("activemq:queue:ActiveMQ.DLQ", 10000, String.class); assertNotNull("It should have been moved to DLQ", reply); // print the SQL log.info("The following orders was recorded in the orders ..."); List<Map<String, Object>> rows = jdbc.queryForList("select * from bookorders"); for (Map<String, Object> row : rows) { log.info("Book order[id={}, book={}]", row.get("order_id"), row.get("order_book")); } log.info("The following orders was recorded in the audit-log ..."); rows = jdbc.queryForList("select * from bookaudit"); for (Map<String, Object> row : rows) { log.info( "Book audit-log[id={}, book={}, redelivery={}]", row.get("order_id"), row.get("order_book"), row.get("order_redelivery")); } }