Java Code Examples for org.apache.camel.ProducerTemplate#asyncSendBody()

The following examples show how to use org.apache.camel.ProducerTemplate#asyncSendBody() . 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: InboundEndpointTest.java    From vertx-camel-bridge with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDirectEndpoint(TestContext context) throws Exception {
  Async async = context.async();
  Endpoint endpoint = camel.getEndpoint("direct:foo");

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addInboundMapping(fromCamel("direct:foo").toVertx("test")));

  vertx.eventBus().consumer("test", message -> {
    context.assertEquals("hello", message.body());
    async.complete();
  });

  camel.start();
  BridgeHelper.startBlocking(bridge);

  ProducerTemplate producer = camel.createProducerTemplate();
  producer.asyncSendBody(endpoint, "hello");
}
 
Example 2
Source File: InboundEndpointTest.java    From vertx-camel-bridge with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDirectEndpointAndCustomType(TestContext context) throws Exception {
  Async async = context.async();
  Endpoint endpoint = camel.getEndpoint("direct:foo");

  vertx.eventBus().registerDefaultCodec(Person.class, new PersonCodec());

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addInboundMapping(fromCamel("direct:foo").toVertx("test")));

  vertx.eventBus().<Person>consumer("test", message -> {
    context.assertEquals("bob", message.body().getName());
    async.complete();
  });

  camel.start();
  BridgeHelper.startBlocking(bridge);

  ProducerTemplate producer = camel.createProducerTemplate();
  producer.asyncSendBody(endpoint, new Person().setName("bob"));
}
 
Example 3
Source File: InboundEndpointTest.java    From vertx-camel-bridge with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDirectEndpoint2(TestContext context) throws Exception {
  Async async = context.async();
  Endpoint endpoint = camel.getEndpoint("direct:foo");

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addInboundMapping(fromCamel(endpoint).toVertx("test")));

  vertx.eventBus().consumer("test", message -> {
    context.assertEquals("hello", message.body());
    async.complete();
  });

  camel.start();
  BridgeHelper.startBlocking(bridge);

  ProducerTemplate producer = camel.createProducerTemplate();
  producer.asyncSendBody(endpoint, "hello");
}
 
Example 4
Source File: InboundEndpointTest.java    From vertx-camel-bridge with Apache License 2.0 6 votes vote down vote up
@Test
public void testWithDirectEndpointWithPublish(TestContext context) throws Exception {
  Async async = context.async();
  Async async2 = context.async();
  Endpoint endpoint = camel.getEndpoint("direct:foo");

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addInboundMapping(fromCamel(endpoint).toVertx("test").usePublish()));

  vertx.eventBus().consumer("test", message -> {
    context.assertEquals("hello", message.body());
    async.complete();
  });

  vertx.eventBus().consumer("test", message -> {
    context.assertEquals("hello", message.body());
    async2.complete();
  });

  camel.start();
  BridgeHelper.startBlocking(bridge);

  ProducerTemplate producer = camel.createProducerTemplate();
  producer.asyncSendBody(endpoint, "hello");
}
 
Example 5
Source File: InboundEndpointTest.java    From vertx-camel-bridge with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithDirectEndpointWithPublishAndCustomType(TestContext context) throws Exception {
  Async async = context.async();
  Async async2 = context.async();
  Endpoint endpoint = camel.getEndpoint("direct:foo");

  vertx.eventBus().registerDefaultCodec(Person.class, new PersonCodec());

  bridge = CamelBridge.create(vertx, new CamelBridgeOptions(camel)
      .addInboundMapping(fromCamel(endpoint).toVertx("test").usePublish()));

  vertx.eventBus().<Person>consumer("test", message -> {
    context.assertEquals("bob", message.body().getName());
    async.complete();
  });

  vertx.eventBus().<Person>consumer("test", message -> {
    context.assertEquals("bob", message.body().getName());
    async2.complete();
  });

  camel.start();
  BridgeHelper.startBlocking(bridge);

  ProducerTemplate producer = camel.createProducerTemplate();
  producer.asyncSendBody(endpoint, new Person().setName("bob"));
}
 
