KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mr > api > simple > Subscriber


1 /*
2  * Copyright 2002 by
3  * <a HREF="http://www.coridan.com">Coridan</a>
4  * <a HREF="mailto: support@coridan.com ">support@coridan.com</a>
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with the
8  * License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is "MantaRay" (TM).
17  *
18  * The Initial Developer of the Original Code is Uri Schneider.
19  * Portions created by the Initial Developer are Copyright (C) 2006
20  * Coridan Inc. All Rights Reserved.
21  *
22  * Contributor(s): all the names of the contributors are added in the source
23  * code where applicable.
24  *
25  * Alternatively, the contents of this file may be used under the terms of the
26  * LGPL license (the "GNU LESSER GENERAL PUBLIC LICENSE"), in which case the
27  * provisions of LGPL are applicable instead of those above. If you wish to
28  * allow use of your version of this file only under the terms of the LGPL
29  * License and not to allow others to use your version of this file under
30  * the MPL, indicate your decision by deleting the provisions above and
31  * replace them with the notice and other provisions required by the LGPL.
32  * If you do not delete the provisions above, a recipient may use your version
33  * of this file under either the MPL or the GNU LESSER GENERAL PUBLIC LICENSE.
34  
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Lesser General Public License as published by the Free Software Foundation;
39  * either version 2.1 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
43  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
44  * License for more details.
45  */

46 package org.mr.api.simple;
47
48 import org.mr.api.jms.MantaTopicConnectionFactory;
49
50 /**
51  * Subscriber.java
52  *
53  * An object used to receive messages from a specific topic. Use the
54  * <code>SimpleAPI</code> factory to create one of these, and then subscribe a
55  * listener using the {@link #subscribe subscribe} method.<p>
56  * <b>Concurrency</b><p>
57  * This object is not safe for use by multiple concurrent threads. Lock it if
58  * want concurrency.
59  *
60  * Created: Thu Jun 24 19:23:55 2004
61  *
62  * @author Uri Schneider
63  * @version 1.0
64  */

65 public class Subscriber {
66     private String JavaDoc topicName;
67     private javax.jms.TopicSubscriber JavaDoc subscriber;
68     private MessageListener listener;
69
70     Subscriber(String JavaDoc topicName) throws SimpleException {
71         this.topicName = topicName;
72
73         javax.jms.TopicConnectionFactory JavaDoc factory;
74         javax.jms.TopicConnection JavaDoc connection;
75         javax.jms.TopicSession JavaDoc session;
76         javax.jms.Topic JavaDoc topic;
77
78         try {
79             factory = new MantaTopicConnectionFactory();
80             connection = factory.createTopicConnection();
81             session = connection.createTopicSession(false, javax.jms.Session.
82                                                     AUTO_ACKNOWLEDGE);
83             topic = session.createTopic(topicName);
84
85             this.subscriber = session.createSubscriber(topic);
86             connection.start();
87         } catch (javax.jms.JMSException JavaDoc e) {
88             throw new SimpleException(e);
89         }
90
91         this.listener = null;
92     } // Subscriber constructor
93

94     /**
95      * register a listener to this subscriber's topic. Successive calls to this
96      * method will cause any existing listener to be overriden.
97      *
98      * @param listener The listener object which will receive incoming
99      * messages.
100      */

101     public void subscribe(MessageListener listener) throws SimpleException {
102         try {
103             this.listener = listener;
104             this.subscriber.setMessageListener(new javax.jms.MessageListener JavaDoc()
105                 {
106                     public void onMessage(javax.jms.Message JavaDoc message) {
107                         Message simpleMessage = null;
108                         try {
109                             simpleMessage =
110                                 new Message((javax.jms.BytesMessage JavaDoc) message);
111                         } catch (javax.jms.JMSException JavaDoc e) {
112                             System.out.println("Simple API Error: " + e);
113                             // TODO: log
114
}
115                         Subscriber.this.listener.
116                             onMessage(simpleMessage,
117                                       Subscriber.this.topicName);
118                     }
119                 });
120         } catch (javax.jms.JMSException JavaDoc e) {
121             throw new SimpleException(e);
122         }
123     }
124
125     /**
126      * Close this subscriber. All resources associated with this subscriber are
127      * released, and no further operations are allowed.
128      *
129      * @throws SimpleException If this subscriber was already closed.
130      */

131     public void close() throws SimpleException {
132         if (this.listener == null) {
133             throw new SimpleException(SimpleException.ERROR_CLOSED_ACTOR,
134                                       "Suscriber for topic " + this.topicName +
135                                       " is already closed.");
136         }
137
138         try {
139             this.subscriber.close();
140         } catch (javax.jms.JMSException JavaDoc e) {
141             throw new SimpleException(e);
142         }
143         this.listener = null;
144     }
145
146     /**
147      * @return Returns the topicName.
148      */

149     public String JavaDoc getTopicName() {
150         return topicName;
151     }
152 } // Subscriber
153
Popular Tags