Java Code Examples for org.apache.camel.ConsumerTemplate#receiveBody()

The following examples show how to use org.apache.camel.ConsumerTemplate#receiveBody() . 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: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        ProducerTemplate template = camelctx.createProducerTemplate();
        Mockito.when(map.get("4711")).thenReturn("my-foo");
        template.sendBodyAndHeader("direct:get", null, HazelcastConstants.OBJECT_ID, "4711");

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        String body = consumer.receiveBody("seda:out", 5000, String.class);
        Mockito.verify(map).get("4711");
        Assert.assertEquals("my-foo", body);
    } finally {
        camelctx.close();
    }
}
 
Example 2
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAllEmptySet() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Set<Object> l = new HashSet<Object>();
        Map t = new HashMap();
        t.put("key1", "value1");
        t.put("key2", "value2");
        t.put("key3", "value3");
        Mockito.when(map.getAll(Mockito.anySet())).thenReturn(t);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:getAll", null, HazelcastConstants.OBJECT_ID, l);

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        String body = consumer.receiveBody("seda:out", 5000, String.class);
        Mockito.verify(map).getAll(l);
        Assert.assertTrue(body.contains("key1=value1"));
        Assert.assertTrue(body.contains("key2=value2"));
        Assert.assertTrue(body.contains("key3=value3"));
    } finally {
        camelctx.close();
    }
}
 
Example 3
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetAllOnlyOneKey() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Set<Object> l = new HashSet<Object>();
        l.add("key1");
        Map t = new HashMap();
        t.put("key1", "value1");
        Mockito.when(map.getAll(l)).thenReturn(t);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:getAll", null, HazelcastConstants.OBJECT_ID, l);

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        String body = consumer.receiveBody("seda:out", 5000, String.class);
        Mockito.verify(map).getAll(l);
        Assert.assertEquals("{key1=value1}", body);
    } finally {
        camelctx.close();
    }
}
 
Example 4
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testQuery() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        String sql = "bar > 1000";
        Mockito.when(map.values(Mockito.any(SqlPredicate.class))).thenReturn(Arrays.<Object>asList(new Dummy("beta", 2000), new Dummy("gamma", 3000)));

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:queue", null, HazelcastConstants.QUERY, sql);
        Mockito.verify(map).values(Mockito.any(SqlPredicate.class));

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Collection<?> b1 = consumer.receiveBody("seda:out", 5000, Collection.class);

        Assert.assertNotNull(b1);
        Assert.assertEquals(2, b1.size());
    } finally {
        camelctx.close();
    }
}
 
Example 5
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testEmptyQuery() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Mockito.when(map.values()).thenReturn(Arrays.<Object>asList(new Dummy("beta", 2000), new Dummy("gamma", 3000), new Dummy("delta", 4000)));

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:queue", null);
        Mockito. verify(map).values();

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Collection<?> b1 = consumer.receiveBody("seda:out", 5000, Collection.class);

        Assert.assertNotNull(b1);
        Assert.assertEquals(3, b1.size());
    } finally {
        camelctx.close();
    }
}
 
Example 6
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testContainsKey() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Mockito.when(map.containsKey("testOk")).thenReturn(true);
        Mockito.when(map.containsKey("testKo")).thenReturn(false);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBodyAndHeader("direct:containsKey", null, HazelcastConstants.OBJECT_ID, "testOk");

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Boolean body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsKey("testOk");
        Assert.assertEquals(true, body);
        template.sendBodyAndHeader("direct:containsKey", null, HazelcastConstants.OBJECT_ID, "testKo");
        body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsKey("testKo");
        Assert.assertEquals(false, body);
    } finally {
        camelctx.close();
    }
}
 
