You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2009/03/30 08:06:37 UTC

svn commit: r759845 - in /camel/trunk/components/camel-jms/src: main/java/org/apache/camel/component/jms/ test/java/org/apache/camel/component/jms/

Author: davsclaus
Date: Mon Mar 30 06:06:36 2009
New Revision: 759845

URL: http://svn.apache.org/viewvc?rev=759845&view=rev
Log:
CAMEL-1281: ConsumerTemplate wiki examples.

Added:
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java   (with props)
    camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java   (with props)
Modified:
    camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java

Modified: camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java?rev=759845&r1=759844&r2=759845&view=diff
==============================================================================
--- camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java (original)
+++ camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java Mon Mar 30 06:06:36 2009
@@ -226,7 +226,7 @@
                 // create jms message containg the caused exception
                 answer = createJmsMessage(cause, session);
             } else {
-                ObjectHelper.notNull(camelMessage, "message body");
+                ObjectHelper.notNull(camelMessage, "message");
                 // create regular jms message using the camel message body
                 answer = createJmsMessage(exchange, camelMessage.getBody(), camelMessage.getHeaders(), session, exchange.getContext());
                 appendJmsProperties(answer, exchange, camelMessage);
@@ -400,7 +400,8 @@
         // TODO: should we throw an exception instead?
         if (LOG.isDebugEnabled()) {
             LOG.debug("Could not determine specific JmsMessage type to use from body."
-                    + " Will use generic JmsMessage. Body class: " + body.getClass().getCanonicalName());
+                    + " Will use generic JmsMessage."
+                    + (body != null ? (" Body class: " + body.getClass().getCanonicalName()) : "") );
         }
 
         // return a default message

Added: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java?rev=759845&view=auto
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java (added)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java Mon Mar 30 06:06:36 2009
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jms;
+
+import java.util.concurrent.Executors;
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentClientAcknowledge;
+
+/**
+ * @version $Revision$
+ */
+public class JmsPollingConsumerTest extends ContextTestSupport {
+
+    public void testJmsPollingConsumer() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello Claus");
+
+        // use another thread for polling consumer to demonstrate that we can wait before
+        // the message is sent to the quueue
+        Executors.newSingleThreadExecutor().execute(new Runnable() {
+            public void run() {
+                String body = consumer.receiveBody("activemq:queue.start", String.class);
+                template.sendBody("activemq:queue.foo", body + " Claus");
+            }
+        });
+
+        // wait a little to demonstrate we can start poll before we have a msg on the queue
+        Thread.sleep(50);
+
+        template.sendBody("direct:start", "Hello");
+
+        assertMockEndpointsSatisfied();
+    }
+
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+
+        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
+        camelContext.addComponent("activemq", jmsComponentClientAcknowledge(connectionFactory));
+
+        return camelContext;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("activemq:queue.start");
+
+                from("activemq:queue.foo").to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsPollingConsumerTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java?rev=759845&view=auto
==============================================================================
--- camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java (added)
+++ camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java Mon Mar 30 06:06:36 2009
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.jms;
+
+import javax.jms.ConnectionFactory;
+
+import org.apache.activemq.ActiveMQConnectionFactory;
+import org.apache.camel.CamelContext;
+import org.apache.camel.ConsumerTemplate;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import static org.apache.camel.component.jms.JmsComponent.jmsComponentClientAcknowledge;
+
+/**
+ * @version $Revision$
+ */
+public class JmsTimerBasedPollingConsumerTest extends ContextTestSupport {
+
+    public void testJmsTimerBasedPollingConsumer() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectsAscending(header("number"));
+        mock.expectedMessageCount(10);
+
+        for (int i = 0; i < 10; i++) {
+            template.sendBody("activemq:queue.inbox", "World");
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+
+        ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
+        camelContext.addComponent("activemq", jmsComponentClientAcknowledge(connectionFactory));
+
+        return camelContext;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                MyCoolBean cool = new MyCoolBean();
+                cool.setProducer(template);
+                cool.setConsumer(consumer);
+
+                from("timer://foo?period=5000").bean(cool, "someBusinessLogic");
+
+                from("activemq:queue.foo").to("mock:result");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+    // START SNIPPET: e2
+    public static class MyCoolBean {
+
+        private int count;
+        private ConsumerTemplate consumer;
+        private ProducerTemplate producer;
+
+        public void setConsumer(ConsumerTemplate consumer) {
+            this.consumer = consumer;
+        }
+
+        public void setProducer(ProducerTemplate producer) {
+            this.producer = producer;
+        }
+
+        public void someBusinessLogic() {
+            // loop to empty queue
+            while (true) {
+                // receive the message from the queue, wait at most 3 sec
+                String msg = consumer.receiveBody("activemq:queue.inbox", 3000, String.class);
+                if (msg == null) {
+                    // no more messages in queue
+                    break;
+                }
+
+                // do something with body
+                msg = "Hello " + msg;
+
+                // send it to the next queue
+                producer.sendBodyAndHeader("activemq:queue.foo", msg, "number", count++);
+            }
+        }
+    }
+    // END SNIPPET: e2
+
+}

Propchange: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jms/src/test/java/org/apache/camel/component/jms/JmsTimerBasedPollingConsumerTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date