You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by js...@apache.org on 2008/10/29 12:23:27 UTC

svn commit: r708879 - in /activemq/camel/trunk: camel-core/src/main/java/org/apache/camel/impl/ components/camel-jms/src/main/java/org/apache/camel/component/jms/

Author: jstrachan
Date: Wed Oct 29 04:23:27 2008
New Revision: 708879

URL: http://svn.apache.org/viewvc?rev=708879&view=rev
Log:
added a fix for CAMEL-1033 making it easy to create a JMS MessageListener for processing by Camel - along with helper methods to make a Processor from a ProducerTemplate

Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java   (with props)
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java   (with props)
Removed:
    activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/MessageListenerProcessor.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java?rev=708879&r1=708878&r2=708879&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java Wed Oct 29 04:23:27 2008
@@ -30,6 +30,7 @@
 import org.apache.camel.Producer;
 import org.apache.camel.ProducerTemplate;
 import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.CamelContextHelper;
 import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
 
 /**
@@ -46,6 +47,11 @@
     private final Map<String, Endpoint<E>> endpointCache = new HashMap<String, Endpoint<E>>();
     private Endpoint<E> defaultEndpoint;
 
+    public static DefaultProducerTemplate newInstance(CamelContext camelContext, String defaultEndpointUri) {
+        Endpoint endpoint = CamelContextHelper.getMandatoryEndpoint(camelContext, defaultEndpointUri);
+        return new DefaultProducerTemplate(camelContext, endpoint);
+    }
+
     public DefaultProducerTemplate(CamelContext context) {
         this.context = context;
     }

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java?rev=708879&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java Wed Oct 29 04:23:27 2008
@@ -0,0 +1,46 @@
+/**
+ *
+ * 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.impl;
+
+import org.apache.camel.Processor;
+import org.apache.camel.Exchange;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * A simple processor which just sends the message exchange to the default endpoint of the {@link ProducerTemplate}
+ *
+ * @version $Revision: 1.1 $
+ */
+public class ProducerTemplateProcessor implements Processor {
+    private final ProducerTemplate producerTemplate;
+
+    public ProducerTemplateProcessor(ProducerTemplate producerTemplate) {
+        this.producerTemplate = producerTemplate;
+        ObjectHelper.notNull(producerTemplate,  "producerTemplate");
+    }
+
+    public void process(Exchange exchange) throws Exception {
+        producerTemplate.send(exchange);
+    }
+
+    @Override
+    public String toString() {
+        return "ProducerTemplateProcessor[" + producerTemplate + "]";
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/ProducerTemplateProcessor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java?rev=708879&view=auto
==============================================================================
--- activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java (added)
+++ activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java Wed Oct 29 04:23:27 2008
@@ -0,0 +1,133 @@
+/**
+ *
+ * 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 org.apache.camel.CamelContext;
+import org.apache.camel.Exchange;
+import org.apache.camel.ExchangePattern;
+import org.apache.camel.Processor;
+import org.apache.camel.ProducerTemplate;
+import org.apache.camel.impl.DefaultProducerTemplate;
+import org.apache.camel.impl.ProducerTemplateProcessor;
+import org.apache.camel.util.ObjectHelper;
+import static org.apache.camel.util.ObjectHelper.wrapRuntimeCamelException;
+
+import javax.jms.Message;
+import javax.jms.MessageListener;
+
+/**
+ * A JMS {@link MessageListener} which converts an incoming JMS message into a Camel message {@link Exchange} then
+ * processing it by Camel; either using a custom {@link Processor} or if you use one of the static <code>newInstance()</code>
+ * methods such as {@link #newInstance(org.apache.camel.CamelContext, String)} or
+ * {@link #newInstance(org.apache.camel.CamelContext, org.apache.camel.ProducerTemplate)}
+ * you can send the message exchange into a Camel endpoint for processing.
+ *
+ * @version $Revision: 1.1 $
+ */
+public class CamelMessageListener implements MessageListener, Processor {
+    private final CamelContext camelContext;
+    private final Processor processor;
+    private JmsBinding binding = new JmsBinding();
+    private ExchangePattern pattern = ExchangePattern.InOnly;
+
+    /**
+     * Creates a new CamelMessageListener which will invoke a Camel endpoint
+     *
+     * @param camelContext the context to use
+     * @param endpointUri  the endpoint to invoke with the JMS message {@link Exchange}
+     * @return a newly created JMS MessageListener
+     */
+    public static CamelMessageListener newInstance(CamelContext camelContext, String endpointUri) {
+        DefaultProducerTemplate producerTemplate = DefaultProducerTemplate.newInstance(camelContext, endpointUri);
+        return newInstance(camelContext, producerTemplate);
+    }
+
+    /**
+     * Creates a new CamelMessageListener which will invoke the default Camel endpoint of the given
+     * {@link ProducerTemplate}
+     *
+     * @param camelContext the context to use
+     * @param producerTemplate  the template used to send the exchange
+     * @return a newly created JMS MessageListener
+     */
+    public static CamelMessageListener newInstance(CamelContext camelContext, ProducerTemplate producerTemplate) {
+        return new CamelMessageListener(camelContext, new ProducerTemplateProcessor(producerTemplate));
+    }
+
+    public CamelMessageListener(CamelContext camelContext, Processor processor) {
+        this.camelContext = camelContext;
+        this.processor = processor;
+        ObjectHelper.notNull(processor, "processor");
+    }
+
+    /**
+     * Processes the incoming JMS message
+     */
+    public void onMessage(Message message) {
+        try {
+            Exchange exchange = createExchange(message);
+            process(exchange);
+        } catch (Exception e) {
+            throw wrapRuntimeCamelException(e);
+        }
+    }
+
+    /**
+     * Processes the Camel message {@link Exchange}
+     */
+    public void process(Exchange exchange) throws Exception {
+        ObjectHelper.notNull(exchange, "exchange");
+        processor.process(exchange);
+    }
+
+    // Properties
+    //-------------------------------------------------------------------------
+
+    public JmsBinding getBinding() {
+        return binding;
+    }
+
+    /**
+     * Sets the JMS binding used to adapt the incoming JMS message into a Camel message {@link Exchange}
+     */
+    public void setBinding(JmsBinding binding) {
+        this.binding = binding;
+    }
+
+    public ExchangePattern getPattern() {
+        return pattern;
+    }
+
+    /**
+     * Sets the message exchange pattern that will be used on the Camel message {@link Exchange}
+     */
+    public void setPattern(ExchangePattern pattern) {
+        this.pattern = pattern;
+    }
+
+    // Implementation methods
+    //-------------------------------------------------------------------------
+
+
+    /**
+     * Returns a newly created Camel message {@link Exchange} from an inbound JMS message
+     */
+    protected Exchange createExchange(Message message) {
+        return new JmsExchange(camelContext, pattern, binding, message);
+    }
+}

Propchange: activemq/camel/trunk/components/camel-jms/src/main/java/org/apache/camel/component/jms/CamelMessageListener.java
------------------------------------------------------------------------------
    svn:eol-style = native