KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JmsQueueListener.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 javax.jms.JMSException JavaDoc;
27 import javax.jms.Queue JavaDoc;
28 import javax.jms.QueueConnection JavaDoc;
29 import javax.jms.QueueConnectionFactory JavaDoc;
30 import javax.jms.QueueReceiver JavaDoc;
31 import javax.jms.QueueSession JavaDoc;
32 import javax.jms.Session JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
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 /**
43  * JmsQueueListener - Queue (P2P) Message Listener.
44  *
45  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
46  * @version $Rev: 5462 $
47  * @since 2.0
48  */

49 public class JmsQueueListener extends AbstractJmsListener {
50
51     public static final String JavaDoc module = JmsQueueListener.class.getName();
52
53     private QueueConnection JavaDoc con = null;
54     private QueueSession JavaDoc session = null;
55     private Queue JavaDoc queue = null;
56
57     private String JavaDoc jndiServer, jndiName, queueName, userName, password;
58
59     /**
60      * Creates a new JmsQueueListener - Should only be called by the JmsListenerFactory.
61      */

62     public JmsQueueListener(ServiceDispatcher dispatcher, String JavaDoc jndiServer, String JavaDoc jndiName, String JavaDoc queueName, String JavaDoc userName, String JavaDoc 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 JavaDoc e) {
76             throw new GenericServiceException("Cannot close connection(s).", e);
77         }
78     }
79
80     public synchronized void load() throws GenericServiceException {
81         try {
82             InitialContext JavaDoc jndi = JNDIContextFactory.getInitialContext(jndiServer);
83             QueueConnectionFactory JavaDoc factory = (QueueConnectionFactory JavaDoc) 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 JavaDoc) jndi.lookup(queueName);
90                 if (queue != null) {
91                     QueueReceiver JavaDoc 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 JavaDoc ne) {
104             throw new GenericServiceException("JNDI lookup problems; listener not running.", ne);
105         } catch (JMSException JavaDoc 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