1 24 package org.ofbiz.service.jms; 25 26 import java.lang.reflect.Constructor ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 import java.util.Set ; 32 33 import org.ofbiz.service.GenericServiceException; 34 import org.ofbiz.service.ServiceDispatcher; 35 import org.ofbiz.service.config.ServiceConfigUtil; 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilXml; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NodeList ; 40 41 48 public class JmsListenerFactory implements Runnable { 49 50 public static final String module = JmsListenerFactory.class.getName(); 51 52 public static final String TOPIC_LISTENER_CLASS = "org.ofbiz.service.jms.JmsTopicListener"; 53 public static final String QUEUE_LISTENER_CLASS = "org.ofbiz.service.jms.JmsQueueListener"; 54 55 protected static Map listeners = new HashMap (); 56 protected static Map servers = new HashMap (); 57 58 protected ServiceDispatcher dispatcher; 59 protected boolean firstPass = true; 60 protected int loadable = 0; 61 protected int connected = 0; 62 protected Thread thread; 63 64 public JmsListenerFactory(ServiceDispatcher dispatcher) { 65 this.dispatcher = dispatcher; 66 thread = new Thread (this, this.toString()); 67 thread.setDaemon(false); 68 thread.start(); 69 } 70 71 public void run() { 72 Debug.logInfo("Starting JMS Listener Factory Thread", module); 73 while (firstPass || connected < loadable) { 74 if (Debug.verboseOn()) Debug.logVerbose("First Pass: " + firstPass + " Connected: " + connected + " Available: " + loadable, module); 75 this.loadListeners(); 76 firstPass = false; 77 try { 78 Thread.sleep(20000); 79 } catch (InterruptedException ie) {} 80 continue; 81 } 82 Debug.logInfo("JMS Listener Factory Thread Finished; All listeners connected.", module); 83 } 84 85 private void loadListeners() { 87 try { 88 Element rootElement = ServiceConfigUtil.getXmlRootElement(); 89 NodeList nodeList = rootElement.getElementsByTagName("jms-service"); 90 91 if (Debug.verboseOn()) Debug.logVerbose("[ServiceDispatcher] : Loading JMS Listeners.", module); 92 for (int i = 0; i < nodeList.getLength(); i++) { 93 Element element = (Element ) nodeList.item(i); 94 List serverList = UtilXml.childElementList(element, "server"); 95 Iterator serverIter = serverList.iterator(); 96 97 while (serverIter.hasNext()) { 98 Element server = (Element ) serverIter.next(); 99 100 try { 101 String listenerEnabled = server.getAttribute("listen"); 102 103 if (listenerEnabled.equalsIgnoreCase("true")) { 104 StringBuffer serverKey = new StringBuffer (); 106 107 serverKey.append(server.getAttribute("jndi-server-name") + ":"); 108 serverKey.append(server.getAttribute("jndi-name") + ":"); 109 serverKey.append(server.getAttribute("topic-queue")); 110 servers.put(serverKey.toString(), server); 112 GenericMessageListener listener = loadListener(serverKey.toString(), server); 114 115 if (serverKey.length() > 0 && listener != null) 117 listeners.put(serverKey.toString(), listener); 118 } 119 } catch (GenericServiceException gse) { 120 Debug.logVerbose("Cannot load message listener (" + gse.toString() + ").", module); 121 } catch (Exception e) { 122 Debug.logError(e, "Uncaught exception.", module); 123 } 124 } 125 } 126 } catch (org.ofbiz.base.config.GenericConfigException gce) { 127 Debug.logError(gce, "Cannot get serviceengine.xml root element.", module); 128 } catch (Exception e) { 129 Debug.logError(e, "Uncaught exception.", module); 130 } 131 } 132 133 private GenericMessageListener loadListener(String serverKey, Element server) throws GenericServiceException { 134 String serverName = server.getAttribute("jndi-server-name"); 135 String jndiName = server.getAttribute("jndi-name"); 136 String queueName = server.getAttribute("topic-queue"); 137 String type = server.getAttribute("type"); 138 String userName = server.getAttribute("username"); 139 String password = server.getAttribute("password"); 140 String className = server.getAttribute("listener-class"); 141 142 if (className == null || className.length() == 0) { 143 if (type.equals("topic")) 144 className = JmsListenerFactory.TOPIC_LISTENER_CLASS; 145 else if (type.equals("queue")) 146 className = JmsListenerFactory.QUEUE_LISTENER_CLASS; 147 } 148 149 GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey); 150 151 if (listener == null) { 152 synchronized (this) { 153 listener = (GenericMessageListener) listeners.get(serverKey); 154 if (listener == null) { 155 ClassLoader cl = this.getClass().getClassLoader(); 156 Class [] paramTypes = new Class [] {ServiceDispatcher.class, String .class, String .class, String .class, String .class, String .class}; 157 Object [] params = new Object [] {dispatcher, serverName, jndiName, queueName, userName, password}; 158 159 try { 160 Class c = cl.loadClass(className); 161 Constructor cn = c.getConstructor(paramTypes); 162 163 listener = (GenericMessageListener) cn.newInstance(params); 164 } catch (Exception e) { 165 throw new GenericServiceException(e.getMessage(), e); 166 } 167 if (listener != null) 168 listeners.put(serverKey, listener); 169 loadable++; 170 } 171 } 172 173 } 174 if (listener != null && !listener.isConnected()) { 175 listener.load(); 176 if (listener.isConnected()) 177 connected++; 178 } 179 return listener; 180 } 181 182 187 public void loadListener(String serverKey) throws GenericServiceException { 188 Element server = (Element ) servers.get(serverKey); 189 190 if (server == null) 191 throw new GenericServiceException("No listener found with that serverKey."); 192 loadListener(serverKey, server); 193 } 194 195 199 public void closeListeners() throws GenericServiceException { 200 loadable = 0; 201 Set listenerKeys = listeners.keySet(); 202 Iterator listenerIterator = listenerKeys.iterator(); 203 while (listenerIterator.hasNext()) { 204 String serverKey = (String ) listenerIterator.next(); 205 closeListener(serverKey); 206 } 207 } 208 209 214 public void closeListener(String serverKey) throws GenericServiceException { 215 GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey); 216 217 if (listener == null) 218 throw new GenericServiceException("No listener found with that serverKey."); 219 listener.close(); 220 } 221 222 227 public void refreshListener(String serverKey) throws GenericServiceException { 228 GenericMessageListener listener = (GenericMessageListener) listeners.get(serverKey); 229 230 if (listener == null) 231 throw new GenericServiceException("No listener found with that serverKey."); 232 listener.refresh(); 233 } 234 235 239 public Map getJMSListeners() { 240 return new HashMap (listeners); 241 } 242 243 } 244 | Popular Tags |