Example 6
Source File: SJMSIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageProviderRoute() throws Exception {

    CamelContext camelctx = createCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").
            transform(body().prepend("Hello ")).
            to("sjms:queue:" + QUEUE_NAME + "?connectionFactory=#cfactory");
        }
    });

    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);

    camelctx.start();
    try {
        // Get the message from the queue
        ConnectionFactory cfactory = lookupConnectionFactory();
        Connection connection = cfactory.createConnection();
        try {
            receiveMessage(connection, QUEUE_JNDI_NAME, new MessageListener() {
                @Override
                public void onMessage(Message message) {
                    TextMessage text = (TextMessage) message;
                    try {
                        result.add(text.getText());
                    } catch (JMSException ex) {
                        result.add(ex.getMessage());
                    }
                    latch.countDown();
                }
            });

            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Kermit");

            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("One message", 1, result.size());
            Assert.assertEquals("Hello Kermit", result.get(0));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: PahoIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMQTTProducer() throws Exception {

    String conUrl = TestUtils.getResourceValue(getClass(), "/tcp-connection");

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").
            transform(body().prepend("Hello ")).
            to("paho:" + BrokerSetup.TEST_TOPIC + "?brokerUrl=" + conUrl);
        }
    });

    camelctx.start();
    try {
        MqttClient client = new MqttClient(conUrl, "MqttClient", new MemoryPersistence());
        MqttConnectOptions opts = new MqttConnectOptions();
        opts.setCleanSession(true);
        client.connect(opts);
        client.subscribe(BrokerSetup.TEST_TOPIC, 2);

        final List<String> result = new ArrayList<>();
        final CountDownLatch latch = new CountDownLatch(1);

        client.setCallback(new MqttCallback() {

            @Override
            public void connectionLost(Throwable cause) {
            }

            @Override
            public void messageArrived(String topic, MqttMessage message) throws Exception {
                result.add(new String (message.getPayload()));
                latch.countDown();
            }

            @Override
            public void deliveryComplete(IMqttDeliveryToken token) {
            }});

        ProducerTemplate producer = camelctx.createProducerTemplate();
        producer.asyncSendBody("direct:start", "Kermit");

        Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
        Assert.assertEquals("One message", 1, result.size());
        Assert.assertEquals("Hello Kermit", result.get(0));
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: JMSIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageProviderRoute() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .transform(simple("Hello ${body}"))
            .toF("jms:queue:%s?connectionFactory=ConnectionFactory", QUEUE_NAME);
        }
    });

    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);

    camelctx.start();
    try {
        // Get the message from the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        try {
            receiveMessage(connection, QUEUE_JNDI_NAME, message -> {
                TextMessage text = (TextMessage) message;
                try {
                    result.add(text.getText());
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            });

            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Kermit");

            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("One message", 1, result.size());
            Assert.assertEquals("Hello Kermit", result.get(0));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: JMSIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testMessageProviderRouteWithClientAck() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start")
            .transform(simple("Hello ${body}"))
            .toF("jms:queue:%s?connectionFactory=ConnectionFactory", QUEUE_NAME);
        }
    });

    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(4);

    camelctx.start();
    try {
        // Get the message from the queue
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        final Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
        Destination destination = (Destination) initialctx.lookup(QUEUE_JNDI_NAME);
        MessageConsumer consumer = session.createConsumer(destination);
        consumer.setMessageListener(new MessageListener() {
            @Override
            public synchronized void onMessage(Message message) {
                TextMessage text = (TextMessage) message;
                long count = latch.getCount();
                try {
                    // always append the message text
                    result.add(text.getText() + " " + (5 - count));

                    if (count == 4) {
                        // do nothing on first
                    } else if (count == 3) {
                        // recover causing a redelivery
                        session.recover();
                    } else {
                        // ackknowledge
                        message.acknowledge();
                    }
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            }
        });
        connection.start();

        try {
            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Message");
            producer.asyncSendBody("direct:start", "Message");

            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("Four messages", 4, result.size());
            Assert.assertEquals("Hello Message 1", result.get(0));
            Assert.assertEquals("Hello Message 2", result.get(1));
            Assert.assertEquals("Hello Message 3", result.get(2));
            Assert.assertEquals("Hello Message 4", result.get(3));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.close();
    }
}
 
Example 10
Source File: ActiveMQIntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testReceiveMessage() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").
            transform(simple("Hello ${body}")).
            toF("activemq:queue:%s?connectionFactory=java:/ActiveMQConnectionFactory", QUEUE_NAME);
        }
    });

    final StringBuffer result = new StringBuffer();
    final CountDownLatch latch = new CountDownLatch(1);

    camelctx.start();
    try {
        ConnectionFactory connectionFactory = lookupConnectionFactory();
        Connection con = connectionFactory.createConnection();
        try {
            receiveMessage(con, message -> {
                TextMessage text = (TextMessage) message;
                try {
                    result.append(text.getText());
                } catch (JMSException ex) {
                    result.append(ex.getMessage());
                }
                latch.countDown();
            });

            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Kermit");

            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("Hello Kermit", result.toString());
        } finally {
            con.close();
        }
    } finally {
        camelctx.close();
    }
}
 
Example 11
Source File: SJMS2IntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testSJMS2Producer() throws Exception {
    CamelContext camelctx = new DefaultCamelContext(new JndiBeanRepository());
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("direct:start").
            to("sjms2:queue:" + QUEUE_NAME + "?connectionFactory=#ConnectionFactory");
        }
    });

    final List<String> result = new ArrayList<>();
    final CountDownLatch latch = new CountDownLatch(1);

    camelctx.start();
    try {
        ConnectionFactory cfactory = (ConnectionFactory) initialctx.lookup("java:/ConnectionFactory");
        Connection connection = cfactory.createConnection();
        try {
            receiveMessage(connection, QUEUE_JNDI_NAME, message -> {
                TextMessage text = (TextMessage) message;
                try {
                    result.add(text.getText());
                } catch (JMSException ex) {
                    result.add(ex.getMessage());
                }
                latch.countDown();
            });

            ProducerTemplate producer = camelctx.createProducerTemplate();
            producer.asyncSendBody("direct:start", "Hello Kermit");

            Assert.assertTrue(latch.await(10, TimeUnit.SECONDS));
            Assert.assertEquals("One message", 1, result.size());
            Assert.assertEquals("Hello Kermit", result.get(0));
        } finally {
            connection.close();
        }
    } finally {
        camelctx.close();
    }
}