Example 7
Source File: HazelcastMapProducerIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testContainsValue() throws Exception {
    CamelContext camelctx = createCamelContext();
    camelctx.start();
    try {
        Mockito.when(map.containsValue("testOk")).thenReturn(true);
        Mockito.when(map.containsValue("testKo")).thenReturn(false);

        ProducerTemplate template = camelctx.createProducerTemplate();
        template.sendBody("direct:containsValue", "testOk");

        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        Boolean body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsValue("testOk");
        Assert.assertEquals(true, body);
        template.sendBody("direct:containsValue", "testKo");
        body = consumer.receiveBody("seda:out", 5000, Boolean.class);
        Mockito.verify(map).containsValue("testKo");
        Assert.assertEquals(false, body);
    } finally {
        camelctx.close();
    }
}
 
Example 8
Source File: CassandraIntegrationTest.java    From wildfly-camel with Apache License 2.0 6 votes vote down vote up
@Test
public void testConsumeAll() throws Exception {

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("cql://localhost/camel_ks?cql=" + CQL).to("seda:end");
        }
    });

    camelctx.start();
    try {
        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        List<?> result = consumer.receiveBody("seda:end", 3000, List.class);
        Assert.assertNotNull("Result not null", result);
        Assert.assertEquals("Two records selected", 2, result.size());
    } finally {
        camelctx.close();
    }
}
 
Example 9
Source File: CamelBridge.java    From incubator-batchee with Apache License 2.0 6 votes vote down vote up
public static Object receive(final String locator, final String endpoint, final long timeout, final Class<?> expected) {
    final BeanLocator.LocatorInstance<CamelTemplateLocator> locatorInstance = locator(locator);
    try {
        final ConsumerTemplate consumerTemplate = locatorInstance.getValue().findConsumerTemplate();
        if (timeout > 0) {
            if (expected != null) {
                return consumerTemplate.receiveBody(endpoint, expected);
            }
            return consumerTemplate.receiveBody(endpoint);
        }

        if (expected != null) {
            return consumerTemplate.receiveBody(endpoint, timeout, expected);
        }
        return consumerTemplate.receiveBody(endpoint, timeout);
    } finally {
        locatorInstance.release();
    }
}
 
Example 10
Source File: WildFlySwarmCamelTest.java    From camelinaction2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSeda() throws Exception {
    // send to the seda inbox queue
    producer.sendBody("Hello Swarm");

    ConsumerTemplate consumer = camelContext.createConsumerTemplate();
    // use 5 second timeout to receive the message from outbox
    Object body = consumer.receiveBody("seda:outbox", 5000);

    // expect it was the message we sent
    assertEquals("Hello Swarm", body);
}
 
Example 11
Source File: JMXIntegrationTest.java    From wildfly-camel with Apache License 2.0 5 votes vote down vote up
@Test
public void testMonitorMBeanAttribute() throws Exception {
    CamelContext context = contextRegistry.getCamelContext("jmx-context-1");
    Assert.assertNotNull("jmx-context-1 not null", context);
    final String routeName = context.getRoutes().get(0).getId();

    MBeanServer server = ManagementFactory.getPlatformMBeanServer();
    ObjectName onameAll = ObjectNameFactory.create("org.apache.camel:*");
    Set<ObjectInstance> mbeans = server.queryMBeans(onameAll, null);
    System.out.println(">>>>>>>>> MBeans: " + mbeans.size());
    mbeans.forEach(mb -> System.out.println(mb.getObjectName()));

    CamelContext camelctx = new DefaultCamelContext();
    camelctx.addRoutes(new RouteBuilder() {
        @Override
        public void configure() throws Exception {
            from("jmx:platform?format=raw&objectDomain=org.apache.camel&key.context=jmx-context-1&key.type=routes&key.name=\"" + routeName + "\"" +
            "&monitorType=counter&observedAttribute=ExchangesTotal&granularityPeriod=500").
            to("direct:end");
        }
    });

    camelctx.start();
    try {
        ConsumerTemplate consumer = camelctx.createConsumerTemplate();
        MonitorNotification notifcation = consumer.receiveBody("direct:end", MonitorNotification.class);
        Assert.assertEquals("ExchangesTotal", notifcation.getObservedAttribute());
    } finally {
        camelctx.close();
    }
}
 
