1 24 package org.ofbiz.service.jms; 25 26 import javax.jms.JMSException ; 27 import javax.jms.Session ; 28 import javax.jms.Topic ; 29 import javax.jms.TopicConnection ; 30 import javax.jms.TopicConnectionFactory ; 31 import javax.jms.TopicSession ; 32 import javax.jms.TopicSubscriber ; 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.ofbiz.service.GenericServiceException; 37 import org.ofbiz.service.ServiceDispatcher; 38 import org.ofbiz.base.util.Debug; 39 import org.ofbiz.base.util.GeneralException; 40 import org.ofbiz.base.util.JNDIContextFactory; 41 42 49 public class JmsTopicListener extends AbstractJmsListener { 50 51 public static final String module = JmsTopicListener.class.getName(); 52 53 private TopicConnection con = null; 54 private TopicSession session = null; 55 private Topic topic = null; 56 57 private String jndiServer, jndiName, topicName, userName, password; 58 59 62 public JmsTopicListener(ServiceDispatcher dispatcher, String jndiServer, String jndiName, String topicName, String userName, String password) { 63 super(dispatcher); 64 this.jndiServer = jndiServer; 65 this.jndiName = jndiName; 66 this.topicName = topicName; 67 this.userName = userName; 68 this.password = password; 69 } 70 71 public void close() throws GenericServiceException { 72 try { 73 if (session != null) 74 session.close(); 75 if (con != null) 76 con.close(); 77 } catch (JMSException e) { 78 throw new GenericServiceException("Cannot close connection(s).", e); 79 } 80 } 81 82 public synchronized void load() throws GenericServiceException { 83 try { 84 InitialContext jndi = JNDIContextFactory.getInitialContext(jndiServer); 85 TopicConnectionFactory factory = (TopicConnectionFactory ) jndi.lookup(jndiName); 86 87 if (factory != null) { 88 con = factory.createTopicConnection(userName, password); 89 con.setExceptionListener(this); 90 session = con.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 91 topic = (Topic ) jndi.lookup(topicName); 92 if (topic != null) { 93 TopicSubscriber subscriber = session.createSubscriber(topic); 94 subscriber.setMessageListener(this); 95 con.start(); 96 this.setConnected(true); 97 if (Debug.infoOn()) Debug.logInfo("Listening to topic [" + topicName + "] on [" + jndiServer + "]...", module); 98 } else { 99 throw new GenericServiceException("Topic lookup failed."); 100 } 101 } else { 102 throw new GenericServiceException("Factory (broker) lookup failed."); 103 } 104 } catch (NamingException ne) { 105 throw new GenericServiceException("JNDI lookup problems; listener not running.", ne); 106 } catch (JMSException je) { 107 throw new GenericServiceException("JMS internal error; listener not running.", je); 108 } catch (GeneralException ge) { 109 throw new GenericServiceException("Problems with InitialContext; listener not running.", ge); 110 } 111 } 112 } 113 | Popular Tags |