activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jstrac...@apache.org
Subject svn commit: r521240 - in /activemq/camel/trunk/camel-jms/src: main/java/org/apache/camel/component/jms/JmsBinding.java main/java/org/apache/camel/component/jms/JmsEndpoint.java test/java/org/apache/camel/component/jms/JmsRouteTest.java
Date Thu, 22 Mar 2007 12:40:48 GMT
Author: jstrachan
Date: Thu Mar 22 05:40:46 2007
New Revision: 521240

URL: http://svn.apache.org/viewvc?view=rev&rev=521240
Log:
handle headers nicely in the binding and tidied up the test cases to test for them

Modified:
    activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
    activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
    activemq/camel/trunk/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTest.java

Modified: activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java?view=diff&rev=521240&r1=521239&r2=521240
==============================================================================
--- activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
(original)
+++ activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsBinding.java
Thu Mar 22 05:40:46 2007
@@ -29,6 +29,7 @@
 import java.util.Enumeration;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * A Strategy used to convert between a Camel {@JmsExchange} and {@JmsMessage} to and from
a
@@ -38,26 +39,6 @@
  */
 public class JmsBinding {
     /**
-     * Creates a JMS message from the Camel exchange and message
-     *
-     * @param session the JMS session used to create the message
-     * @return a newly created JMS Message instance containing the
-     * @throws JMSException if the message could not be created
-     */
-    public Message createJmsMessage(JmsExchange exchange, JmsMessage message, Session session)
throws JMSException {
-        Object value = message.getBody();
-        if (value instanceof String) {
-            return session.createTextMessage((String) value);
-        }
-        else if (value instanceof Serializable) {
-            return session.createObjectMessage((Serializable) value);
-        }
-        else {
-            return session.createMessage();
-        }
-    }
-
-    /**
      * Extracts the body from the JMS message
      *
      * @param exchange
@@ -90,6 +71,46 @@
     }
 
     /**
+     * Creates a JMS message from the Camel exchange and message
+     *
+     * @param session the JMS session used to create the message
+     * @return a newly created JMS Message instance containing the
+     * @throws JMSException if the message could not be created
+     */
+    public Message makeJmsMessage(JmsMessage message, Session session) throws JMSException
{
+        Message answer = createJmsMessage(message, session);
+        appendJmsProperties(answer, message, session);
+        return answer;
+    }
+
+    /**
+     * Appends the JMS headers from the Camel {@link JmsMessage}
+     */
+    protected void appendJmsProperties(Message jmsMessage, JmsMessage camelMessage, Session
session) throws JMSException {
+        Set<Map.Entry<String, Object>> entries = camelMessage.getHeaders().entrySet();
+        for (Map.Entry<String, Object> entry : entries) {
+            String headerName = entry.getKey();
+            Object headerValue = entry.getValue();
+            if (shouldOutputHeader(camelMessage, headerName, headerValue)) {
+                jmsMessage.setObjectProperty(headerName, headerValue);
+            }
+        }
+    }
+
+    protected Message createJmsMessage(JmsMessage message, Session session) throws JMSException
{
+        Object value = message.getBody();
+        if (value instanceof String) {
+            return session.createTextMessage((String) value);
+        }
+        else if (value instanceof Serializable) {
+            return session.createObjectMessage((Serializable) value);
+        }
+        else {
+            return session.createMessage();
+        }
+    }
+
+    /**
      * Extracts a {@link Map} from a {@link MapMessage}
      */
     public Map<String, Object> createMapFromMapMessage(MapMessage message) throws JMSException
{
@@ -101,5 +122,12 @@
             answer.put(name, value);
         }
         return answer;
+    }
+
+    /**
+     * Strategy to allow filtering of headers which are put on the JMS message
+     */
+    protected boolean shouldOutputHeader(JmsMessage camelMessage, String headerName, Object
headerValue) {
+        return true;
     }
 }