Example 12
Source File: S3IntegrationTest.java    From wildfly-camel with Apache License 2.0 4 votes vote down vote up
@Test
public void testBucketOperations() throws Exception {

    AmazonS3Client s3Client = provider.getClient();
    Assume.assumeNotNull("AWS client not null", s3Client);

    assertNoStaleBuckets(s3Client, "before");
    try {
        try {
            S3Utils.createBucket(s3Client, bucketName);

            WildFlyCamelContext camelctx = new WildFlyCamelContext();
            camelctx.getNamingContext().bind("s3Client", s3Client);
            camelctx.addRoutes(new RouteBuilder() {
                @Override
                public void configure() throws Exception {
                    String clientref = "amazonS3Client=#s3Client";
                    from("direct:upload").to("aws-s3://" + bucketName + "?" + clientref);
                    from("aws-s3://" + bucketName + "?" + clientref).to("seda:read");
                }
            });

            try {
                camelctx.start();
                try {

                    // Put object
                    Map<String, Object> headers = new HashMap<>();
                    headers.put(S3Constants.KEY, OBJECT_KEY);

                    ProducerTemplate producer = camelctx.createProducerTemplate();
                    String content = "My bucket content";
                    String result1 = producer.requestBodyAndHeaders("direct:upload", content, headers,
                            String.class);
                    Assert.assertEquals(content, result1);

                    ConsumerTemplate consumer = camelctx.createConsumerTemplate();
                    String result2 = consumer.receiveBody("seda:read", String.class);
                    Assert.assertEquals(content, result2);
                } finally {
                    camelctx.close();
                }
            } finally {
                s3Client.deleteObject(bucketName, OBJECT_KEY);
            }
        } finally {
            S3Utils.deleteBucket(s3Client, bucketName);
        }
    } finally {
        assertNoStaleBuckets(s3Client, "after");
    }

}
 
Example 13
Source File: CamelConsumerTemplate.java    From hazelcastmq with Apache License 2.0 4 votes vote down vote up
@Override
public void start() throws Exception {

  // Create a Hazelcast instance.
  Config config = new Config();
  config.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(false);
  HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config);

  try {
    // Create the HazelcaseMQ instance.
    HazelcastMQConfig mqConfig = new HazelcastMQConfig();
    mqConfig.setHazelcastInstance(hazelcast);
    HazelcastMQInstance mqInstance = HazelcastMQ
        .newHazelcastMQInstance(mqConfig);

    // Create the camel component.
    HazelcastMQCamelConfig mqCamelConfig = new HazelcastMQCamelConfig();
    mqCamelConfig.setHazelcastMQInstance(mqInstance);

    HazelcastMQCamelComponent mqCamelComponent =
        new HazelcastMQCamelComponent();
    mqCamelComponent.setConfiguration(mqCamelConfig);

    // Create the Camel context. This could be done via a Spring XML file.
    CamelContext camelContext = new DefaultCamelContext();
    camelContext.addComponent("hazelcastmq", mqCamelComponent);

    camelContext.start();

    // Send a message to a queue.
    try (HazelcastMQContext mqContext = mqInstance.createContext()) {
      HazelcastMQProducer mqProducer = mqContext.createProducer();
      mqProducer.send("/queue/primo.test", "Hello World!");
    }

    // Create a Camel polling consumer.
    ConsumerTemplate consumerTemplate = camelContext.createConsumerTemplate();
    String body = consumerTemplate.
        receiveBody("hazelcastmq:queue:primo.test", 2000, String.class);

    if (body == null) {
      log.warn("Did not get expected message!");
    }
    else {
      log.info("Got message on queue: " + body);
    }

    consumerTemplate.stop();
    camelContext.stop();
  }
  finally {
    // Shutdown Hazelcast.
    hazelcast.getLifecycleService().shutdown();
  }
}