cocoon-cvs mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From reinh...@apache.org
Subject svn commit: r680957 - in /cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl: ./ src/main/java/org/apache/cocoon/acting/ src/main/java/org/apache/cocoon/components/jms/ src/main/java/org/apache/cocoon/samples/ src/main/resources/META-INF/cocoon/avalon/ src...
Date Wed, 30 Jul 2008 10:13:36 GMT
Author: reinhard
Date: Wed Jul 30 03:13:35 2008
New Revision: 680957

URL: http://svn.apache.org/viewvc?rev=680957&view=rev
Log:
COCOON-2229

apply Lukas' patch with some minor modifications

. don't depend on hsqldb anymore
. don't depend on self-written JMS connection code (Spring provides anything we need)
. Spring migration of the remaining code

Removed:
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnection.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnectionEventListener.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnectionEventNotifier.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnectionImpl.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnectionManager.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/JMSConnectionManagerImpl.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/samples/
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/resources/META-INF/cocoon/avalon/cocoon-jms.xconf
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/resources/org/apache/cocoon/components/jms/jms.roles
Modified:
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/pom.xml
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/acting/JMSPublisherAction.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessageListener.java
    cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessagePublisher.java

Modified: cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/pom.xml
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/pom.xml?rev=680957&r1=680956&r2=680957&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/pom.xml (original)
+++ cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/pom.xml Wed Jul 30 03:13:35 2008
@@ -7,9 +7,9 @@
   to you 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
-
+  
+  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
@@ -41,12 +41,12 @@
     </dependency>
     <dependency>
       <groupId>org.apache.cocoon</groupId>
-      <artifactId>cocoon-cron-impl</artifactId>
+      <artifactId>cocoon-eventcache-impl</artifactId>
     </dependency>
     <dependency>
-      <groupId>hsqldb</groupId>
-      <artifactId>hsqldb</artifactId>
-    </dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-jms</artifactId>
+    </dependency>    
     <dependency>
       <groupId>org.apache.geronimo.specs</groupId>
       <artifactId>geronimo-jms_1.1_spec</artifactId>

Modified: cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/acting/JMSPublisherAction.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/acting/JMSPublisherAction.java?rev=680957&r1=680956&r2=680957&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/acting/JMSPublisherAction.java
(original)
+++ cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/acting/JMSPublisherAction.java
Wed Jul 30 03:13:35 2008
@@ -19,63 +19,74 @@
 import java.util.Collections;
 import java.util.Map;
 
+import javax.jms.Destination;
+import javax.jms.JMSException;
 import javax.jms.Message;
+import javax.jms.Session;
+import javax.jms.TextMessage;
 
 import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.cocoon.components.jms.AbstractMessagePublisher;
 import org.apache.cocoon.environment.Redirector;
 import org.apache.cocoon.environment.SourceResolver;
-
-import org.apache.cocoon.components.jms.AbstractMessagePublisher;
+import org.springframework.jms.core.MessageCreator;
 
 /**
- * Action to publish TextMessages to a JMS Topic. For description of static
- * parameter configuration see {@link org.apache.cocoon.components.jms.AbstractMessagePublisher}
+ * Action to publish TextMessages to a given JMS {@link Destination}, which could be ether
a Topic or a Queue. For
+ * description of static parameter configuration see {@link org.apache.cocoon.components.jms.AbstractMessagePublisher}
  * 
- * <p>Sitemap parameters:</p>
- * <table border="1">
- *  <tbody>
- *   <tr>
- *     <th align="left">parameter</th>
- *     <th align="left">required</th>
- *     <th align="left">default</th>
- *     <th align="left">description</th>
- *   </tr>
- *  <tbody>
- *   <tr>
- *     <td>message</td>
- *     <td>required</td>
- *     <td>&nbsp;</td>
- *     <td>Content of TextMessage to publish</td>
- *   </tr>
- *  </tbody>
- * </table>
+ * <p>
+ * Sitemap parameters:
+ * </p>
+ * <table border="1"> <tbody>
+ * <tr>
+ * <th align="left">parameter</th>
+ * <th align="left">required</th>
+ * <th align="left">default</th>
+ * <th align="left">description</th>
+ * </tr>
+ * <tbody>
+ * <tr>
+ * <td>message</td>
+ * <td>required</td>
+ * <td>&nbsp;</td>
+ * <td>Content of TextMessage to publish</td>
+ * </tr>
+ * </tbody> </table>
  */
