1 package org.lsst.ccs.bus.jms;
2
3
4
5
6
7
8
9
10
11
12
13 import org.lsst.ccs.utilities.logging.Logger;
14
15 import javax.jms.Connection;
16 import javax.jms.ConnectionFactory;
17 import javax.jms.Destination;
18 import javax.jms.JMSException;
19 import javax.jms.MessageConsumer;
20 import javax.jms.MessageProducer;
21 import javax.jms.Session;
22 import javax.jms.Topic;
23 import javax.naming.Context;
24 import javax.naming.InitialContext;
25 import javax.naming.NamingException;
26 import java.util.Properties;
27 import org.lsst.ccs.bootstrap.resources.BootstrapResourceUtils;
28
29
30
31
32
33
34
35
36 public class TopicSessionFactory {
37 static protected TopicSessionFactory instance = null;
38
39 public static synchronized TopicSessionFactory getSessionFactory() {
40 if (instance == null) {
41 instance = new TopicSessionFactory();
42 }
43 return instance;
44 }
45
46 static Logger log = Logger.getLogger("org.lsst.ccs.bus.jms.TopicSessionFactory");
47
48 Connection conn;
49
50 Context context;
51
52 ConnectionFactory tcf;
53
54 Topic statusTopic;
55 Topic commandTopic;
56 Topic logTopic;
57
58 public void reset() {
59 context = null;
60 tcf = null;
61 conn = null;
62 commandTopic = null;
63 logTopic = null;
64 statusTopic = null;
65
66 }
67
68 public synchronized Session getTopicSession() {
69 try {
70 if (context == null) {
71
72
73 Properties props = BootstrapResourceUtils.getBootstrapProperties("jndi");
74 context = new InitialContext(props);
75
76
77 }
78 if (tcf == null) {
79 tcf = (ConnectionFactory) context.lookup("ConnectionFactory");
80 }
81 if (conn == null) {
82 conn = tcf.createConnection();
83 conn.start();
84 }
85 if (commandTopic == null) {
86 log.info("lookup topic/control/command");
87 commandTopic = (Topic) context.lookup("topic/control/command");
88 }
89 if (statusTopic == null) {
90 log.info("lookup topic/control/status");
91 statusTopic = (Topic) context.lookup("topic/control/status");
92 }
93 if (logTopic == null) {
94 log.info("lookup topic/control/log");
95 logTopic = (Topic) context.lookup("topic/control/log");
96 }
97
98
99 Session session = conn.createSession(false,
100 Session.AUTO_ACKNOWLEDGE);
101 return session;
102 } catch (NamingException e1) {
103 e1.printStackTrace();
104 throw new RuntimeException(e1);
105 } catch (JMSException e2) {
106 e2.printStackTrace();
107 throw new RuntimeException(e2);
108 }
109 }
110
111 public synchronized Topic getCommandTopic() {
112 if (commandTopic == null) {
113 getTopicSession();
114 }
115 return commandTopic;
116 }
117
118 public synchronized Topic getStatusTopic() {
119 if (statusTopic == null) {
120 getTopicSession();
121 }
122 return statusTopic;
123 }
124
125 public synchronized Topic getLogTopic() {
126 if (logTopic == null) {
127 getTopicSession();
128 }
129 return logTopic;
130 }
131
132 public MessageProducer getCommandPublisher(Session sess) {
133 try {
134 return sess.createProducer(commandTopic);
135 } catch (JMSException e) {
136 e.printStackTrace();
137 throw new RuntimeException(e);
138 }
139 }
140
141 public MessageProducer getStatusPublisher(Session sess) {
142 try {
143 return sess.createProducer(statusTopic);
144 } catch (JMSException e) {
145 e.printStackTrace();
146 throw new RuntimeException(e);
147 }
148 }
149
150 public MessageProducer getLogPublisher(Session sess) {
151 try {
152 return sess.createProducer(logTopic);
153 } catch (JMSException e) {
154 e.printStackTrace();
155 throw new RuntimeException(e);
156 }
157 }
158
159 public MessageConsumer getCommandSubscriber(Session sess) {
160 try {
161 return sess.createConsumer(commandTopic, null, true);
162 } catch (JMSException e) {
163 e.printStackTrace();
164 throw new RuntimeException(e);
165 }
166 }
167
168 public MessageConsumer getCommandSubscriber(Session sess,
169 String messageSelector) {
170 try {
171 return sess.createConsumer(commandTopic, messageSelector, true);
172 } catch (JMSException e) {
173 e.printStackTrace();
174 throw new RuntimeException(e);
175 }
176 }
177
178 public MessageConsumer getStatusSubscriber(Session sess) {
179 try {
180 return sess.createConsumer(statusTopic, null, true);
181 } catch (JMSException e) {
182 e.printStackTrace();
183 throw new RuntimeException(e);
184 }
185 }
186
187 public MessageConsumer getStatusSubscriber(Session sess,
188 String messageSelector) {
189 try {
190 return sess.createConsumer(statusTopic, messageSelector, true);
191 } catch (JMSException e) {
192 e.printStackTrace();
193 throw new RuntimeException(e);
194 }
195 }
196
197 public MessageConsumer getLogSubscriber(Session sess, String messageSelector) {
198 try {
199 return sess.createConsumer(logTopic, messageSelector, true);
200 } catch (JMSException e) {
201 e.printStackTrace();
202 throw new RuntimeException(e);
203 }
204 }
205
206 public MessageProducer getCommandReplyPublisher(Session sess,
207 Destination dest) {
208 try {
209 return sess.createProducer(dest);
210 } catch (JMSException e) {
211 e.printStackTrace();
212 throw new RuntimeException(e);
213 }
214 }
215
216 }