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.logicblaze.lingo.jms.JmsProducerConfig;
22 import org.springframework.beans.factory.DisposableBean;
23
24 import javax.jms.Connection;
25 import javax.jms.ConnectionFactory;
26 import javax.jms.Destination;
27 import javax.jms.JMSException;
28 import javax.jms.Message;
29 import javax.jms.MessageProducer;
30 import javax.jms.Session;
31
32 /***
33 * An implementation of the {@link org.logicblaze.lingo.jms.JmsProducer} which
34 * contains a reference to the connection, session and producer so that it can
35 * easily close down all its resources properly. The connection may be owned by
36 * another object and so it may not be automatically closed.
37 *
38 * @version $Revision: 1.5 $
39 */
40 public class DefaultJmsProducer implements JmsProducer, DisposableBean {
41
42 private Connection connection;
43 private Session session;
44 private MessageProducer producer;
45 private boolean ownsConnection = true;
46
47 public static DefaultJmsProducer newInstance(ConnectionFactory factory, JmsProducerConfig config) throws JMSException {
48 Connection connection = config.createConnection(factory);
49 return newInstance(connection, config, true);
50 }
51
52 public static DefaultJmsProducer newInstance(Connection connection, JmsProducerConfig config, boolean ownsConnection) throws JMSException {
53 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
54 return new DefaultJmsProducer(connection, session, config, ownsConnection);
55 }
56
57 public DefaultJmsProducer(Connection connection, Session session, MessageProducer producer, boolean ownsConnection) throws JMSException {
58 this.connection = connection;
59 this.session = session;
60 this.producer = producer;
61 this.ownsConnection = ownsConnection;
62 }
63
64 public DefaultJmsProducer(Connection connection, Session session, JmsProducerConfig config, boolean ownsConnection) throws JMSException {
65 this.connection = connection;
66 this.session = session;
67 this.ownsConnection = ownsConnection;
68 this.producer = session.createProducer(null);
69 config.configure(producer);
70 }
71
72 public Connection getConnection() {
73 return connection;
74 }
75
76 public Session getSession() {
77 return session;
78 }
79
80 public MessageProducer getMessageProducer() {
81 return producer;
82 }
83
84 public void close() throws JMSException {
85 if (producer != null) {
86 MessageProducer tmp = producer;
87 producer = null;
88 tmp.close();
89 }
90 if (session != null) {
91 Session tmp = session;
92 session = null;
93 tmp.close();
94 }
95 if (connection != null) {
96 Connection tmp = connection;
97 connection = null;
98 if (ownsConnection) {
99 tmp.close();
100 }
101 }
102 }
103
104 public void destroy() throws Exception {
105 close();
106 }
107
108 public void send(Destination destination, Message message) throws JMSException {
109 getMessageProducer().send(destination, message);
110 }
111
112 public void send(Destination destination, Message message, int deliveryMode, int priority, long timeToLive) throws JMSException {
113 getMessageProducer().send(destination, message, deliveryMode, priority, timeToLive);
114 }
115 }