-public class JMSPublisherAction extends AbstractMessagePublisher implements Action, ThreadSafe
{
-
-    // ---------------------------------------------------- Constants
+public class JMSPublisherAction extends AbstractMessagePublisher implements Action {
 
+    /**
+     * Action parameter name.
+     */
     private static final String MESSAGE_PARAM = "message";
 
-    // ---------------------------------------------------- Lifecycle
-
-    public JMSPublisherAction () {
+    /**
+     * Default constructor.
+     */
+    public JMSPublisherAction() {
+        super();
     }
 
-    // ---------------------------------------------------- Action
-
-    public Map act(Redirector redirector,
-                   SourceResolver resolver,
-                   Map objectModel,
-                   String source,
-                   Parameters parameters) throws Exception {
+    public Map act(Redirector redirector, SourceResolver resolver, Map objectModel, String
source, Parameters parameters)
+            throws Exception {
 
         Map result = null;
         try {
-            // publish the message
-            final String event = parameters.getParameter(MESSAGE_PARAM);
-            final Message message = m_session.createTextMessage(event);
-            publishMessage(message);
+            // Get message.
+            final String msg = parameters.getParameter(MESSAGE_PARAM);
+
+            MessageCreator creator = new MessageCreator() {
+                public Message createMessage(Session session) throws JMSException {
+                    String text = msg;
+                    TextMessage message = session.createTextMessage(text);
+                    return message;
+                }
+            };
+
+            // Publish the message.
+            this.template.send(this.destination, creator);
+
             result = Collections.EMPTY_MAP;
         } catch (Exception e) {
             if (getLogger().isWarnEnabled()) {
@@ -85,5 +96,4 @@
 
         return result;
     }
-
-}
+}
\ No newline at end of file

Modified: cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessageListener.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessageListener.java?rev=680957&r1=680956&r2=680957&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessageListener.java
(original)
+++ cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessageListener.java
Wed Jul 30 03:13:35 2008
@@ -16,204 +16,134 @@
  */
 package org.apache.cocoon.components.jms;
 
+import javax.jms.Connection;
+import javax.jms.ConnectionFactory;
+import javax.jms.Destination;
 import javax.jms.JMSException;
+import javax.jms.MessageConsumer;
 import javax.jms.MessageListener;
 import javax.jms.Session;
-import javax.jms.Topic;
-import javax.jms.TopicConnection;
-import javax.jms.TopicSession;
-import javax.jms.TopicSubscriber;
-
-import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.parameters.ParameterException;
-import org.apache.avalon.framework.parameters.Parameterizable;
-import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
 
 import org.apache.cocoon.util.AbstractLogEnabled;
+import org.springframework.jms.core.JmsTemplate;
 
 /**
- * Abstract {@link javax.jms.MessageListener} implementation. 
- * Use this as a basis for concrete MessageListener implementations. 
- * When used in conjunction with the default {@link org.apache.cocoon.components.jms.JMSConnectionManager}

- * implementation this class supports automatic reconnection when the connection gets severed.
- * 
- * <p>Parameters:</p>
- * <table border="1">
- *  <tbody>
- *   <tr>
- *     <th align="left">parameter</th>
- *     <th align="left">required/default</th>
- *     <th align="left">description</th>
- *   </tr>
- *   <tr>
- *     <td valign="top">connection</td>
- *     <td valign="top">required</td>
- *     <td valign="top">
- *       Name of the connection registered with 
- *       {@link org.apache.cocoon.components.jms.JMSConnectionManager}. 
- *       This must be a topic connection.
- *     </td>
- *   </tr>
- *   <tr>
- *     <td>topic</td>
- *     <td>required</td>
- *     <td>The name of the topic to subscribe to.</td>
- *   </tr>
- *   <tr>
- *     <td>subscription-id</td>
- *     <td>(<code>null</code>)</td>
- *     <td>An optional durable subscription id.</td>
- *   </tr>
- *   <tr>
- *     <td>message-selector</td>
- *     <td>(<code>null</code>)</td>
- *     <td>An optional message selector.</td>
- *   </tr>
- *  </tbody>
- * </table>
+ * Abstract {@link javax.jms.MessageListener} implementation. Registers as listener for an
injected {@link Destination},
+ * where Destination could either be a Topic or a Queue.
  * 
  * @version $Id$
  */
-public abstract class AbstractMessageListener extends AbstractLogEnabled
-                                              implements MessageListener, Serviceable, Parameterizable,
-                                                         Initializable, Disposable, JMSConnectionEventListener
{
-
-    // ---------------------------------------------------- Constants
-
-    private static final String CONNECTION_PARAM = "connection";
-    private static final String TOPIC_PARAM = "topic";
-    private static final String SUBSCRIPTION_ID_PARAM = "subscription-id";
-    private static final String MESSAGE_SELECTOR_PARAM = "message-selector";
-
-    // ---------------------------------------------------- Instance variables
-
-    protected ServiceManager m_manager;
-
-    /* configuration */
-    protected String m_connectionName;
-    protected String m_topicName;
-    protected String m_subscriptionId;
-    protected String m_selector;
-    protected int m_acknowledgeMode;
-
-    /* connection manager component */
-    private JMSConnectionManager m_connectionManager;
-
-    /* our session */
-    private TopicSession m_session;
+public abstract class AbstractMessageListener extends AbstractLogEnabled implements MessageListener
{
 
-    /* our subscriber */
-    private TopicSubscriber m_subscriber;
-
-    // ---------------------------------------------------- Lifecycle
-
-    public AbstractMessageListener () {
-    }
-
-    public void service(ServiceManager manager) throws ServiceException {
-        m_manager = manager;
-        m_connectionManager = (JMSConnectionManager) m_manager.lookup(JMSConnectionManager.ROLE);
-    }
+    /**
+     * JMS template injected by Spring.
+     */
+    private JmsTemplate template;
 
-    public void parameterize(Parameters parameters) throws ParameterException {
+    /**
+     * JMS {@link Connection}.
+     */
+    private Connection connection;
 
-        m_connectionName = parameters.getParameter(CONNECTION_PARAM);
-        m_topicName = parameters.getParameter(TOPIC_PARAM);
+    /**
+     * JMS {@link Session}.
+     */
+    private Session session;
 
-        m_subscriptionId = parameters.getParameter(SUBSCRIPTION_ID_PARAM, null);
-        m_selector = parameters.getParameter(MESSAGE_SELECTOR_PARAM, null);
+    /**
+     * Acknowledge Mode.
+     */
+    private int acknowledgeMode = Session.DUPS_OK_ACKNOWLEDGE;
 
-    }
+    /**
+     * JMS {@link MessageConsumer}.
+     */
+    private MessageConsumer consumer;
 
     /**
-     * Registers this MessageListener as a TopicSubscriber to the configured Topic.
-     * @throws Exception
+     * JMS {@link Destination}, injected by Spring.
      */
-    public void initialize() throws Exception {
-        if (m_connectionManager instanceof JMSConnectionEventNotifier) {
-            ((JMSConnectionEventNotifier) m_connectionManager).addConnectionListener(m_connectionName,
this);
-        }
-        createSessionAndSubscriber();
-    }
+    private Destination destination;
 
-    public void dispose() {
-        closeSubscriberAndSession();
-        m_manager.release(m_connectionManager);
-    }
+    /**
+     * Subscription Id, injected by Spring.
+     */
+    private String subscriptionId;
 
-    public void onConnection(String name) {
-        if (getLogger().isInfoEnabled()) {
-            getLogger().info("Creating subscriber because of reconnection");
-        }
-        try {
-            createSessionAndSubscriber();
-        }
-        catch (JMSException e) {
-            if (getLogger().isWarnEnabled()) {
-                getLogger().warn("Reinitialization after reconnection failed", e);
+    /**
+     * Initialization method called by Spring. Starts connection, creates a session and a
consumer, depending on the
+     * injected Destination and registers as listener.
+     * 
+     * @throws JMSException In case, initialization fails.
+     */
+    public void init() throws JMSException {
+        ConnectionFactory factory = this.template.getConnectionFactory();
+        this.connection = factory.createConnection();
+        synchronized (this.connection) {
+            if (this.connection.getClientID() == null) {
+                this.connection.setClientID(this.subscriptionId);
             }
         }
+        this.connection.start();
+        this.session = this.connection.createSession(false, this.acknowledgeMode);
+        this.consumer = this.session.createConsumer(this.destination);
+        // register this class as callback
+        this.consumer.setMessageListener(this);
+        this.session.recover();
     }
 
-    public void onDisconnection(String name) {
-        if (getLogger().isInfoEnabled()) {
-            getLogger().info("Closing subscriber because of disconnection");
-        }
-        closeSubscriberAndSession();
-    }
-
-    private void createSessionAndSubscriber() throws JMSException {
-        // set the default acknowledge mode to dups
-        // concrete implementations may want to override this
-        m_acknowledgeMode = Session.DUPS_OK_ACKNOWLEDGE;
-
-        // register this MessageListener with a TopicSubscriber
-        final TopicConnection connection = (TopicConnection) m_connectionManager.getConnection(m_connectionName);
-        if (connection != null) {
-            m_session = connection.createTopicSession(false, m_acknowledgeMode);
-            final Topic topic = m_session.createTopic(m_topicName);
-            if (m_subscriptionId != null) {
-                m_subscriber = m_session.createDurableSubscriber(topic, m_subscriptionId,
m_selector, false);
-            }
-            else {
-                m_subscriber = m_session.createSubscriber(topic, m_selector, false);
-            }
-            m_subscriber.setMessageListener(this);
-            // recover in case of reconnection
-            m_session.recover();
-        }
-        else {
-            if (getLogger().isWarnEnabled()) {
-                getLogger().warn("Could not obtain JMS connection '" + m_connectionName +
"'");
+    /**
+     * Destroy method, called by Spring.
+     */
+    public void destroy() {
+        if (this.consumer != null) {
+            try {
+                this.consumer.close();
+            } catch (JMSException e) {
+                this.getLogger().error("Error closing consumer", e);
+            } finally {
+                this.consumer = null;
             }
         }
-    }
-
-    private void closeSubscriberAndSession() {
-        if (m_subscriber != null) {
+        if (this.session != null) {
             try {
-                m_subscriber.close();
+                this.session.close();
             } catch (JMSException e) {
-                getLogger().error("Error closing subscriber", e);
-            }
-            finally {
-                m_subscriber = null;
+                this.getLogger().error("Error closing session", e);
+            } finally {
+                this.session = null;
             }
         }
-        if (m_session != null) {
+        if (this.connection != null) {
             try {
-                m_session.close();
-            }
-            catch (JMSException e) {
-                getLogger().error("Error closing session", e);
-            }
-            finally {
-                m_session = null;
+                this.connection.stop();
+                this.connection.close();
+            } catch (JMSException e) {
+                this.getLogger().error("Error closing session", e);
+            } finally {
+                this.connection = null;
             }
         }
     }
-}
+
+    /**
+     * @param template the template to set
+     */
+    public final void setTemplate(JmsTemplate template) {
+        this.template = template;
+    }
+
+    /**
+     * @param subscriptionId the subscriptionId to set
+     */
+    public final void setSubscriptionId(String subscriptionId) {
+        this.subscriptionId = subscriptionId;
+    }
+
+    /**
+     * @param topic the topic to set
+     */
+    public final void setDestination(Destination destination) {
+        this.destination = destination;
+    }
+}
\ No newline at end of file

Modified: cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessagePublisher.java
URL: http://svn.apache.org/viewvc/cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessagePublisher.java?rev=680957&r1=680956&r2=680957&view=diff
==============================================================================
--- cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessagePublisher.java
(original)
+++ cocoon/trunk/blocks/cocoon-jms/cocoon-jms-impl/src/main/java/org/apache/cocoon/components/jms/AbstractMessagePublisher.java
Wed Jul 30 03:13:35 2008
@@ -16,216 +16,57 @@
  */
 package org.apache.cocoon.components.jms;
 
-import javax.jms.DeliveryMode;
+import javax.jms.Destination;
 import javax.jms.JMSException;
 import javax.jms.Message;
 import javax.jms.Session;
-import javax.jms.Topic;
-import javax.jms.TopicConnection;
-import javax.jms.TopicPublisher;
-import javax.jms.TopicSession;
-
-import org.apache.avalon.framework.activity.Disposable;
-import org.apache.avalon.framework.activity.Initializable;
-import org.apache.avalon.framework.parameters.ParameterException;
-import org.apache.avalon.framework.parameters.Parameterizable;
-import org.apache.avalon.framework.parameters.Parameters;
-import org.apache.avalon.framework.service.ServiceException;
-import org.apache.avalon.framework.service.ServiceManager;
-import org.apache.avalon.framework.service.Serviceable;
 
 import org.apache.cocoon.util.AbstractLogEnabled;
+import org.springframework.jms.core.JmsTemplate;
+import org.springframework.jms.core.MessageCreator;
 
 /**
- * Abstract JMS message publisher. Use this as a basis for components 
- * that want to publish JMS messages.
- * When used in conjunction with the default {@link org.apache.cocoon.components.jms.JMSConnectionManager}

- * implementation this class supports automatic reconnection when the connection gets severed.
- * 
- * <p>Parameters:</p>
- * <table border="1">
- *  <tbody>
- *   <tr>
- *     <th align="left">parameter</th>
- *     <th align="left">required</th>
- *     <th align="left">default</th>
- *     <th align="left">description</th>
- *   </tr>
- *   <tr>
- *     <td valign="top">connection</td>
- *     <td valign="top">yes</td>
- *     <td>&nbsp;</td>
- *     <td valign="top">
- *       Name of the connection registered with 
- *       {@link org.apache.cocoon.components.jms.JMSConnectionManager}. 
- *       This must be a topic connection.
- *     </td>
- *   </tr>
- *   <tr>
- *     <td valign="top">topic</td>
- *     <td valign="top">yes</td>
- *     <td>&nbsp;</td>
- *     <td valign="top">The name of the topic to publish messages to.</td>
- *   </tr>
- *   <tr>
- *     <td valign="top">priority</td>
- *     <td valign="top">no</td>
- *     <td>4</td>
- *     <td valign="top">the priority of the published messages</td>
- *   </tr>
- *   <tr>
- *     <td valign="top">time-to-live</td>
- *     <td valign="top">no</td>
- *     <td>10000</td>
- *     <td valign="top">the message's lifetime in milliseconds</td>
- *   </tr>
- *   <tr>
- *     <td valign="top">persistent-delivery</td>
- *     <td valign="top">no</td>
- *     <td>false</td>
- *     <td valign="top">whether to use persistent delivery mode when publishing messages</td>
- *   </tr>
- *  </tbody>
- * </table>
+ * Abstract JMS message publisher. Use this as a basis for components that want to publish
JMS messages.
  * 
  * @version $Id$
  */
-public abstract class AbstractMessagePublisher extends AbstractLogEnabled
-                                               implements Serviceable, Parameterizable, Initializable,
-                                                          Disposable, JMSConnectionEventListener
{
-
-    // ---------------------------------------------------- Constants
-
-    private static final String CONNECTION_PARAM = "connection";
-    private static final String TOPIC_PARAM = "topic";
-    private static final String PRIORITY_PARAM = "priority";
-    private static final String TIME_TO_LIVE_PARAM = "time-to-live";
-    private static final String PERSISTENT_DELIVERY_PARAM = "persistent-delivery";
-    
-    private static final int DEFAULT_PRIORITY = 4;
-    private static final int DEFAULT_TIME_TO_LIVE = 10000;
-
-    // ---------------------------------------------------- Instance variables
-
-    private ServiceManager m_manager;
-    private JMSConnectionManager m_connectionManager;
-
-    protected TopicSession m_session;
-    protected TopicPublisher m_publisher;
-
-    protected int m_mode;
-    protected int m_priority;
-    protected int m_timeToLive;
-    protected String m_topicName;
-    protected int m_acknowledgeMode;
-    protected String m_connectionName;
-
-    // ---------------------------------------------------- Lifecycle
-
-    public AbstractMessagePublisher() {
-    }
-
-    public void service(ServiceManager manager) throws ServiceException {
-        m_manager = manager;
-        m_connectionManager = (JMSConnectionManager) m_manager.lookup(JMSConnectionManager.ROLE);
-    }
-
-    public void parameterize(Parameters parameters) throws ParameterException {
-        m_connectionName = parameters.getParameter(CONNECTION_PARAM);
-        m_topicName = parameters.getParameter(TOPIC_PARAM);
-        m_priority = parameters.getParameterAsInteger(PRIORITY_PARAM, DEFAULT_PRIORITY);
-        boolean persistent = parameters.getParameterAsBoolean(PERSISTENT_DELIVERY_PARAM,
false);
-        m_mode = (persistent ? DeliveryMode.PERSISTENT : DeliveryMode.NON_PERSISTENT);
-        m_timeToLive = parameters.getParameterAsInteger(TIME_TO_LIVE_PARAM, DEFAULT_TIME_TO_LIVE);
-    }
-
-    public void initialize() throws Exception {
-        if (m_connectionManager instanceof JMSConnectionEventNotifier) {
-            ((JMSConnectionEventNotifier) m_connectionManager).addConnectionListener(m_connectionName,
this);
-        }
-        createSessionAndPublisher();
-    }
+public abstract class AbstractMessagePublisher extends AbstractLogEnabled {
 
-    public void dispose() {
-        closePublisherAndSession();
-        if (m_manager != null) {
-            if (m_connectionManager != null) {
-                m_manager.release(m_connectionManager);
-            }
-        }
-    }
+    /**
+     * JMS template injected by Spring.
+     */
+    protected JmsTemplate template;
 
-    // ---------------------------------------------------- JMSConnectionEventListener
+    /**
+     * Destination (e.g. Topic or Queue) injected by Spring.
+     */
+    protected Destination destination;
 
-    public void onConnection(String name) {
-        if (getLogger().isInfoEnabled()) {
-            getLogger().info("Creating publisher because of reconnection");
-        }
-        try {
-            createSessionAndPublisher();
-        }
-        catch (JMSException e) {
-            if (getLogger().isWarnEnabled()) {
-                getLogger().warn("Reinitialization after reconnection failed", e);
+    /**
+     * Publishes a provided message.
+     */
+    protected synchronized void publishMessage(final Message message) throws JMSException
{
+        if (this.getLogger().isDebugEnabled()) {
+            this.getLogger().debug("Publishing message '" + message + "'");
+        }
+        this.template.send(this.destination, new MessageCreator() {
+            public Message createMessage(Session session) throws JMSException {
+                return message;
             }
-        }
-    }
-
-    public void onDisconnection(String name) {
-        if (getLogger().isInfoEnabled()) {
-            getLogger().info("Closing subscriber because of disconnection");
-        }
-        closePublisherAndSession();
+        });
     }
 
-    // ---------------------------------------------------- Implementation
-
     /**
-     * Concrete classes call this method to publish messages.
+     * @param template the template to set
      */
-    protected synchronized void publishMessage(Message message) throws JMSException {
-        // TODO: discover disconnected state and queue messages until connected.
-        if (getLogger().isDebugEnabled()) {
-            getLogger().debug("Publishing message '" + message + "'");
-        }
-        m_publisher.publish(message, m_mode, m_priority, m_timeToLive);
-    }
-
-    private void createSessionAndPublisher() throws JMSException {
-        // set the default acknowledge mode
-        // concrete implementations may override this
-        m_acknowledgeMode = Session.DUPS_OK_ACKNOWLEDGE;
-
-        // create the message publisher
-        final TopicConnection connection = (TopicConnection) m_connectionManager.getConnection(m_connectionName);
-        if (connection != null) {
-            m_session = connection.createTopicSession(false, m_acknowledgeMode);
-            final Topic topic = m_session.createTopic(m_topicName);
-            m_publisher = m_session.createPublisher(topic);
-        }
-        else {
-            if (getLogger().isWarnEnabled()) {
-                getLogger().warn("Could not obtain JMS connection '" + m_connectionName +
"'");
-            }
-        }
+    public final void setTemplate(final JmsTemplate template) {
+        this.template = template;
     }
 
-    private void closePublisherAndSession() {
-        if (m_publisher != null) {
-            try {
-                m_publisher.close();
-            } catch (JMSException e) {
-                getLogger().error("Error closing publisher.", e);
-            }
-        }
-        if (m_session != null) {
-            try {
-                m_session.close();
-            }
-            catch (JMSException e) {
-                getLogger().warn("Error closing session.", e);
-            }
-        }
+    /**
+     * @param destination the destination to set
+     */
+    public final void setDestination(final Destination destination) {
+        this.destination = destination;
     }
-
-}
+}
\ No newline at end of file



Mime
View raw message