KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > SomniMessageListenerRunner


1 package net.walend.somnifugi;
2
3 import javax.jms.MessageListener JavaDoc;
4 import javax.jms.JMSException JavaDoc;
5 import javax.jms.Message JavaDoc;
6
7 /**
8 A Runnable that calls the consumer's receive method to get a Message, then calls the MessageListener's onMessage method.
9 <p>
10 To stop it, call the consumer's close() method.
11 <p>
12 If the MessageListener's onMessage() or the MessageConsumer produces a RuntimeException, this SomniMessageListenerRunner will log the error via the ExceptionListener, redeliver the message, and let the message listener's run() method end.
13 <p>
14 @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
15  */

16
17 class SomniMessageListenerRunner
18     implements Runnable JavaDoc
19 {
20     private final SomniMessageConsumer consumer;
21     private final MessageListener JavaDoc listener;
22     private Thread JavaDoc thread;
23     private boolean closed = false;
24     private final SomniExceptionListener exceptionListener;
25
26
27     private final Object JavaDoc guard = new Object JavaDoc();
28
29     protected SomniMessageListenerRunner(SomniMessageConsumer consumer,MessageListener JavaDoc listener,SomniExceptionListener exceptionListener)
30     {
31         this.consumer = consumer;
32         this.listener = listener;
33         this.exceptionListener = exceptionListener;
34     }
35
36     protected MessageListener JavaDoc getMessageListener()
37     {
38         return listener;
39     }
40
41     void close()
42     {
43         synchronized(guard)
44             {
45                 closed = true;
46             }
47         SomniLogger.IT.finer(consumer.getName()+"'s MessageListener closed.");
48     }
49
50     void joinThread()
51         throws SomniCannotCloseException
52     {
53         if((thread!=null)&&(thread!=Thread.currentThread()))
54         {
55             try
56             {
57                 SomniLogger.IT.fine(consumer.getName()+"'s thread, "+thread.getName()+" is joining "+Thread.currentThread().getName());
58                 thread.interrupt();
59                 thread.join();
60                 SomniLogger.IT.fine(consumer.getName()+"'s thread, "+thread.getName()+" joined "+Thread.currentThread().getName());
61             }
62             catch(InterruptedException JavaDoc ie)
63             {
64                 throw new SomniCannotCloseException(ie);
65             }
66         }
67     }
68
69     private boolean isClosed()
70     {
71         synchronized(guard)
72             {
73                 return closed;
74             }
75     }
76
77     //Runnable interface
78
public void run()
79     {
80         synchronized(guard)
81             {
82                 thread = Thread.currentThread();
83             }
84         
85         Message JavaDoc message = null;
86         try
87             {
88                 while(!thread.isInterrupted()&&!isClosed())
89                     {
90                         try
91                             {
92                                 message = null;
93                                 message = consumer.receive();
94                                 listener.onMessage(message);
95                             }
96                         catch(SomniInterruptedException aie)
97                             {
98                                 SomniLogger.IT.fine(consumer.getName()+"'s MessageListener reinterrupting thread "+thread.getName());
99                                 thread.interrupt();
100                             }
101                         catch(JMSException JavaDoc jmse)
102                             {
103                                 exceptionListener.onException(jmse);
104                             }
105                     }
106             }
107         catch(RuntimeException JavaDoc re)
108             {
109                 exceptionListener.onException(re);
110                 consumer.redeliver(message);
111             }
112     }
113 }
114
115 /* Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
116 All rights reserved.
117
118 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
119
120 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
121
122 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
123
124 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
125
126 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
127
128 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
129 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
130
131 =================================================================================
132
133 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
134  */

135
Popular Tags