KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: AbstractJmsListener.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.Map JavaDoc;
27
28 import javax.jms.ExceptionListener JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.MapMessage JavaDoc;
31 import javax.jms.Message JavaDoc;
32
33 import org.ofbiz.base.util.Debug;
34 import org.ofbiz.base.util.ObjectType;
35 import org.ofbiz.entity.serialize.XmlSerializer;
36 import org.ofbiz.service.*;
37
38 /**
39  * AbstractJmsListener
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 2.0
44  */

45 public abstract class AbstractJmsListener implements GenericMessageListener, ExceptionListener JavaDoc {
46
47     public static final String JavaDoc module = AbstractJmsListener.class.getName();
48
49     protected LocalDispatcher dispatcher;
50     protected boolean isConnected = false;
51
52     /**
53      * Initializes the LocalDispatcher for this service listener.
54      * @param dispatcher
55      */

56     protected AbstractJmsListener(ServiceDispatcher dispatcher) {
57         DispatchContext dctx = new DispatchContext("JMSDispatcher", null, this.getClass().getClassLoader(), null);
58         this.dispatcher = new GenericDispatcher(dctx, dispatcher);
59     }
60
61     /**
62      * Runs the service defined in the MapMessage
63      * @param message
64      * @return Map
65      */

66     protected Map JavaDoc runService(MapMessage JavaDoc message) {
67         Map JavaDoc context = null;
68         String JavaDoc serviceName = null;
69         String JavaDoc xmlContext = null;
70
71         try {
72             serviceName = message.getString("serviceName");
73             xmlContext = message.getString("serviceContext");
74             if (serviceName == null || xmlContext == null) {
75                 Debug.logError("Message received is not an OFB service message. Ignored!", module);
76                 return null;
77             }
78
79             Object JavaDoc o = XmlSerializer.deserialize(xmlContext, dispatcher.getDelegator());
80
81             if (Debug.verboseOn()) Debug.logVerbose("De-Serialized Context --> " + o, module);
82             if (ObjectType.instanceOf(o, "java.util.Map"))
83                 context = (Map JavaDoc) o;
84         } catch (JMSException JavaDoc je) {
85             Debug.logError(je, "Problems reading message.", module);
86         } catch (Exception JavaDoc e) {
87             Debug.logError(e, "Problems deserializing the service context.", module);
88         }
89
90         try {
91             ModelService model = dispatcher.getDispatchContext().getModelService(serviceName);
92             if (!model.export) {
93                 Debug.logWarning("Attempt to invoke a non-exported service: " + serviceName, module);
94                 return null;
95             }
96         } catch (GenericServiceException e) {
97             Debug.logError(e, "Unable to get ModelService for service : " + serviceName, module);
98         }
99
100         if (Debug.verboseOn()) Debug.logVerbose("Running service: " + serviceName, module);
101         
102         Map JavaDoc result = null;
103         if (context != null) {
104             try {
105                 result = dispatcher.runSync(serviceName, context);
106             } catch (GenericServiceException gse) {
107                 Debug.logError(gse, "Problems with service invocation.", module);
108             }
109         }
110         return result;
111     }
112
113     /**
114      * Receives the MapMessage and processes the service.
115      * @see javax.jms.MessageListener#onMessage(Message)
116      */

117     public void onMessage(Message JavaDoc message) {
118         MapMessage JavaDoc mapMessage = null;
119
120         if (Debug.verboseOn()) Debug.logVerbose("JMS Message Received --> " + message, module);
121         
122         if (message instanceof MapMessage JavaDoc) {
123             mapMessage = (MapMessage JavaDoc) message;
124         } else {
125             Debug.logError("Received message is not a MapMessage!", module);
126             return;
127         }
128         runService(mapMessage);
129     }
130
131     /**
132      * On exception try to re-establish connection to the JMS server.
133      * @see javax.jms.ExceptionListener#onException(JMSException)
134      */

135     public void onException(JMSException JavaDoc je) {
136         this.setConnected(false);
137         Debug.logError(je, "JMS connection exception", module);
138         while (!isConnected()) {
139             try {
140                 this.refresh();
141             } catch (GenericServiceException e) {
142                 try {
143                     Thread.sleep(10000);
144                 } catch (InterruptedException JavaDoc ie) {}
145                 continue;
146             }
147         }
148     }
149
150     /**
151      *
152      * @see org.ofbiz.service.jms.GenericMessageListener#refresh()
153      */

154     public void refresh() throws GenericServiceException {
155         this.close();
156         this.load();
157     }
158
159     /**
160      *
161      * @see org.ofbiz.service.jms.GenericMessageListener#isConnected()
162      */

163     public boolean isConnected() {
164         return this.isConnected;
165     }
166
167     /**
168      * Setter method for the connected field.
169      * @param connected
170      */

171     protected void setConnected(boolean connected) {
172         this.isConnected = connected;
173     }
174
175 }
176
Popular Tags