[jboss-svn-commits] JBL Code SVN: r22457 - 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 15:51:24 EDT 2008
Author: tfennelly
Date: 2008-09-05 15:51:24 -0400 (Fri, 05 Sep 2008)
New Revision: 22457
Added:
labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java
labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
Removed:
labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java
Log:
More JMS stuff
Added: labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java
===================================================================
--- labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java (rev 0)
+++ labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java 2008-09-05 19:51:24 UTC (rev 22457)
@@ -0,0 +1,382 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.jms;
+
+import org.apache.log4j.Logger;
+import org.jboss.esb.deploy.DeploymentException;
+
+import javax.jms.*;
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import java.util.Properties;
+
+/**
+ * Abstract Message Handler.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class AbstractMessageHandler
+{
+ /**
+ * Logger.
+ */
+ private Logger logger;
+ /**
+ * JMS destination name.
+ */
+ private String destinationName;
+ /**
+ * JNDI Properties.
+ */
+ private Properties jndiProperties;
+ /**
+ * JMS Connection.
+ */
+ private Connection conn = null;
+ /**
+ * JMS Session.
+ */
+ private Session session = null;
+ /**
+ * JMS Destination.
+ */
+ private Destination destination = null;
+
+ public AbstractMessageHandler(final Properties jndiProperties, final String destinationName)
+ {
+ logger = Logger.getLogger(getClass());
+ this.jndiProperties = jndiProperties;
+ this.destinationName = destinationName;
+ }
+
+ /**
+ * Get the JMS Destination name.
+ *
+ * @return The JMS Destination name.
+ */
+ public final String getDestinationName()
+ {
+ return destinationName;
+ }
+
+ /**
+ * Get the JMS Session.
+ *
+ * @return The JMS Session.
+ */
+ public final Session getSession()
+ {
+ return session;
+ }
+
+ /**
+ * Get the JMS Destination.
+ *
+ * @return The JMS Destination.
+ */
+ public final Destination getDestination()
+ {
+ return destination;
+ }
+
+ /**
+ * Connect to the configured destination.
+ *
+ * @throws org.jboss.esb.deploy.DeploymentException
+ * Failed to connect.
+ */
+ protected void connect() throws DeploymentException
+ {
+ ConnectionFactory connectionFactory;
+
+ // Get the Destination ConnectionFactory...
+ try
+ {
+ 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);
+ }
+
+ // 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);
+ }
+
+ // Listen for exceptions on the connection...
+ try
+ {
+ if (this instanceof ExceptionListener)
+ {
+ conn.setExceptionListener((ExceptionListener) this);
+ } else
+ {
+ conn.setExceptionListener(new DefaultExceptionListener());
+ }
+ } catch (JMSException e)
+ {
+ throw new DeploymentException("Failed to attach an ExceptionListener.", e);
+ }
+ }
+
+ /**
+ * Get the JMS Connection factory.
+ *
+ * @return Connection Factory.
+ * @throws org.jboss.esb.deploy.DeploymentException
+ * Error getting factory.
+ */
+ private ConnectionFactory getJmsConnectionFactory() throws DeploymentException
+ {
+ ConnectionFactory factory = null;
+ Context context;
+ String connectionFactoryRuntime = jndiProperties.getProperty(ConnectionFactory.class.getName(), "ConnectionFactory");
+
+ context = getNamingContext();
+ try
+ {
+ factory = (ConnectionFactory) context.lookup(connectionFactoryRuntime);
+ } catch (NamingException e)
+ {
+ throw new DeploymentException("JNDI lookup of JMS Connection Factory [" + connectionFactoryRuntime + "] failed.", e);
+ } catch (ClassCastException e)
+ {
+ throw new DeploymentException("JNDI lookup of JMS Connection Factory failed. Class [" + connectionFactoryRuntime + "] is not an instance of [" + ConnectionFactory.class.getName() + "].", e);
+ } finally
+ {
+ if (context != null)
+ {
+ try
+ {
+ context.close();
+ } catch (NamingException ne)
+ {
+ logger.error("Failed to close Naming Context.", ne);
+ }
+ }
+ }
+
+ return factory;
+ }
+
+ /**
+ * Get the JNDI Context.
+ *
+ * @return The context.
+ * @throws org.jboss.esb.deploy.DeploymentException
+ * Error getting context.
+ */
+ private Context getNamingContext() throws DeploymentException
+ {
+ Context context;
+
+ try
+ {
+ context = new InitialContext(jndiProperties);
+ } catch (NamingException e)
+ {
+ throw new DeploymentException("Failed to load InitialContext: " + jndiProperties);
+ }
+ if (context == null)
+ {
+ throw new DeploymentException("Failed to create JMS Server JNDI context. Check that '" + Context.PROVIDER_URL + "', '" + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES + "' are correctly configured in the supplied JNDI properties.");
+ }
+
+ return context;
+ }
+
+ /**
+ * Close out the handler and all it's resources.
+ */
+ public void close()
+ {
+ try
+ {
+ if (conn != null)
+ {
+ conn.stop();
+ logger.debug("Stopping JMS Connection for connected handler '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
+ }
+ } catch (Throwable e)
+ {
+ logger.error("Failed to stop JMS connection.", e);
+ conn = null;
+ }
+ try
+ {
+ if (session != null)
+ {
+ session.close();
+ logger.debug("Closing JMS Session for connected handler '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
+ }
+ } catch (Throwable e)
+ {
+ logger.error("Failed to close JMS session.", e);
+ } finally
+ {
+ session = null;
+ }
+ try
+ {
+ if (conn != null)
+ {
+ conn.close();
+ logger.debug("Closing JMS Connection for connected handler '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
+ }
+ } catch (Throwable e)
+ {
+ logger.error("Failed to close JMS connection.", e);
+ } finally
+ {
+ conn = null;
+ }
+ destination = null;
+ logger.debug("JMS handler '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed.");
+ }
+
+ /**
+ * The Default JMS Exception Listener.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+ private class DefaultExceptionListener implements ExceptionListener
+ {
+
+ /**
+ * We want this handler to handle only one exception.
+ * It will close all existing resources and create a new instance
+ * once it successfully reconnects.
+ */
+ private boolean hasHandledOneException = false;
+
+ /**
+ * JMS Exception handler.
+ *
+ * @param e The exception.
+ */
+ public final void onException(final JMSException e)
+ {
+ synchronized (AbstractMessageHandler.DefaultExceptionListener.class)
+ {
+ if (!hasHandledOneException)
+ {
+ if (!logger.isDebugEnabled())
+ {
+ logger.warn("JMS Exception on '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed (turn on debugging for more): " + e.getMessage());
+ } else
+ {
+ logger.warn("JMS Exception on '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed.", e);
+ }
+ close();
+ while (!isConnected())
+ {
+ try
+ {
+ connect();
+ } catch (DeploymentException e1)
+ {
+ // Keep trying...
+ continue;
+ }
+
+ // We've reconnected...
+ try
+ {
+ Thread.sleep(5000);
+ } catch (InterruptedException e1)
+ {
+ if (!logger.isDebugEnabled())
+ {
+ logger.warn("Interrupted during reconnect attempt. Aborting reconnect! Will need restart to reconnect (turn on debugging for more): " + e.getMessage());
+ } else
+ {
+ logger.debug("Interrupted during reconnect attempt. Aborting reconnect! Will need restart to reconnect.", e);
+ }
+ }
+ }
+ hasHandledOneException = true;
+ }
+ }
+ }
+ }
+
+ /**
+ * Is the handler connected.
+ * @return True if the connector is connected, otherwise false.
+ */
+ public abstract boolean isConnected();
+}
Property changes on: labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageHandler.java
___________________________________________________________________
Name: svn:eol-style
+ native
Deleted: 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:42:57 UTC (rev 22456)
+++ labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java 2008-09-05 19:51:24 UTC (rev 22457)
@@ -1,422 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
- * by the @authors tag. All rights reserved.
- * See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- * This copyrighted material is made available to anyone wishing to use,
- * modify, copy, or redistribute it subject to the terms and conditions
- * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
- * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
- * You should have received a copy of the GNU Lesser General Public License,
- * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA.
- *
- * (C) 2005-2008, JBoss Inc.
- */
-package org.jboss.esb.jms;
-
-import org.apache.log4j.Logger;
-import org.jboss.esb.deploy.DeploymentException;
-
-import javax.jms.*;
-import javax.naming.Context;
-import javax.naming.InitialContext;
-import javax.naming.NamingException;
-import java.util.Properties;
-
-/**
- * Abstract JMS Destination Listener.
- * <p/>
- * Manages destination connection, close and cleanup.
- *
- * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
- */
-public abstract class AbstractMessageListener implements MessageListener
-{
- /**
- * Logger.
- */
- private Logger logger;
- /**
- * JMS destination name.
- */
- private String destinationName;
- /**
- * JNDI Properties.
- */
- private Properties jndiProperties;
- /**
- * JMS Connection.
- */
- private Connection conn = null;
- /**
- * JMS Session.
- */
- private Session session = null;
- /**
- * JMS Consumer.
- */
- private MessageConsumer messageConsumer;
- /**
- * JMS Destination.
- */
- private Destination destination = null;
- /**
- * JMS Exception listener.
- */
- private ExceptionListener exceptionListener;
-
- /**
- * Public constructor.
- * <p/>
- * Connects the listener to the destination.
- *
- * @param destinationName The destination name.
- * @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
- {
- // Get the Destination ConnectionFactory...
- try
- {
- 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);
- }
-
- // 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
- {
- if (exceptionListener != null)
- {
- conn.setExceptionListener(exceptionListener);
- } else
- {
- conn.setExceptionListener(new DefaultExceptionListener());
- }
- } catch (JMSException e)
- {
- throw new DeploymentException("Failed to attach an DefaultExceptionListener.", 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 exception listener.
- *
- * @return The exception listener.
- */
- public final ExceptionListener getExceptionListener()
- {
- return exceptionListener;
- }
-
- /**
- * Set the exception listener.
- *
- * @param exceptionListener The exception listener.
- */
- public final void setExceptionListener(final ExceptionListener exceptionListener)
- {
- this.exceptionListener = exceptionListener;
- }
-
- /**
- * Get the JMS Connection factory.
- *
- * @return Connection Factory.
- * @throws DeploymentException Error getting factory.
- */
- private ConnectionFactory getJmsConnectionFactory() throws DeploymentException
- {
- ConnectionFactory factory = null;
- Context context;
- String connectionFactoryRuntime = jndiProperties.getProperty(ConnectionFactory.class.getName(), "ConnectionFactory");
-
- context = getNamingContext();
- try
- {
- factory = (ConnectionFactory) context.lookup(connectionFactoryRuntime);
- } catch (NamingException e)
- {
- throw new DeploymentException("JNDI lookup of JMS Connection Factory [" + connectionFactoryRuntime + "] failed.", e);
- } catch (ClassCastException e)
- {
- throw new DeploymentException("JNDI lookup of JMS Connection Factory failed. Class [" + connectionFactoryRuntime + "] is not an instance of [" + ConnectionFactory.class.getName() + "].", e);
- } finally
- {
- if (context != null)
- {
- try
- {
- context.close();
- } catch (NamingException ne)
- {
- logger.error("Failed to close Naming Context.", ne);
- }
- }
- }
-
- return factory;
- }
-
- /**
- * Get the JNDI Context.
- *
- * @return The context.
- * @throws DeploymentException Error getting context.
- */
- private Context getNamingContext() throws DeploymentException
- {
- Context context;
-
- try
- {
- context = new InitialContext(jndiProperties);
- } catch (NamingException e)
- {
- throw new DeploymentException("Failed to load InitialContext: " + jndiProperties);
- }
- if (context == null)
- {
- throw new DeploymentException("Failed to create JMS Server JNDI context. Check that '" + Context.PROVIDER_URL + "', '" + Context.INITIAL_CONTEXT_FACTORY + "', '" + Context.URL_PKG_PREFIXES + "' are correctly configured in the supplied JNDI properties.");
- }
-
- return context;
- }
-
- /**
- * Close out the listener and all it's resources.
- */
- private void close()
- {
- try
- {
- if (messageConsumer != null)
- {
- messageConsumer.close();
- logger.debug("Closed JMS MessageConsumer for connected listener '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
- }
- } catch (Throwable e)
- {
- logger.error("Failed to close JMS Consumer.", e);
- }
- try
- {
- if (conn != null)
- {
- conn.stop();
- logger.debug("Stopping JMS Connection for connected listener '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
- }
- } catch (Throwable e)
- {
- logger.error("Failed to stop JMS connection.", e);
- conn = null;
- }
- try
- {
- if (session != null)
- {
- session.close();
- logger.debug("Closing JMS Session for connected listener '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
- }
- } catch (Throwable e)
- {
- logger.error("Failed to close JMS session.", e);
- } finally
- {
- session = null;
- }
- try
- {
- if (conn != null)
- {
- conn.close();
- logger.debug("Closing JMS Connection for connected listener '" + getClass().getName() + "' on JMS destination '" + destinationName + "'.");
- }
- } catch (Throwable e)
- {
- logger.error("Failed to close JMS connection.", e);
- } finally
- {
- conn = null;
- }
- destination = null;
- logger.debug("JMS listener '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed.");
- }
-
- /**
- * The Default JMS Exception Listener.
- *
- * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
- */
- private class DefaultExceptionListener implements ExceptionListener
- {
-
- /**
- * We want this listener to handle only one exception.
- * It will close all existing resources and create a new instance
- * once it successfully reconnects.
- */
- private boolean hasHandledOneException = false;
-
- /**
- * JMS Exception handler.
- *
- * @param e The exception.
- */
- public final void onException(final JMSException e)
- {
- synchronized (DefaultExceptionListener.class)
- {
- if (!hasHandledOneException)
- {
- if (!logger.isDebugEnabled())
- {
- logger.warn("JMS Exception on '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed (turn on debugging for more): " + e.getMessage());
- } else
- {
- logger.warn("JMS Exception on '" + getClass().getName() + "' on JMS destination '" + destinationName + "' is now closed.", e);
- }
- close();
- while (messageConsumer == null)
- {
- try
- {
- connect();
- } catch (DeploymentException e1)
- {
- // Keep trying...
- continue;
- }
-
- // We've reconnected...
- try
- {
- Thread.sleep(5000);
- } catch (InterruptedException e1)
- {
- if (!logger.isDebugEnabled())
- {
- logger.warn("Interrupted during reconnect attempt. Aborting reconnect! Will need restart to reconnect (turn on debugging for more): " + e.getMessage());
- } else
- {
- logger.debug("Interrupted during reconnect attempt. Aborting reconnect! Will need restart to reconnect.", e);
- }
- }
- }
- hasHandledOneException = true;
- }
- }
- }
- }
-}
Added: 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 (rev 0)
+++ labs/jbossesb/workspace/skeagh/runtime/src/main/java/org/jboss/esb/jms/AbstractMessageListener.java 2008-09-05 19:51:24 UTC (rev 22457)
@@ -0,0 +1,136 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright XXXX, Red Hat Middleware LLC, and others contributors as indicated
+ * by the @authors tag. All rights reserved.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ *
+ * (C) 2005-2008, JBoss Inc.
+ */
+package org.jboss.esb.jms;
+
+import org.apache.log4j.Logger;
+import org.jboss.esb.deploy.DeploymentException;
+
+import javax.jms.*;
+import java.util.Properties;
+
+/**
+ * Abstract JMS Listener.
+ * <p/>
+ * Manages destination connection, close and cleanup.
+ *
+ * @author <a href="mailto:tom.fennelly at jboss.com">tom.fennelly at jboss.com</a>
+ */
+public abstract class AbstractMessageListener extends AbstractMessageHandler implements MessageListener
+{
+ /**
+ * Logger.
+ */
+ private Logger logger;
+ /**
+ * JMS Listener.
+ */
+ private MessageConsumer messageConsumer;
+
+ /**
+ * Public constructor.
+ * <p/>
+ * Connects the listener to the destination.
+ *
+ * @param destinationName The destination name.
+ * @param jndiProperties The JNDI properties.
+ */
+ protected AbstractMessageListener(final String destinationName, final Properties jndiProperties)
+ {
+ super(jndiProperties, destinationName);
+ logger = Logger.getLogger(getClass());
+ }
+
+ /**
+ * Connect to the configured destination.
+ *
+ * @throws org.jboss.esb.deploy.DeploymentException Failed to connect.
+ */
+ public final void connect() throws DeploymentException
+ {
+ try
+ {
+ logger.debug("Attempting to connect listener '" + getClass().getName() + "' to JMS Destination '" + getDestinationName() + "'.");
+
+ // Call super first to connect etc...
+ super.connect();
+
+ // Bind "this" listener to the destination...
+ try
+ {
+ if (getSession() instanceof TopicSession)
+ {
+ messageConsumer = ((TopicSession) getSession()).createSubscriber((Topic) getDestination());
+ messageConsumer.setMessageListener(this);
+ } else
+ {
+ messageConsumer = ((QueueSession) getSession()).createReceiver((Queue) getDestination());
+ messageConsumer.setMessageListener(this);
+ }
+ } catch (JMSException e)
+ {
+ throw new DeploymentException("Failed to start JMS Destination Connection.", e);
+ }
+ logger.debug("Successfully connected listener '" + getClass().getName() + "' to JMS destination '" + getDestinationName() + "'.");
+ } finally
+ {
+ // If we failed to create the message listener, close and clean up....
+ if (messageConsumer == null)
+ {
+ close();
+ }
+ }
+ }
+
+ /**
+ * Close out the listener and all it's resources.
+ */
+ public void close()
+ {
+ try
+ {
+ try
+ {
+ if (messageConsumer != null)
+ {
+ messageConsumer.close();
+ logger.debug("Closed JMS MessageConsumer for connected listener '" + getClass().getName() + "' on JMS destination '" + getDestinationName() + "'.");
+ }
+ } catch (Throwable e)
+ {
+ logger.error("Failed to close JMS Consumer.", e);
+ } finally
+ {
+ messageConsumer = null;
+ }
+ } finally
+ {
+ super.close();
+ }
+ }
+
+ /**
+ * Is the handler connected.
+ * @return True if the connector is connected, otherwise false.
+ */
+ public boolean isConnected()
+ {
+ return (messageConsumer != null);
+ }
+}
More information about the jboss-svn-commits
mailing list