Java Code Examples for org.apache.camel.builder.NotifyBuilder#matches()

The following examples show how to use org.apache.camel.builder.NotifyBuilder#matches() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NotifyTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotifyWhenAnyDoneMatches() throws Exception {
    // use a predicate to indicate when a certain message is done
    NotifyBuilder notify = new NotifyBuilder(context)
            .from("seda:order").whenAnyDoneMatches(body().isEqualTo("OK,123,2017-04-20'T'15:48:00,2222,3333")).create();

    // send in 2 messages. Its the 2nd message we want to test
    template.sendBody("seda:order", "123,2017-04-20'T'15:47:59,4444,5555");
    template.sendBody("seda:order", "123,2017-04-20'T'15:48:00,2222,3333");

    boolean matches = notify.matches(5, TimeUnit.SECONDS);
    assertTrue(matches);

    SedaEndpoint confirm = context.getEndpoint("seda:confirm", SedaEndpoint.class);
    // there should be 2 messages on the confirm queue
    assertEquals(2, confirm.getExchanges().size());
    // and the 2nd message should be the message we wanted to test for
    assertEquals("OK,123,2017-04-20'T'15:48:00,2222,3333", confirm.getExchanges().get(1).getIn().getBody());
}
 
Example 2
Source File: NotifyTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotifyFrom() throws Exception {
    // use from to indicate it should only be messages originating from the given endpoint
    NotifyBuilder notify = new NotifyBuilder(context)
            .from("seda:order").whenDone(1).create();

    template.sendBody("seda:quote", "Camel rocks");
    template.sendBody("seda:order", "123,2017-04-20'T'15:47:59,4444,5555");

    boolean matches = notify.matches(5, TimeUnit.SECONDS);
    assertTrue(matches);

    SedaEndpoint confirm = context.getEndpoint("seda:confirm", SedaEndpoint.class);
    assertEquals(1, confirm.getExchanges().size());
    assertEquals("OK,123,2017-04-20'T'15:47:59,4444,5555", confirm.getExchanges().get(0).getIn().getBody());
}
 
Example 3
Source File: OrderTest.java    From camelinaction2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testOrderClientInvalid() throws Exception {
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();

    Calendar cal = Calendar.getInstance(Locale.US);
    cal.set(Calendar.YEAR, 2017);
    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,2017-04-20T07:47:58,5555,2222", list.get(0).getIn().getBody(String.class));
}
 
Example 4
Source File: OrderTest.java    From camelinaction with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example 5
Source File: NotifyTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testNotifyOr() throws Exception {
    // shows how to stack multiple expressions using binary operations (or)
    NotifyBuilder notify = new NotifyBuilder(context)
            .from("seda:quote").whenReceived(1).or().whenFailed(1).create();

    template.sendBody("seda:quote", "Camel rocks");
    template.sendBody("seda:order", "123,2010-04-20'T'15:48:00,2222,3333");

    boolean matches = notify.matches(5, TimeUnit.SECONDS);
    assertTrue(matches);
}
 
Example 6
Source File: ManualRouteWithOnCompletionTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testManualRouteWithOnCompletion() throws Exception {
    // notify us when the exchange is done
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();

    // we actually do not need to use a mock but since we wanted to show the trick
    // in the createRouteBuilders we do both. We could just have relied on using
    // the NotifyBuilder to signal when the exchange is done
    getMockEndpoint("mock:update").expectedMessageCount(2);

    // route should be stopped at startup
    assertTrue("Route should be stopped at startup", context.getRouteStatus("manual").isStopped());

    // then start the route
    context.startRoute("manual");

    // send a file which is picked up and processed
    String input = "4444,57123,Bumper,50\n4444,57124,Fender,87";
    template.sendBodyAndHeader("file:target/inventory/manual", input, Exchange.FILE_NAME, "manual.csv");

    // assert we got the message
    assertMockEndpointsSatisfied();

    // wait for the route to be done
    boolean matches = notify.matches(5, TimeUnit.SECONDS);
    assertTrue("Should match notifier", matches);

    // we gotta wait just a little extra to stop, before querying status
    Thread.sleep(1000);
    // it should have stopped itself
    assertTrue("Route should be stopped", context.getRouteStatus("manual").isStopped());
}
 
