[jboss-svn-commits] JBL Code SVN: r22453 - labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Fri Sep 5 12:18:49 EDT 2008


Author: tfennelly
Date: 2008-09-05 12:18:48 -0400 (Fri, 05 Sep 2008)
New Revision: 22453

Modified:
   labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
Log:
rejigged the connect method

Modified: labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
===================================================================
--- labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java	2008-09-05 16:07:09 UTC (rev 22452)
+++ labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java	2008-09-05 16:18:48 UTC (rev 22453)
@@ -70,30 +70,138 @@
      * Public constructor.
      * <p/>
      * Connects the listener to the destination.
+     *
      * @param destinationName The destination name.
-     * @param jndiProperties The JNDI properties.
+     * @param jndiProperties  The JNDI properties.
      */
     protected AbstractMessageListener(final String destinationName, final Properties jndiProperties)
     {
         logger = Logger.getLogger(getClass());
         this.destinationName = destinationName;
         this.jndiProperties = jndiProperties;
+    }
+
+    /**
+     * Connect to the configured destination.
+     *
+     * @throws DeploymentException Failed to connect.
+     */
+    public final void connect() throws DeploymentException
+    {
+        ConnectionFactory connectionFactory;
+
+        logger.debug("Attempting to connect listener '" + getClass().getName() + "' to JMS Destination '" + destinationName + "'.");
+        messageConsumer = null;
         try
         {
-            if (!connect())
+            // Get the Destination ConnectionFactory...
+            try
             {
-                logger.warn("Failed to connect JMS Listener '" + getClass().getName() + "' to JMS destination '" + destinationName + "'. Turn on debug logging for more info.");
+                connectionFactory = getJmsConnectionFactory();
+            } catch (DeploymentException e)
+            {
+                throw new DeploymentException("Lookup of the JMS ConnectionFactory failed.", e);
+            } catch (ClassCastException e)
+            {
+                throw new DeploymentException("Invalid JMS ConnectionFactory config.  ConnectionFactory doesn't implement " + TopicConnectionFactory.class.getName() + ".", e);
             }
-        } catch (Throwable t)
-        {
-            logger.warn("Unexpected error while trying to connect JMS Listener '" + getClass().getName() + "' to JMS destination '" + destinationName + "'. Turn on debug logging for more info.");
-            close();
-            return;
+
+            // Create the destination connection...
+            try
+            {
+                if (connectionFactory instanceof TopicConnectionFactory)
+                {
+                    conn = ((TopicConnectionFactory) connectionFactory).createTopicConnection();
+                } else
+                {
+                    conn = ((QueueConnectionFactory) connectionFactory).createQueueConnection();
+                }
+            } catch (JMSException e)
+            {
+                throw new DeploymentException("Failed to open JMS Destination Connection.", e);
+            }
+
+            // Lookup the destination...
+            try
+            {
+                Context context = getNamingContext();
+                try
+                {
+                    destination = (Destination) context.lookup(destinationName);
+                } finally
+                {
+                    context.close();
+                }
+            } catch (DeploymentException e)
+            {
+                throw new DeploymentException("Destination lookup failed.  Destination name '" + destinationName + "'.", e);
+            } catch (NamingException e)
+            {
+                throw new DeploymentException("Destination lookup failed.  Destination name '" + destinationName + "'.", e);
+            }
+
+            // Create the Destination Session...
+            try
+            {
+                if (conn instanceof TopicConnection)
+                {
+                    session = ((TopicConnection) conn).createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
+                } else
+                {
+                    session = ((QueueConnection) conn).createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
+                }
+            } catch (JMSException e)
+            {
+                throw new DeploymentException("Destination Session creation failed.", e);
+            }
+
+            // Start the connection...
+            try
+            {
+                conn.start();
+            } catch (JMSException e)
+            {
+                throw new DeploymentException("Failed to start JMS Destination Connection.", e);
+            }
+
+            // Bind "this" listener to the destination...
+            try
+            {
+                if (session instanceof TopicSession)
+                {
+                    messageConsumer = ((TopicSession) session).createSubscriber((Topic) destination);
+                    messageConsumer.setMessageListener(this);
+                } else
+                {
+                    messageConsumer = ((QueueSession) session).createReceiver((Queue) destination);
+                    messageConsumer.setMessageListener(this);
+                }
+            } catch (JMSException e)
+            {
+                throw new DeploymentException("Failed to start JMS Destination Connection.", e);
+            }
+
+            // Listen for exceptions on the connection...
+            try
+            {
+                conn.setExceptionListener(new ExceptionListener());
+            } catch (JMSException e)
+            {
+                throw new DeploymentException("Failed to attach an ExceptionListener.", e);
+            }
+            logger.debug("Successfully connected listener '" + getClass().getName() + "' to JMS destination '" + destinationName + "'.");
+        } finally {
+            // If we failed to create the message consumer, close and clean up....
+            if(messageConsumer == null)
+            {
+                close();
+            }
         }
     }
 
     /**
      * Get the JMS Connection factory.
+     *
      * @return Connection Factory.
      * @throws DeploymentException Error getting factory.
      */