Modified: activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java?view=diff&rev=521240&r1=521239&r2=521240
==============================================================================
--- activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
(original)
+++ activemq/camel/trunk/camel-jms/src/main/java/org/apache/camel/component/jms/JmsEndpoint.java
Thu Mar 22 05:40:46 2007
@@ -65,7 +65,7 @@
     public void onExchange(final JmsExchange exchange) {
         template.send(destination, new MessageCreator() {
             public Message createMessage(Session session) throws JMSException {
-                Message message = getBinding().createJmsMessage(exchange, exchange.getIn(),
session);
+                Message message = getBinding().makeJmsMessage(exchange.getIn(), session);
                 if (log.isDebugEnabled()) {
                     log.debug(JmsEndpoint.this + " sending JMS message: " + message);
                 }

Modified: activemq/camel/trunk/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTest.java?view=diff&rev=521240&r1=521239&r2=521240
==============================================================================
--- activemq/camel/trunk/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTest.java
(original)
+++ activemq/camel/trunk/camel-jms/src/test/java/org/apache/camel/component/jms/JmsRouteTest.java
Thu Mar 22 05:40:46 2007
@@ -30,8 +30,8 @@
 
 import javax.jms.ConnectionFactory;
 import javax.jms.Message;
-import javax.jms.ObjectMessage;
 import javax.jms.TextMessage;
+import javax.jms.ObjectMessage;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.TimeUnit;
 
@@ -47,55 +47,46 @@
 
     public void testJmsRouteWithTextMessage() throws Exception {
         String expectedBody = "Hello there!";
+        sendExchange(expectedBody);
 
-        // now lets fire in a message
-        JmsExchange exchange = endpoint.createExchange();
-        JmsMessage in = exchange.getIn();
-        in.setBody(expectedBody);
-        in.setHeader("cheese", 123);
-        endpoint.onExchange(exchange);
-
-        // lets wait on the message being received
-        boolean received = latch.await(5, TimeUnit.SECONDS);
-        assertTrue("Did not recieve the message!", received);
-
-        assertNotNull(receivedExchange);
-
-        Object body = receivedExchange.getIn().getBody();
-        log.debug("Received body: " + body);
+        Object body = assertReceivedValidExchange(TextMessage.class);
         assertEquals("body", expectedBody, body);
-
-        Message jmsMessage = receivedExchange.getIn().getJmsMessage();
-        assertTrue("Received a JMS TextMessage: " + jmsMessage, jmsMessage instanceof TextMessage);
-
-        log.debug("Received JMS message: " + jmsMessage);
     }
 
     public void testJmsRouteWithObjectMessage() throws Exception {
         PurchaseOrder expectedBody = new PurchaseOrder("Beer", 10);
 
+        sendExchange(expectedBody);
+
+        Object body = assertReceivedValidExchange(ObjectMessage.class);
+        assertEquals("body", expectedBody, body);
+    }
+
+    protected void sendExchange(Object expectedBody) {
         // now lets fire in a message
         JmsExchange exchange = endpoint.createExchange();
         JmsMessage in = exchange.getIn();
         in.setBody(expectedBody);
         in.setHeader("cheese", 123);
         endpoint.onExchange(exchange);
+    }
 
+    protected Object assertReceivedValidExchange(Class type) throws Exception {
         // lets wait on the message being received
         boolean received = latch.await(5, TimeUnit.SECONDS);
         assertTrue("Did not recieve the message!", received);
 
         assertNotNull(receivedExchange);
+        JmsMessage receivedMessage = receivedExchange.getIn();
 
-        Object body = receivedExchange.getIn().getBody();
+        assertEquals("cheese header", 123, receivedMessage.getHeader("cheese"));
+        Object body = receivedMessage.getBody();
         log.debug("Received body: " + body);
-
-        assertEquals("body", expectedBody, body);
-
-        Message jmsMessage = receivedExchange.getIn().getJmsMessage();
-        assertTrue("Received a JMS TextMessage: " + jmsMessage, jmsMessage instanceof ObjectMessage);
+        Message jmsMessage = receivedMessage.getJmsMessage();
+        assertTrue("Expected an instance of " + type.getName() + " but was " + jmsMessage,
type.isInstance(jmsMessage));
 
         log.debug("Received JMS message: " + jmsMessage);
+        return body;
     }
 
     @Override



Mime
View raw message