1 package com.instantbank.component.lettersjobmdb.util;
2
3 import javax.jms.QueueConnectionFactory;
4 import javax.jms.QueueConnection;
5 import javax.jms.QueueSession;
6 import javax.jms.QueueSender;
7 import javax.jms.Queue;
8 import javax.jms.JMSException;
9 import javax.jms.MapMessage;
10 import javax.jms.Message;
11
12 import javax.naming.Context;
13 import javax.naming.InitialContext;
14 import javax.naming.NamingEnumeration;
15 import javax.naming.NameClassPair;
16 import javax.naming.CompositeName;
17 import javax.naming.NameNotFoundException;
18
19 import java.util.Hashtable;
20
21 import com.instantbank.common.utilcomponents.CommonUtil;
22 import com.instantbank.common.utilcomponents.JNDINames;
23 import com.instantbank.common.utilcomponents.Debug;
24 import com.instantbank.common.utilcomponents.LetterTemplateGlobals;
25
26
32 public class MessageSender {
33
34
37 private String initContextFactory = null;
38
39
42 private String contextProviderUrl = null;
43
44
47 private QueueConnection qConnection = null;
48
49
52 private QueueSession qSession = null;
53
54
57 private QueueSender qSender = null;
58
59
62 private Queue q = null;
63
64
67 private Debug debug;
68
69
70
79 public MessageSender(String server, int port, String queue)
80 throws JMSException {
81 debug = new Debug();
82 debug.setDebugginOn(true);
83 debug.setPreMessage("** MessageSender: ");
84
85 contextProviderUrl = "t3://" + server + ":" + port;
86 initContextFactory =
87 CommonUtil.getApplicationProperty(JNDINames.WLInitialContextFactory);
88 init(queue);
89
90 }
91
92
93
101 public MessageSender(String contextProviderUrl,
102 String initContextFactory, String queue)
103 throws JMSException {
104 debug = new Debug();
105 debug.setDebugginOn(true);
106 debug.setPreMessage("** MessageSender: ");
107
108 this.contextProviderUrl = contextProviderUrl;
109 this.initContextFactory = initContextFactory;
110 debug.println("contextProviderUrl = " + contextProviderUrl);
111 init(queue);
112
113 }
114
115
116
122 private void init(String queue) throws JMSException {
123 Context ctx = null;
124 Hashtable ht = new Hashtable();
125 QueueConnectionFactory qConnectionFactory = null;
126
127 try {
128 debug.println("queue = " + queue);
129 debug.println("initCtxFac = " + initContextFactory);
130
131 ht.put(Context.INITIAL_CONTEXT_FACTORY, initContextFactory);
132
133 ht.put(Context.PROVIDER_URL, contextProviderUrl);
134
135 ctx = new InitialContext(ht);
136
137
138
139
140 qConnectionFactory =
141 (QueueConnectionFactory)
142 ctx.lookup(LetterTemplateGlobals.LETTERS_JMS_FACTORY);
143
144 qConnection = qConnectionFactory.createQueueConnection();
145 qSession =
146 qConnection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE);
147
148 q = (Queue)ctx.lookup(queue);
149
150 qSender = qSession.createSender(q);
151
152 }
153 catch(Exception ex) {
154 debug.println(CommonUtil.stackTraceToString(ex));
155
156 try {
157 qSender.close();
158 qSession.close();
159 qConnection.close();
160 throw new JMSException("Problems constructing the " + queue + " queue");
161 }
162 catch(Exception jex) {
163 debug.println(CommonUtil.stackTraceToString(jex));
164 }
165 }
166 }
167
168
169
182 public Message buildLetterJobMessage(
183 String companyId, String temporalPath,
184 Long userId, Long jobId, String contextProvider,
185 String initContextFactory, String maxZipSize)
186 throws JMSException {
187
188 MapMessage msg = qSession.createMapMessage();
189 msg.setString("CompanyId", companyId);
190 msg.setString("WorkDir", temporalPath);
191
192 msg.setString("ContextProviderUrl", contextProvider);
193 msg.setString("InitContextFactory", initContextFactory);
194 msg.setObject("UserId", userId);
195 msg.setObject("JobKey", jobId);
196 msg.setString("MaxZipSize", maxZipSize);
197
198 return msg;
199 }
200
201
202
214 public Message buildFtpMessage(String companyId, Long userId,
215 String filePath, long jobLogId, long locId1, long locId2)
216 throws JMSException {
217 MapMessage ftpMsg = qSession.createMapMessage();
218 ftpMsg.setString("CompanyId", companyId);
219 ftpMsg.setObject("UserId", userId);
220 ftpMsg.setString("FilePath", filePath);
221 ftpMsg.setLong("JobLogId", jobLogId);
222 ftpMsg.setLong("FtpPrimLoc", locId1);
223 ftpMsg.setLong("FtpAltLoc", locId2);
224 return ftpMsg;
225 }
226
227
228
234 public void send(Message msg) throws JMSException {
235 qSender.send(msg);
236 }
237
238
239
242 public void release() {
243 try {
244 if(qSender != null) {
245 qSender.close();
246 }
247
248 if(qSession != null) {
249 qSession.close();
250 }
251
252 if(qConnection != null) {
253 qConnection.close();
254 }
255 }
256 catch(Exception jex) {
257 debug.println(CommonUtil.stackTraceToString(jex));
258 }
259 }
260
261
262
267 public String getContextProviderUrl() {
268 return this.contextProviderUrl;
269 }
270
271
272
277 public String getInitContextFactory() {
278 return this.initContextFactory;
279 }
280
281
282
288 private void debugContext(Context ctx) {
289 try {
290 String[] names = {"", "java:comp/env", "java:comp/env/jms",
291 "java:comp/env/ejb", "java:comp/env/url"};
292 for(int i = 0; i < names.length; i++) {
293 String name = names[i];
294 debug.println("====" + name + "===");
295 NamingEnumeration ne;
296 try {
297 ne = ctx.list(new CompositeName(name));
298 }
299 catch(NameNotFoundException nex) {
300 debug.println("Never mind. The name \"" + name + "\" could not be resolved");
301 continue;
302 }
303
304 while(ne.hasMore()) {
305 NameClassPair ncp = (NameClassPair)ne.next();
306 debug.println(ncp.getName() + " .=. " + ncp.getClassName());
307 }
308 }
309 }
310 catch(Exception ex) {
311 debug.println(CommonUtil.stackTraceToString(ex));
312 }
313 }
314 }
315