KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > service > jms > JmsServiceEngine


1 /*
2  * $Id: JmsServiceEngine.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.service.jms;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Map JavaDoc;
31
32 import javax.jms.JMSException JavaDoc;
33 import javax.jms.MapMessage JavaDoc;
34 import javax.jms.Message JavaDoc;
35 import javax.jms.Queue JavaDoc;
36 import javax.jms.QueueConnection JavaDoc;
37 import javax.jms.QueueConnectionFactory JavaDoc;
38 import javax.jms.QueueSender JavaDoc;
39 import javax.jms.QueueSession JavaDoc;
40 import javax.jms.Session JavaDoc;
41 import javax.jms.Topic JavaDoc;
42 import javax.jms.TopicConnection JavaDoc;
43 import javax.jms.TopicConnectionFactory JavaDoc;
44 import javax.jms.TopicPublisher JavaDoc;
45 import javax.jms.TopicSession JavaDoc;
46 import javax.jms.XAQueueConnection JavaDoc;
47 import javax.jms.XAQueueConnectionFactory JavaDoc;
48 import javax.jms.XAQueueSession JavaDoc;
49 import javax.naming.InitialContext JavaDoc;
50 import javax.naming.NamingException JavaDoc;
51 import javax.transaction.xa.XAResource JavaDoc;
52
53 import org.ofbiz.base.config.GenericConfigException;
54 import org.ofbiz.base.util.Debug;
55 import org.ofbiz.base.util.GeneralException;
56 import org.ofbiz.base.util.JNDIContextFactory;
57 import org.ofbiz.base.util.UtilXml;
58 import org.ofbiz.entity.serialize.XmlSerializer;
59 import org.ofbiz.entity.transaction.GenericTransactionException;
60 import org.ofbiz.entity.transaction.TransactionUtil;
61 import org.ofbiz.service.GenericRequester;
62 import org.ofbiz.service.GenericServiceException;
63 import org.ofbiz.service.ModelService;
64 import org.ofbiz.service.ServiceDispatcher;
65 import org.ofbiz.service.ServiceUtil;
66 import org.ofbiz.service.config.ServiceConfigUtil;
67 import org.ofbiz.service.engine.AbstractEngine;
68 import org.w3c.dom.Element JavaDoc;
69
70 /**
71  * AbstractJMSEngine
72  *
73  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
74  * @version $Rev: 5462 $
75  * @since 2.0
76  */

