org.apache.camel.support.SynchronizationAdapter Java Examples

The following examples show how to use org.apache.camel.support.SynchronizationAdapter. 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: CamelCallbackTest.java    From camelinaction with Apache License 2.0 5 votes vote down vote up
@Test
public void testCamelCallback() throws Exception {
    // echos is the list of replies which could be modified by multiple thread
    final List<String> echos = new CopyOnWriteArrayList<String>();
    final CountDownLatch latch = new CountDownLatch(3);

    // use this callback to gather the replies and add it to the echos list
    Synchronization callback = new SynchronizationAdapter() {
        @Override
        public void onDone(Exchange exchange) {
            // get the reply and add it to echoes
            echos.add(exchange.getOut().getBody(String.class));
            // count down latch when we receive a response
            latch.countDown();
        }
    };

    // now submit 3 async request/reply messages and use the same callback to
    // handle the replies
    template.asyncCallbackRequestBody("seda:echo", "Donkey", callback);
    template.asyncCallbackRequestBody("seda:echo", "Tiger", callback);
    template.asyncCallbackRequestBody("seda:echo", "Camel", callback);

    // wait until the messages is done, or timeout after 6 seconds
    latch.await(6, TimeUnit.SECONDS);

    // assert we got 3 replies
    assertEquals(3, echos.size());
    List result = new ArrayList(echos);
    // sort list so we can assert by index
    Collections.sort(result);
    assertEquals("CamelCamel", result.get(0));
    assertEquals("DonkeyDonkey", result.get(1));
    assertEquals("TigerTiger", result.get(2));
}
 
Example #2
Source File: GRPCIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testGRPCAsynchronousProducer() throws Exception {

    Future<String> future = AvailablePortFinder.readServerDataAsync("grpc-port");
    int port = Integer.parseInt(future.get(10, TimeUnit.SECONDS));

    DefaultCamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .toF("grpc://localhost:%d/%s?method=pingAsyncSync", port, "org.wildfly.camel.test.grpc.subA.PingPong");
        }
    });

    camelctx.start();
    try {
        final CountDownLatch latch = new CountDownLatch(1);

        PingRequest pingRequest = PingRequest.newBuilder()
            .setPingName(GRPC_TEST_PING_VALUE)
            .setPingId(GRPC_TEST_PING_ID_1)
            .build();

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.asyncCallbackSendBody("direct:start", pingRequest, new SynchronizationAdapter() {
            @Override
            public void onComplete(Exchange exchange) {
                latch.countDown();

                @SuppressWarnings("unchecked")
                List<PongResponse> response = exchange.getOut().getBody(List.class);

                Assert.assertEquals(2, response.size());
                Assert.assertEquals(response.get(0).getPongId(), GRPC_TEST_PONG_ID_1);
                Assert.assertEquals(response.get(0).getPongName(), GRPC_TEST_PING_VALUE + GRPC_TEST_PONG_VALUE);
                Assert.assertEquals(response.get(1).getPongId(), GRPC_TEST_PONG_ID_2);
            }
        });

        Assert.assertTrue("Gave up waiting for latch", latch.await(5, TimeUnit.SECONDS));
    } finally {
        camelctx.close();
    }
}