On Thu, Jan 5, 2012 at 3:21 PM, wing-tung Leung
<wingtung.le...@gmail.com> wrote:
> But there is one more thing we would like to do afterwards, and that's
> capture and log the JMS message ID in some audit table.

I finally patched the Camel JmsProducer class, with a minor
modification of the "InOnly" behavior. In this case, the actually sent
JMS message, containing the filled in "JMSMessageID", is copied to the
"out" from the exchange.

This enables processors further down the line to capture the message
ID for logging.

Attached the patch. Any comments/ideas to improve this further? Any
idea if such idea could end up in the trunk? I applied it to version
2.6.0, but it can be applied to the current trunk without conflicts as
well.

Regards,

Tung
diff --git 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
index 4434252..2dc4caf 100644
--- 
a/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
+++ 
b/components/camel-jms/src/main/java/org/apache/camel/component/jms/JmsProducer.java
@@ -1,3 +1,4 @@
+
 /**
  * Licensed to the Apache Software Foundation (ASF) under one or more
  * contributor license agreements.  See the NOTICE file distributed with
@@ -306,8 +307,11 @@ public class JmsProducer extends DefaultAsyncProducer {
             }
         };
 
-        doSend(false, destinationName, destination, messageCreator, null);
-
+        MessageCaptor messageCaptor = new MessageCaptor();
+        doSend(false, destinationName, destination, messageCreator, 
messageCaptor);
+        Message sentMessage = messageCaptor.message;
+        exchange.setOut(new JmsMessage(sentMessage, null));
+        
         // after sending then set the OUT message id to the JMSMessageID so 
its identical
         setMessageId(exchange);
 
@@ -315,6 +319,13 @@ public class JmsProducer extends DefaultAsyncProducer {
         callback.done(true);
         return true;
     }
+    
+    private class MessageCaptor implements MessageSentCallback {
+       protected Message message;
+               public void sent(Session session, Message message, Destination 
destination) {
+                       this.message = message;
+               }
+    }
 
     /**
      * Sends the message using the JmsTemplate.
@@ -336,24 +347,12 @@ public class JmsProducer extends DefaultAsyncProducer {
 
         // destination should be preferred
         if (destination != null) {
-            if (inOut) {
-                if (template != null) {
-                    template.send(destination, messageCreator, callback);
-                }
-            } else {
-                if (template != null) {
-                    template.send(destination, messageCreator);
-                }
+            if (template != null) {
+                template.send(destination, messageCreator, callback);
             }
         } else if (destinationName != null) {
-            if (inOut) {
-                if (template != null) {
-                    template.send(destinationName, messageCreator, callback);
-                }
-            } else {
-                if (template != null) {
-                    template.send(destinationName, messageCreator);
-                }
+            if (template != null) {
+                template.send(destinationName, messageCreator, callback);
             }
         } else {
             throw new IllegalArgumentException("Neither destination nor 
destinationName is specified on this endpoint: " + endpoint);

Reply via email to