1 24 package org.ofbiz.service.jms; 25 26 import javax.jms.JMSException ; 27 import javax.jms.Queue ; 28 import javax.jms.QueueConnection ; 29 import javax.jms.QueueConnectionFactory ; 30 import javax.jms.QueueReceiver ; 31 import javax.jms.QueueSession ; 32 import javax.jms.Session ; 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 JmsQueueListener extends AbstractJmsListener { 50 51 public static final String module = JmsQueueListener.class.getName(); 52 53 private QueueConnection con = null; 54 private QueueSession session = null; 55 private Queue queue = null; 56 57 private String jndiServer, jndiName, queueName, userName, password; 58 59 62 public JmsQueueListener(ServiceDispatcher dispatcher, String jndiServer, String jndiName, String queueName, String userName, String password) { 63 super(dispatcher); 64 this.jndiServer = jndiServer; 65 this.jndiName = jndiName; 66 this.queueName = queueName; 67 this.userName = userName; 68 this.password = password; 69 } 70 71 public void close() throws GenericServiceException { 72 try { 73 session.close(); 74 con.close(); 75 } catch (JMSException e) { 76 throw new GenericServiceException("Cannot close connection(s).", e); 77 } 78 } 79 80 public synchronized void load() throws GenericServiceException { 81 try { 82 InitialContext jndi = JNDIContextFactory.getInitialContext(jndiServer); 83 QueueConnectionFactory factory = (QueueConnectionFactory ) jndi.lookup(jndiName); 84 85 if (factory != null) { 86 con = factory.createQueueConnection(userName, password); 87 con.setExceptionListener(this); 88 session = con.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 89 queue = (Queue ) jndi.lookup(queueName); 90 if (queue != null) { 91 QueueReceiver receiver = session.createReceiver(queue); 92 93 receiver.setMessageListener(this); 94 con.start(); 95 this.setConnected(true); 96 Debug.logInfo("Listening to queue [" + queueName + "]...", module); 97 } else { 98 throw new GenericServiceException("Queue lookup failed."); 99 } 100 } else { 101 throw new GenericServiceException("Factory (broker) lookup failed."); 102 } 103 } catch (NamingException ne) { 104 throw new GenericServiceException("JNDI lookup problems; listener not running.", ne); 105 } catch (JMSException je) { 106 throw new GenericServiceException("JMS internal error; listener not running.", je); 107 } catch (GeneralException ge) { 108 throw new GenericServiceException("Problems with InitialContext; listener not running.", ge); 109 } 110 } 111 } 112 | Popular Tags |