Example 7
Source File: BigFileTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 8
Source File: SpringManualRouteWithOnCompletionTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testManualRouteWithOnCompletion() throws Exception {
    // notify us when the exchange is done
    NotifyBuilder notify = new NotifyBuilder(context).whenDone(1).create();

    // we actually do not need to use a mock but since we wanted to show the trick
    // in the createRouteBuilders we do both. We could just have relied on using
    // the NotifyBuilder to signal when the exchange is done
    getMockEndpoint("mock:update").expectedMessageCount(2);

    // route should be stopped at startup
    assertTrue("Route should be stopped at startup", context.getRouteStatus("manual").isStopped());

    // then start the route
    context.startRoute("manual");

    // send a file which is picked up and processed
    String input = "4444,57123,Bumper,50\n4444,57124,Fender,87";
    template.sendBodyAndHeader("file:target/inventory/manual", input, Exchange.FILE_NAME, "manual.csv");

    // assert we got the message
    assertMockEndpointsSatisfied();

    // wait for the route to be done
    notify.matches(5, TimeUnit.SECONDS);

    // we gotta wait just a little extra to stop
    Thread.sleep(5000);

    // it should have stopped itself
    assertTrue("Route should be stopped", context.getRouteStatus("manual").isStopped());
}
 
Example 9
Source File: SpringBigFileCachedThreadPoolTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 10
Source File: MetricsTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testMetrics() throws Exception {
    // wait for 20 or more to be done
    NotifyBuilder builder = new NotifyBuilder(context).whenDone(20).create();
    builder.matches(1, TimeUnit.MINUTES);

    context.stopRoute("foo");
    context.stopRoute("bar");

    // you can get access to dropwizard metrics from Java API as well
    MetricsRegistryService registryService = context.hasService(MetricsRegistryService.class);
    if (registryService != null) {
        MetricRegistry registry = registryService.getMetricsRegistry();

        long count = registry.timer("camel-1:foo.responses").getCount();
        double mean = registry.timer("camel-1:foo.responses").getMeanRate();
        double rate1 = registry.timer("camel-1:foo.responses").getOneMinuteRate();
        double rate5 = registry.timer("camel-1:foo.responses").getFiveMinuteRate();
        double rate15 = registry.timer("camel-1:foo.responses").getFifteenMinuteRate();
        log.info("Foo metrics: count={}, mean={}, rate1={}, rate5={}, rate15={}", count, mean, rate1, rate5, rate15);

        count = registry.timer("camel-1:bar.responses").getCount();
        mean = registry.timer("camel-1:bar.responses").getMeanRate();
        rate1 = registry.timer("camel-1:bar.responses").getOneMinuteRate();
        rate5 = registry.timer("camel-1:bar.responses").getFiveMinuteRate();
        rate15 = registry.timer("camel-1:bar.responses").getFifteenMinuteRate();
        log.info("Bar metrics: count={}, mean={}, rate1={}, rate5={}, rate15={}", count, mean, rate1, rate5, rate15);
    }

    // and we can also access the JMX MBean to dump the statistics in JSon
    ObjectName on = new ObjectName("org.apache.camel:context=camel-1,type=services,name=MetricsRegistryService");
    MBeanServer server = context.getManagementStrategy().getManagementAgent().getMBeanServer();
    String json = (String) server.invoke(on, "dumpStatisticsAsJson", null, null);
    log.info("MetricsRegistryService.dumpStatisticsAsJson() => \n{}", json);
}
 
Example 11
Source File: OrderTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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));
}
 
Example 12
Source File: SpringBigFileFixedThreadPoolTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 13
Source File: SpringBigFileParallelTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 14
Source File: BigFileTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 15
Source File: BigFileParallelTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 16
Source File: BigFileFixedThreadPoolTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 17
Source File: BigFileCachedThreadPoolTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@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 1 min timeout (use ctrl + c to stop)");
    notify.matches(60, TimeUnit.SECONDS);

    long delta = System.currentTimeMillis() - start;
    System.out.println("Took " + delta / 1000 + " seconds");
}
 
Example 18
Source File: SpringBigFileParallelTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 19
Source File: BigFileParallelTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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");
}
 
Example 20
Source File: SpringBigFileFixedThreadPoolTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@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");
}