77 public class JmsServiceEngine extends AbstractEngine {
78
79     public static final String JavaDoc module = JmsServiceEngine.class.getName();
80
81     public JmsServiceEngine(ServiceDispatcher dispatcher) {
82         super(dispatcher);
83     }
84
85     protected Element JavaDoc getServiceElement(ModelService modelService) throws GenericServiceException {
86         Element JavaDoc rootElement = null;
87
88         try {
89             rootElement = ServiceConfigUtil.getXmlRootElement();
90         } catch (GenericConfigException e) {
91             throw new GenericServiceException("Error getting JMS Service element", e);
92         }
93
94         String JavaDoc location = this.getLocation(modelService);
95
96         Element JavaDoc serviceElement = UtilXml.firstChildElement(rootElement, "jms-service", "name", location);
97
98         if (serviceElement == null) {
99             throw new GenericServiceException("Cannot find an JMS service definition for the name [" + location + "] in the serviceengine.xml file");
100         }
101         return serviceElement;
102     }
103
104     protected Message JavaDoc makeMessage(Session JavaDoc session, ModelService modelService, Map JavaDoc context)
105         throws GenericServiceException, JMSException JavaDoc {
106         List JavaDoc outParams = modelService.getParameterNames(ModelService.OUT_PARAM, false);
107
108         if (outParams != null && outParams.size() > 0)
109             throw new GenericServiceException("JMS service cannot have required OUT parameters; no parameters will be returned.");
110         String JavaDoc xmlContext = null;
111
112         try {
113             if (Debug.verboseOn()) Debug.logVerbose("Serializing Context --> " + context, module);
114             xmlContext = XmlSerializer.serialize(context);
115         } catch (Exception JavaDoc e) {
116             throw new GenericServiceException("Cannot serialize context.", e);
117         }
118         MapMessage JavaDoc message = session.createMapMessage();
119
120         message.setString("serviceName", modelService.invoke);
121         message.setString("serviceContext", xmlContext);
122         return message;
123     }
124
125     protected List JavaDoc serverList(Element JavaDoc serviceElement) throws GenericServiceException {
126         String JavaDoc sendMode = serviceElement.getAttribute("send-mode");
127         List JavaDoc serverList = UtilXml.childElementList(serviceElement, "server");
128
129         if (sendMode.equals("none")) {
130             return new ArrayList JavaDoc();
131         } else if (sendMode.equals("all")) {
132             return serverList;
133         } else {
134             throw new GenericServiceException("Requested send mode not supported.");
135         }
136     }
137
138     protected Map JavaDoc runTopic(ModelService modelService, Map JavaDoc context, Element JavaDoc server) throws GenericServiceException {
139         String JavaDoc serverName = server.getAttribute("jndi-server-name");
140         String JavaDoc jndiName = server.getAttribute("jndi-name");
141         String JavaDoc topicName = server.getAttribute("topic-queue");
142         String JavaDoc userName = server.getAttribute("username");
143         String JavaDoc password = server.getAttribute("password");
144         String JavaDoc clientId = server.getAttribute("client-id");
145
146         InitialContext JavaDoc jndi = null;
147         TopicConnectionFactory JavaDoc factory = null;
148         TopicConnection JavaDoc con = null;
149         
150         try {
151             jndi = JNDIContextFactory.getInitialContext(serverName);
152             factory = (TopicConnectionFactory JavaDoc) jndi.lookup(jndiName);
153         } catch (GeneralException ge) {
154             throw new GenericServiceException("Problems getting JNDI InitialContext.", ge.getNested());
155         } catch (NamingException JavaDoc ne) {
156             JNDIContextFactory.clearInitialContext(serverName);
157             try {
158                 jndi = JNDIContextFactory.getInitialContext(serverName);
159                 factory = (TopicConnectionFactory JavaDoc) jndi.lookup(jndiName);
160             } catch (GeneralException ge2) {
161                 throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
162             } catch (NamingException JavaDoc ne2) {
163                 throw new GenericServiceException("JNDI lookup problems.", ne);
164             }
165         }
166         
167         try {
168             con = factory.createTopicConnection(userName, password);
169
170             if (clientId != null && clientId.length() > 1)
171                 con.setClientID(clientId);
172             con.start();
173
174             TopicSession JavaDoc session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
175             Topic JavaDoc topic = (Topic JavaDoc) jndi.lookup(topicName);
176             TopicPublisher JavaDoc publisher = session.createPublisher(topic);
177
178             // create/send the message
179
Message JavaDoc message = makeMessage(session, modelService, context);
180
181             publisher.publish(message);
182             if (Debug.verboseOn()) Debug.logVerbose("Sent JMS Message to " + topicName, module);
183
184             // close the connections
185
publisher.close();
186             session.close();
187             con.close();
188         } catch (NamingException JavaDoc ne) {
189             throw new GenericServiceException("Problems with JNDI lookup.", ne);
190         } catch (JMSException JavaDoc je) {
191             throw new GenericServiceException("JMS Internal Error.", je);
192         }
193         return ServiceUtil.returnSuccess();
194
195     }
196
197     protected Map JavaDoc runQueue(ModelService modelService, Map JavaDoc context, Element JavaDoc server) throws GenericServiceException {
198         String JavaDoc serverName = server.getAttribute("jndi-server-name");
199         String JavaDoc jndiName = server.getAttribute("jndi-name");
200         String JavaDoc queueName = server.getAttribute("topic-queue");
201         String JavaDoc userName = server.getAttribute("username");
202         String JavaDoc password = server.getAttribute("password");
203         String JavaDoc clientId = server.getAttribute("client-id");
204
205         InitialContext JavaDoc jndi = null;
206         QueueConnectionFactory JavaDoc factory = null;
207         QueueConnection JavaDoc con = null;
208         
209         try {
210             jndi = JNDIContextFactory.getInitialContext(serverName);
211             factory = (QueueConnectionFactory JavaDoc) jndi.lookup(jndiName);
212         } catch (GeneralException ge){
213             throw new GenericServiceException("Problems getting JNDI InitialContext.", ge.getNested());
214         } catch (NamingException JavaDoc ne) {
215             JNDIContextFactory.clearInitialContext(serverName);
216             try {
217                 jndi = JNDIContextFactory.getInitialContext(serverName);
218                 factory = (QueueConnectionFactory JavaDoc) jndi.lookup(jndiName);
219             } catch (GeneralException ge2) {
220                 throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
221             } catch (NamingException JavaDoc ne2) {
222                 throw new GenericServiceException("JNDI lookup problem.", ne2);
223             }
224         }
225         
226         try {
227             con = factory.createQueueConnection(userName, password);
228
229             if (clientId != null && clientId.length() > 1)
230                 con.setClientID(clientId);
231             con.start();
232
233             QueueSession JavaDoc session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
234             Queue JavaDoc queue = (Queue JavaDoc) jndi.lookup(queueName);
235             QueueSender JavaDoc sender = session.createSender(queue);
236
237             // create/send the message
238
Message JavaDoc message = makeMessage(session, modelService, context);
239
240             sender.send(message);
241             if (Debug.verboseOn()) Debug.logVerbose("Sent JMS Message to " + queueName, module);
242
243             // close the connections
244
sender.close();
245             session.close();
246             con.close();
247         } catch (NamingException JavaDoc ne) {
248             throw new GenericServiceException("Problems with JNDI lookup.", ne);
249         } catch (JMSException JavaDoc je) {
250             throw new GenericServiceException("JMS Internal Error.", je);
251         }
252         return ServiceUtil.returnSuccess();
253     }
254
255     protected Map JavaDoc runXaQueue(ModelService modelService, Map JavaDoc context, Element JavaDoc server) throws GenericServiceException {
256         String JavaDoc serverName = server.getAttribute("jndi-server-name");
257         String JavaDoc jndiName = server.getAttribute("jndi-name");
258         String JavaDoc queueName = server.getAttribute("topic-queue");
259         String JavaDoc userName = server.getAttribute("username");
260         String JavaDoc password = server.getAttribute("password");
261         String JavaDoc clientId = server.getAttribute("client-id");
262         
263         InitialContext JavaDoc jndi = null;
264         XAQueueConnectionFactory JavaDoc factory = null;
265         XAQueueConnection JavaDoc con = null;
266         
267         try {
268             jndi = JNDIContextFactory.getInitialContext(serverName);
269             factory = (XAQueueConnectionFactory JavaDoc) jndi.lookup(jndiName);
270         } catch (GeneralException ge){
271             throw new GenericServiceException("Problems getting JNDI InitialContext.", ge.getNested());
272         } catch (NamingException JavaDoc ne) {
273             JNDIContextFactory.clearInitialContext(serverName);
274             try {
275                 jndi = JNDIContextFactory.getInitialContext(serverName);
276                 factory = (XAQueueConnectionFactory JavaDoc) jndi.lookup(jndiName);
277             } catch (GeneralException ge2){
278                 throw new GenericServiceException("Problems getting JNDI InitialContext.", ge2.getNested());
279             } catch (NamingException JavaDoc ne2) {
280                 throw new GenericServiceException("JNDI lookup problems.", ne2);
281             }
282         }
283                         
284         try {
285             con = factory.createXAQueueConnection(userName, password);
286
287             if (clientId != null && clientId.length() > 1)
288                 con.setClientID(userName);
289             con.start();
290
291             // enlist the XAResource
292
XAQueueSession JavaDoc session = con.createXAQueueSession();
293             XAResource JavaDoc resource = session.getXAResource();
294
295             if (TransactionUtil.getStatus() == TransactionUtil.STATUS_ACTIVE)
296                 TransactionUtil.enlistResource(resource);
297
298             Queue JavaDoc queue = (Queue JavaDoc) jndi.lookup(queueName);
299             QueueSession JavaDoc qSession = session.getQueueSession();
300             QueueSender JavaDoc sender = qSession.createSender(queue);
301
302             // create/send the message
303
Message JavaDoc message = makeMessage(session, modelService, context);
304
305             sender.send(message);
306
307             if (TransactionUtil.getStatus() != TransactionUtil.STATUS_ACTIVE)
308                 session.commit();
309
310             Debug.logInfo("Message sent.", module);
311
312             // close the connections
313
sender.close();
314             session.close();
315             con.close();
316         } catch (GenericTransactionException gte) {
317             throw new GenericServiceException("Problems enlisting resource w/ transaction manager.", gte.getNested());
318         } catch (NamingException JavaDoc ne) {
319             throw new GenericServiceException("Problems with JNDI lookup.", ne);
320         } catch (JMSException JavaDoc je) {
321             throw new GenericServiceException("JMS Internal Error.", je);
322         }
323         return ServiceUtil.returnSuccess();
324     }
325
326     protected Map JavaDoc run(ModelService modelService, Map JavaDoc context) throws GenericServiceException {
327         Element JavaDoc serviceElement = getServiceElement(modelService);
328         List JavaDoc serverList = serverList(serviceElement);
329
330         Map JavaDoc result = new HashMap JavaDoc();
331         Iterator JavaDoc i = serverList.iterator();
332
333         while (i.hasNext()) {
334             Element JavaDoc server = (Element JavaDoc) i.next();
335             String JavaDoc serverType = server.getAttribute("type");
336
337             if (serverType.equals("topic"))
338                 result.putAll(runTopic(modelService, context, server));
339             else if (serverType.equals("queue"))
340                 result.putAll(runQueue(modelService, context, server));
341             else
342                 throw new GenericServiceException("Illegal server messaging type.");
343         }
344         return result;
345     }
346
347     /**
348      * @see org.ofbiz.service.engine.GenericEngine#runSync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
349      */

350     public Map JavaDoc runSync(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
351         return run(modelService, context);
352     }
353
354     /**
355      * @see org.ofbiz.service.engine.GenericEngine#runSyncIgnore(java.lang.String, org.ofbiz.service.ModelService, java.util.Map)
356      */

357     public void runSyncIgnore(String JavaDoc localName, ModelService modelService, Map JavaDoc context) throws GenericServiceException {
358         run(modelService, context);
359     }
360
361     /**
362      * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, org.ofbiz.service.GenericRequester, boolean)
363      */

364     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, GenericRequester requester, boolean persist) throws GenericServiceException {
365         Map JavaDoc result = run(modelService, context);
366
367         requester.receiveResult(result);
368     }
369
370     /**
371      * @see org.ofbiz.service.engine.GenericEngine#runAsync(java.lang.String, org.ofbiz.service.ModelService, java.util.Map, boolean)
372      */

373     public void runAsync(String JavaDoc localName, ModelService modelService, Map JavaDoc context, boolean persist) throws GenericServiceException {
374         run(modelService, context);
375     }
376
377 }
378
Popular Tags