View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
4    * 
5    * Licensed under the Apache License, Version 2.0 (the "License"); 
6    * you may not use this file except in compliance with the License. 
7    * You may obtain a copy of the License at 
8    * 
9    * http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS, 
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
14   * See the License for the specific language governing permissions and 
15   * limitations under the License. 
16   * 
17   **/
18  package org.logicblaze.lingo.jms;
19  
20  import javax.jms.Connection;
21  import javax.jms.ConnectionFactory;
22  import javax.jms.Destination;
23  import javax.jms.JMSException;
24  import javax.jms.MessageProducer;
25  import javax.jms.Session;
26  import javax.jms.TemporaryQueue;
27  
28  /***
29   * A simple bean of JMS producer configuration options.
30   * 
31   * @version $Revision$
32   */
33  public class JmsProducerConfig {
34  
35      private ConnectionFactory connectionFactory;
36      private String clientID;
37      private boolean transactedMode = false;
38      private int acknowledgementMode = Session.AUTO_ACKNOWLEDGE;
39      private int deliveryMode;
40      private boolean disableMessageID;
41      private boolean disableMessageTimestamp;
42      private int priority = -1;
43      private int timeToLive = 30000;
44  
45      public void configure(MessageProducer producer) throws JMSException {
46          if (deliveryMode > 0) {
47              producer.setDeliveryMode(deliveryMode);
48          }
49          producer.setDisableMessageID(disableMessageID);
50          producer.setDisableMessageTimestamp(disableMessageTimestamp);
51          if (priority >= 0) {
52              producer.setPriority(priority);
53          }
54          if (timeToLive >= 0) {
55              producer.setTimeToLive(timeToLive);
56          }
57      }
58  
59      /***
60       * Creates a new JMS connection and starts it
61       * 
62       * @throws JMSException
63       */
64      public Connection createConnection() throws JMSException {
65          if (connectionFactory == null) {
66              throw new IllegalArgumentException("Property connectionFactory not specified");
67          }
68          return createConnection(connectionFactory);
69      }
70  
71      /***
72       * Creates a new JMS connection and starts it
73       * 
74       * @throws JMSException
75       */
76      public Connection createConnection(ConnectionFactory factory) throws JMSException {
77          Connection connection = factory.createConnection();
78  
79          if (clientID != null) {
80              connection.setClientID(clientID);
81          }
82  
83          // lets start the connection in case that we consume on the same
84          // connection
85          connection.start();
86  
87          return connection;
88      }
89  
90      /***
91       * Creates a new JMS session
92       */
93      public Session createSession(Connection connection) throws JMSException {
94          return connection.createSession(transactedMode, acknowledgementMode);
95      }
96  
97      /***
98       * Creates a new producer on the given session
99       */
100     public MessageProducer createMessageProducer(Session session) throws JMSException {
101         MessageProducer producer = session.createProducer(null);
102         configure(producer);
103         return producer;
104     }
105 
106     public Destination createTemporaryDestination(Session session) throws JMSException {
107         return session.createTemporaryQueue();
108     }
109 
110     // Properties
111     // -------------------------------------------------------------------------
112     public int getDeliveryMode() {
113         return deliveryMode;
114     }
115 
116     public void setDeliveryMode(int deliveryMode) {
117         this.deliveryMode = deliveryMode;
118     }
119 
120     public boolean isDisableMessageID() {
121         return disableMessageID;
122     }
123 
124     public void setDisableMessageID(boolean disableMessageID) {
125         this.disableMessageID = disableMessageID;
126     }
127 
128     public boolean isDisableMessageTimestamp() {
129         return disableMessageTimestamp;
130     }
131 
132     public void setDisableMessageTimestamp(boolean disableMessageTimestamp) {
133         this.disableMessageTimestamp = disableMessageTimestamp;
134     }
135 
136     public int getPriority() {
137         return priority;
138     }
139 
140     public void setPriority(int priority) {
141         this.priority = priority;
142     }
143 
144     public int getTimeToLive() {
145         return timeToLive;
146     }
147 
148     public void setTimeToLive(int timeToLive) {
149         this.timeToLive = timeToLive;
150     }
151 
152     public String getClientID() {
153         return clientID;
154     }
155 
156     /***
157      * Sets the JMS connections unique clientID. This is optional unless you
158      * wish to use durable topic subscriptions. Only one connection can have a
159      * given clientID at any time.
160      */
161     public void setClientID(String clientID) {
162         this.clientID = clientID;
163     }
164 
165     public int getAcknowledgementMode() {
166         return acknowledgementMode;
167     }
168 
169     public void setAcknowledgementMode(int acknowledgementMode) {
170         this.acknowledgementMode = acknowledgementMode;
171     }
172 
173     public ConnectionFactory getConnectionFactory() {
174         return connectionFactory;
175     }
176 
177     public void setConnectionFactory(ConnectionFactory connectionFactory) {
178         this.connectionFactory = connectionFactory;
179     }
180 
181     public boolean isTransactedMode() {
182         return transactedMode;
183     }
184 
185     public void setTransactedMode(boolean transactedMode) {
186         this.transactedMode = transactedMode;
187     }
188 
189 }