KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JmsTopicListener.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.Session JavaDoc;
28 import javax.jms.Topic JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicConnectionFactory JavaDoc;
31 import javax.jms.TopicSession JavaDoc;
32 import javax.jms.TopicSubscriber 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  * JmsTopicListener - Topic (Pub/Sub) 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 JmsTopicListener extends AbstractJmsListener {
50
51     public static final String JavaDoc module = JmsTopicListener.class.getName();
52
53     private TopicConnection JavaDoc con = null;
54     private TopicSession JavaDoc session = null;
55     private Topic JavaDoc topic = null;
56
57     private String JavaDoc jndiServer, jndiName, topicName, userName, password;
58
59     /**
60      * Creates a new JmsTopicListener - Should only be called by the JmsListenerFactory.
61      */

62     public JmsTopicListener(ServiceDispatcher dispatcher, String JavaDoc jndiServer, String JavaDoc jndiName, String JavaDoc topicName, String JavaDoc userName, String JavaDoc 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 JavaDoc e) {
78             throw new GenericServiceException("Cannot close connection(s).", e);
79         }
80     }
81
82     public synchronized void load() throws GenericServiceException {
83         try {
84             InitialContext JavaDoc jndi = JNDIContextFactory.getInitialContext(jndiServer);
85             TopicConnectionFactory JavaDoc factory = (TopicConnectionFactory JavaDoc) 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 JavaDoc) jndi.lookup(topicName);
92                 if (topic != null) {
93                     TopicSubscriber JavaDoc 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 JavaDoc ne) {
105             throw new GenericServiceException("JNDI lookup problems; listener not running.", ne);
106         } catch (JMSException JavaDoc 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