View Javadoc

1   /**
2    * Copyright 2005-2006 the original author or authors.
3    *
4    * Licensed under the Gnu General Pubic License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with
6    * the License. You may obtain a copy of the License at
7    *
8    *      http://www.opensource.org/licenses/gpl-license.php
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13   * See the Gnu General Public License for more details.
14   */
15  package org.figure8.join.core.messaging;
16  
17  import org.figure8.join.util.LogUtil;
18  import org.figure8.join.core.InfrastructureException;
19  
20  import org.apache.commons.logging.Log;
21  import org.springframework.jms.core.MessageCreator;
22  import org.springframework.jms.core.support.JmsGatewaySupport;
23  
24  import javax.jms.Message;
25  import javax.jms.Session;
26  import javax.jms.Destination;
27  import javax.jms.JMSException;
28  
29  import java.io.Serializable;
30  import java.util.Properties;
31  import java.util.Enumeration;
32  /**
33   * This is a base class for implementations needing to send JMS messages over
34   * a JMS destination. How to acquire detinations mechanism is left abstract.
35   * This base class also extends Spring container JMS support class in order
36   * to use Spring's features such as JmsTemplates ...
37   * @author <a href="mailto:laurent.broudoux@free.fr">Laurent Broudoux</a>
38   * @version $Revision: 1.2 $
39   */
40  public abstract class JMSProducerBean extends JmsGatewaySupport{
41  
42     // Attributes ---------------------------------------------------------------
43  
44     /** Commons logger. */
45     protected Log log;
46  
47  
48     // Constructors -------------------------------------------------------------
49  
50     /** Default constructor. */
51     public JMSProducerBean(){
52        log = LogUtil.getLog(this.getClass());
53     }
54  
55  
56     // Public -------------------------------------------------------------------
57  
58     /**
59      * Send the given text as a text message onto destination.
60      * @param text Text to send as a JMS message
61      */
62     public void sendTextMessage(final String text){
63        try{
64           // Send a simple text message.
65           getJmsTemplate().send(getDestination(), new MessageCreator() {
66              public Message createMessage(Session session) throws JMSException{
67                 return session.createTextMessage(text);
68              }
69           });
70        }
71        catch (Exception e){
72           log.error("Exception in sendTextMessage()!", e);
73           throw new InfrastructureException(e);
74        }
75     }
76  
77     /**
78      * Send the given text as a text message onto destination. Add given
79      * properties as String properties on the sent message.
80      * @param text Text to send as a JMS Message
81      * @param properties String properties to add to JMS Message
82      */
83     public void sendTextMessage(final String text, final Properties properties){
84        try{
85           // Send a text message with properties.
86           getJmsTemplate().send(getDestination(), new MessageCreator() {
87              public Message createMessage(Session session) throws JMSException{
88                 Message msg = session.createTextMessage(text);
89                 addProperties(msg, properties);
90                 return msg;
91              }
92           });
93        }
94        catch (Exception e){
95           log.error("Exception is sendTextMessage() with properties!", e);
96           throw new InfrastructureException(e);
97        }
98     }
99  
100    /**
101     * Send the given {@link Serializable} object as an object message onto destination.
102     * @param obj The serializable object to send as a JMS Message
103     */
104    public void sendObjectMessage(final Serializable obj){
105       try{
106          // Send a simple object message.
107          getJmsTemplate().send(getDestination(), new MessageCreator() {
108             public Message createMessage(Session session) throws JMSException{
109                return session.createObjectMessage(obj);
110             }
111          });
112       }
113       catch (Exception e){
114          log.error("Exception in sendObjectMessage()!", e);
115          throw new InfrastructureException(e);
116       }
117    }
118 
119    /**
120     * Send the given {@link Serializable} object as an object message.
121     * Add given properties as String properties on the sent message.
122     * @param obj The serializable object to send as a JMS Message.
123     * @param properties String properties to add to JMS Message
124     */
125    public void sendObjectMessage(final Serializable obj, final Properties properties){
126       try{
127          // Send an object message with properties.
128          getJmsTemplate().send(getDestination(), new MessageCreator() {
129             public Message createMessage(Session session) throws JMSException{
130                Message msg = session.createObjectMessage(obj);
131                addProperties(msg, properties);
132                return msg;
133             }
134          });
135       }
136       catch (Exception e){
137          log.error("Exception in sendObjectMessage() with properties!", e);
138          throw new InfrastructureException(e);
139       }
140    }
141    
142    /**
143     * Abstract method that should be implemented by subclasses. This
144     * method is left abstract because this base class does not impose a
145     * mechanism for acquiring a JMS Destination. This Destination may be
146     * injected as a dependency by Spring or retrieved using a specific
147     * resolver for remote destinations ...
148     * @return The JMS destination to use when sending messages.
149     */
150    public abstract Destination getDestination();
151 
152 
153    // Private ------------------------------------------------------------------
154 
155    /**
156     * Utility method for adding string properties onto a JMS message.
157     * @throws JMSException if properties cannot be added to message.
158     */
159    private void addProperties(Message msg, Properties properties) throws JMSException{
160       Enumeration keys = properties.keys();
161       while (keys != null && keys.hasMoreElements()){
162          String key = (String)keys.nextElement();
163          String value = properties.getProperty(key);
164          msg.setStringProperty(key, value);
165       }
166    }
167 }