1 24 package org.ofbiz.service.jms; 25 26 import java.util.Map ; 27 28 import javax.jms.ExceptionListener ; 29 import javax.jms.JMSException ; 30 import javax.jms.MapMessage ; 31 import javax.jms.Message ; 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 45 public abstract class AbstractJmsListener implements GenericMessageListener, ExceptionListener { 46 47 public static final String module = AbstractJmsListener.class.getName(); 48 49 protected LocalDispatcher dispatcher; 50 protected boolean isConnected = false; 51 52 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 66 protected Map runService(MapMessage message) { 67 Map context = null; 68 String serviceName = null; 69 String 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 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 ) o; 84 } catch (JMSException je) { 85 Debug.logError(je, "Problems reading message.", module); 86 } catch (Exception 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 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 117 public void onMessage(Message message) { 118 MapMessage mapMessage = null; 119 120 if (Debug.verboseOn()) Debug.logVerbose("JMS Message Received --> " + message, module); 121 122 if (message instanceof MapMessage ) { 123 mapMessage = (MapMessage ) message; 124 } else { 125 Debug.logError("Received message is not a MapMessage!", module); 126 return; 127 } 128 runService(mapMessage); 129 } 130 131 135 public void onException(JMSException 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 ie) {} 145 continue; 146 } 147 } 148 } 149 150 154 public void refresh() throws GenericServiceException { 155 this.close(); 156 this.load(); 157 } 158 159 163 public boolean isConnected() { 164 return this.isConnected; 165 } 166 167 171 protected void setConnected(boolean connected) { 172 this.isConnected = connected; 173 } 174 175 } 176 | Popular Tags |