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 org.logicblaze.lingo.jms.JmsProducer;
21  import org.springframework.beans.factory.DisposableBean;
22  
23  import javax.jms.JMSException;
24  import javax.jms.MessageProducer;
25  import javax.jms.Session;
26  
27  /***
28   * An implementation of the {@link org.logicblaze.lingo.jms.JmsProducer} which is designed to work in
29   * message driven POJO type scenarios where the session and producer can be deduced
30   * from the current consumption thread & the session can be reused from the consumer.
31   *
32   * @version $Revision: 1.1 $
33   */
34  public class JmsProducerImpl implements JmsProducer, DisposableBean {
35      private Session session;
36      private MessageProducer producer;
37  
38      public JmsProducerImpl(Session session) throws JMSException {
39          this.session = session;
40          this.producer = session.createProducer(null);
41      }
42  
43      public JmsProducerImpl(Session session, MessageProducer producer) {
44          this.session = session;
45          this.producer = producer;
46      }
47  
48      public Session getSession() {
49          return session;
50      }
51  
52      public MessageProducer getMessageProducer() {
53          return producer;
54      }
55  
56      public void close() throws JMSException {
57          if (producer != null) {
58              MessageProducer tmp = producer;
59              producer = null;
60              tmp.close();
61          }
62          if (session != null) {
63              Session tmp = session;
64              session = null;
65              tmp.close();
66          }
67      }
68  
69      public void destroy() throws Exception {
70          close();
71      }
72  }