View Javadoc

1   /***
2    * 
3    * Copyright 2005 LogicBlaze, Inc.
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.impl;
19  
20  import javax.jms.Connection;
21  import javax.jms.ConnectionFactory;
22  import javax.jms.JMSException;
23  import javax.jms.MessageProducer;
24  import javax.jms.Session;
25  
26  /***
27   * A default implementation of the {@link org.logicblaze.lingo.jms.JmsProducer} which contains a reference to the
28   * connection, session and producer so that it can easily close down all its resources properly.
29   *
30   * @version $Revision: 1.1 $
31   */
32  public class DefaultJmsProducer extends JmsProducerImpl {
33  
34      private Connection connection;
35  
36      public static DefaultJmsProducer newInstance(ConnectionFactory factory) throws JMSException {
37          Connection connection = factory.createConnection();
38  
39          // lets start the connection in case that we consume on the same connection
40          connection.start();
41  
42          return newInstance(connection);
43      }
44  
45      public static DefaultJmsProducer newInstance(Connection connection) throws JMSException {
46          Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
47          MessageProducer producer = session.createProducer(null);
48          return new DefaultJmsProducer(connection, session, producer);
49      }
50  
51      public DefaultJmsProducer(Connection connection, Session session, MessageProducer producer) {
52          super(session, producer);
53          this.connection = connection;
54      }
55  
56      public Connection getConnection() {
57          return connection;
58      }
59  
60      public void close() throws JMSException {
61          super.close();
62          if (connection != null) {
63              Connection tmp = connection;
64              connection = null;
65              tmp.close();
66          }
67      }
68  }