@@ -132,6 +240,7 @@
 
     /**
      * Get the JNDI Context.
+     *
      * @return The context.
      * @throws DeploymentException Error getting context.
      */
@@ -155,128 +264,6 @@
     }
 
     /**
-     * Connect to the configured destination.
-     * @return True if the connect succedded, otherwise false.
-     */
-    private boolean connect()
-    {
-        ConnectionFactory connectionFactory;
-
-        logger.debug("Attempting to connect listener '" + getClass().getName() + "' to JMS Destination '" + destinationName + "'.");
-
-        // Get the Destination ConnectionFactory...
-        try
-        {
-            connectionFactory = getJmsConnectionFactory();
-        } catch (DeploymentException e)
-        {
-            logger.debug("Lookup of the JMS ConnectionFactory failed.", e);
-            return false;
-        } catch (ClassCastException e)
-        {
-            logger.debug("Invalid JMS ConnectionFactory config.  ConnectionFactory doesn't implement " + TopicConnectionFactory.class.getName() + ".", e);
-            return false;
-        }
-
-        // Create the destination connection...
-        try
-        {
-            if (connectionFactory instanceof TopicConnectionFactory)
-            {
-                conn = ((TopicConnectionFactory) connectionFactory).createTopicConnection();
-            } else
-            {
-                conn = ((QueueConnectionFactory) connectionFactory).createQueueConnection();
-            }
-        } catch (JMSException e)
-        {
-            logger.debug("Failed to open JMS Destination Connection.", e);
-            return false;
-        }
-
-        // Lookup the destination...
-        try
-        {
-            Context context = getNamingContext();
-
-            destination = (Destination) context.lookup(destinationName);
-            context.close();
-        } catch (DeploymentException e)
-        {
-            logger.debug("Destination lookup failed.  Destination name '" + destinationName + "'.", e);
-            close();
-            return false;
-        } catch (NamingException e)
-        {
-            logger.debug("Destination lookup failed.  Destination name '" + destinationName + "'.", e);
-            close();
-            return false;
-        }
-
-        // Create the Destination Session...
-        try
-        {
-            if (conn instanceof TopicConnection)
-            {
-                session = ((TopicConnection) conn).createTopicSession(false, TopicSession.AUTO_ACKNOWLEDGE);
-            } else
-            {
-                session = ((QueueConnection) conn).createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
-            }
-        } catch (JMSException e)
-        {
-            logger.debug("Destination Session creation failed.", e);
-            close();
-            return false;
-        }
-
-        // Start the connection...
-        try
-        {
-            conn.start();
-        } catch (JMSException e)
-        {
-            logger.debug("Failed to start JMS Destination Connection.", e);
-            close();
-            return false;
-        }
-
-        // Bind "this" listener to the destination...
-        try
-        {
-            if (session instanceof TopicSession)
-            {
-                messageConsumer = ((TopicSession) session).createSubscriber((Topic) destination);
-                messageConsumer.setMessageListener(this);
-            } else
-            {
-                messageConsumer = ((QueueSession) session).createReceiver((Queue) destination);
-                messageConsumer.setMessageListener(this);
-            }
-        } catch (JMSException e)
-        {
-            logger.debug("Failed to start JMS Destination Connection.", e);
-            close();
-            return false;
-        }
-
-        // Listen for exceptions on the connection...
-        try
-        {
-            conn.setExceptionListener(new ExceptionListener());
-        } catch (JMSException e)
-        {
-            logger.debug("Failed to attach an ExceptionListener.", e);
-            close();
-            return false;
-        }
-
-        logger.debug("Successfully connected listener '" + getClass().getName() + "' to JMS destination '" + destinationName + "'.");
-
-        return true;
-    }
-
-    /**
      * Close out the listener and all it's resources.
      */
     private void close()
@@ -353,6 +340,7 @@
 
         /**
          * JMS Exception handler.
+         *
          * @param e The exception.
          */
         public final void onException(final JMSException e)
@@ -369,10 +357,17 @@
                         logger.warn("JMS Exception on '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed.", e);
                     }
                     close();
-                    while (!connect())
+                    while (messageConsumer == null)
                     {
                         try
                         {
+                            connect();
+                        } catch (DeploymentException e1)
+                        {
+                            // Keep trying...
+                        }
+                        try
+                        {
                             Thread.sleep(5000);
                         } catch (InterruptedException e1)
                         {




More information about the jboss-svn-commits mailing list