Title: [844] branches/servicemix-2.0/trunk/components/base/src/main/java/org/servicemix/components/jms/JmsReceiverComponent.java: SM-176 : JmsReceiverComponent does not work for a JMS 1.02 provider
Revision
844
Author
gnt
Date
2005-11-15 12:21:47 -0500 (Tue, 15 Nov 2005)

Log Message

SM-176 : JmsReceiverComponent does not work for a JMS 1.02 provider
Patch provided by Peter Smith, thanks !

Modified Paths

Diff

Modified: branches/servicemix-2.0/trunk/components/base/src/main/java/org/servicemix/components/jms/JmsReceiverComponent.java (843 => 844)

--- branches/servicemix-2.0/trunk/components/base/src/main/java/org/servicemix/components/jms/JmsReceiverComponent.java	2005-11-15 17:00:57 UTC (rev 843)
+++ branches/servicemix-2.0/trunk/components/base/src/main/java/org/servicemix/components/jms/JmsReceiverComponent.java	2005-11-15 17:21:47 UTC (rev 844)
@@ -1,34 +1,35 @@
-/** 
- * 
+/**
+ *
  * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
- * 
- * Licensed 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 
- * 
+ *
+ * Licensed 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. 
- * 
+ * 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.servicemix.components.jms;
 
 import org.springframework.beans.factory.DisposableBean;
 import org.springframework.beans.factory.InitializingBean;
 import org.springframework.jms.core.JmsTemplate;
-import org.springframework.jms.core.SessionCallback;
 
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
 import javax.jms.Destination;
-import javax.jms.JMSException;
 import javax.jms.MessageConsumer;
 import javax.jms.Session;
 
 /**
- * A component which uses a [EMAIL PROTECTED] JmsTemplate} to consume messages from a destination.
+ * A component which uses a [EMAIL PROTECTED] JmsTemplate} to consume messages from a
+ * destination.
  *
  * @version $Revision$
  */
@@ -36,28 +37,95 @@
     private JmsTemplate template;
     private String selector;
     private MessageConsumer consumer;
+    private ConnectionFactory connectionFactory;
+    private Connection connection;
+    private Session session;
 
     public void afterPropertiesSet() throws Exception {
         if (template == null) {
             throw new IllegalArgumentException("Must have a template set");
         }
-        template.execute(new SessionCallback() {
-            public Object doInJms(Session session) throws JMSException {
-                Destination defaultDestination = template.getDefaultDestination();
-                if (defaultDestination == null) {
-                    defaultDestination = template.getDestinationResolver().resolveDestinationName(session, template.getDefaultDestinationName(), template.isPubSubDomain());
-                }
-                consumer = session.createConsumer(defaultDestination, selector);
-                return null;
+        connectionFactory = template.getConnectionFactory();
+//PS Fix: START
+        /*
+         * Component code did not work for JMS 1.02 compliant provider because uses APIs
+         * that did not exist in JMS 1.02 : ConnectionFactory.createConnection,
+         * Connection.createSession
+         */
+        if (template instanceof org.springframework.jms.core.JmsTemplate102) {
+            //Note1 - would've preferred to call JmsTemplate102 methods but they are protected.
+            if (template.isPubSubDomain()) {
+                javax.jms.TopicConnection tc;
+                connection = tc = ((javax.jms.TopicConnectionFactory)connectionFactory).createTopicConnection();
+                session = tc.createTopicSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
             }
-        }, true);
+            else {
+                javax.jms.QueueConnection qc;
+                connection = qc = ((javax.jms.QueueConnectionFactory)connectionFactory).createQueueConnection();
+                session = qc.createQueueSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
+            }
+        } else { // JMS 1.1 style
+            connection = connectionFactory.createConnection();
+            session = connection.createSession(template.isSessionTransacted(), template.getSessionAcknowledgeMode());
+        }
+//PS Fix: END
+
+        Destination defaultDestination = template.getDefaultDestination();
+        if (defaultDestination == null) {
+            defaultDestination = template.getDestinationResolver().resolveDestinationName(session, template.getDefaultDestinationName(),
+                    template.isPubSubDomain());
+        }
+//PS Fix: START
+        /*
+         * Component code did not work for JMS 1.02 compliant provider because uses APIs
+         * that did not exist in JMS 1.02: Session.createConsumer
+         */
+        if (template instanceof org.springframework.jms.core.JmsTemplate102) {
+            //Note1 - would've preferred to call JmsTemplate102.createConsumer but it is protected. Code below is same.
+            //Note2 - assert that defaultDestination is correct type according to isPubSubDomain()
+            if (template.isPubSubDomain()) {
+                consumer = ((javax.jms.TopicSession)session).createSubscriber((javax.jms.Topic)defaultDestination, selector, template.isPubSubNoLocal());
+            } else {
+                consumer = ((javax.jms.QueueSession)session).createReceiver((javax.jms.Queue)defaultDestination, selector);
+            }
+        } else { // JMS 1.1 style
+            consumer = session.createConsumer(defaultDestination, selector);
+        }
+//PS Fix: END
         consumer.setMessageListener(this);
+        connection.start();
     }
 
     public void destroy() throws Exception {
+//PS Fix: START
+        /*
         if (consumer != null) {
             consumer.close();
+            consumer = null;
         }
+        if (session != null) {
+            session.close();
+            session = null;
+        }
+        if (connection != null) {
+            connection.close();
+            connection = null;
+        }
+        */
+        try {
+            if (connection != null) {
+                connection.close();
+            } else if (session != null) {
+                session.close();
+            } else if (consumer != null) {
+                consumer.close();
+            }
+        } finally {
+            connection = null;
+            session = null;
+            consumer = null;
+        }
+//PS Fix: END
     }
 
     public JmsTemplate getTemplate() {
@@ -75,5 +143,6 @@
     public void setSelector(String selector) {
         this.selector = selector;
     }
+
 }
 

Reply via email to