001    /** 
002     * 
003     * Copyright 2005 LogicBlaze, Inc.
004     * 
005     * Licensed under the Apache License, Version 2.0 (the "License"); 
006     * you may not use this file except in compliance with the License. 
007     * You may obtain a copy of the License at 
008     * 
009     * http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS, 
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
014     * See the License for the specific language governing permissions and 
015     * limitations under the License. 
016     * 
017     **/
018    package org.logicblaze.lingo.jms.impl;
019    
020    import org.logicblaze.lingo.jms.JmsProducer;
021    import org.springframework.beans.factory.DisposableBean;
022    
023    import javax.jms.JMSException;
024    import javax.jms.MessageProducer;
025    import javax.jms.Session;
026    
027    /**
028     * An implementation of the {@link org.logicblaze.lingo.jms.JmsProducer} which is designed to work in
029     * message driven POJO type scenarios where the session and producer can be deduced
030     * from the current consumption thread & the session can be reused from the consumer.
031     *
032     * @version $Revision: 1.1 $
033     */
034    public class JmsProducerImpl implements JmsProducer, DisposableBean {
035        private Session session;
036        private MessageProducer producer;
037    
038        public JmsProducerImpl(Session session) throws JMSException {
039            this.session = session;
040            this.producer = session.createProducer(null);
041        }
042    
043        public JmsProducerImpl(Session session, MessageProducer producer) {
044            this.session = session;
045            this.producer = producer;
046        }
047    
048        public Session getSession() {
049            return session;
050        }
051    
052        public MessageProducer getMessageProducer() {
053            return producer;
054        }
055    
056        public void close() throws JMSException {
057            if (producer != null) {
058                MessageProducer tmp = producer;
059                producer = null;
060                tmp.close();
061            }
062            if (session != null) {
063                Session tmp = session;
064                session = null;
065                tmp.close();
066            }
067        }
068    
069        public void destroy() throws Exception {
070            close();
071        }
072    }