KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jahia > services > cache > JMSHubPublisher


1 /*
2  * ____.
3  * __/\ ______| |__/\. _______
4  * __ .____| | \ | +----+ \
5  * _______| /--| | | - \ _ | : - \_________
6  * \\______: :---| : : | : | \________>
7  * |__\---\_____________:______: :____|____:_____\
8  * /_____|
9  *
10  * . . . i n j a h i a w e t r u s t . . .
11  *
12  *
13  *
14  * ----- BEGIN LICENSE BLOCK -----
15  * Version: JCSL 1.0
16  *
17  * The contents of this file are subject to the Jahia Community Source License
18  * 1.0 or later (the "License"); you may not use this file except in
19  * compliance with the License. You may obtain a copy of the License at
20  * http://www.jahia.org/license
21  *
22  * Software distributed under the License is distributed on an "AS IS" basis,
23  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
24  * for the rights, obligations and limitations governing use of the contents
25  * of the file. The Original and Upgraded Code is the Jahia CMS and Portal
26  * Server. The developer of the Original and Upgraded Code is JAHIA Ltd. JAHIA
27  * Ltd. owns the copyrights in the portions it created. All Rights Reserved.
28  *
29  * The Developer of the Shared Modifications is Jahia Solution Sarl.
30  * Portions created by the Initial Developer are Copyright (C) 2002 by the
31  * Initial Developer. All Rights Reserved.
32  *
33  * Contributor(s):
34  * 05-SEP-2003, Jahia Solutions Sarl, Fulco Houkes : Initial version
35  *
36  * ----- END LICENSE BLOCK -----
37  */

38
39
40 package org.jahia.services.cache;
41
42
43 /** <p>This class implements a threaded JMS Message Publisher.</p>
44  * <p>Instanciating this class will not automatically perform a connection to the JMS
45  * server, this operation has to be done through the
46  * {@link org.jahia.services.cache.JMSHubPublisher#connect connect()} method. Initiaiting
47  * a connection will create a new
48  * {@link org.jahia.services.cache.JMSHubPublisherHandler JMSHubPublisherHandler} instance
49  * and associate it to a new thread instance.</p>
50  * <p>It's important to call the
51  * {@link org.jahia.services.cache.JMSHubPublisher#disconnect disconnect()} method, because
52  * it will properly stop the Message Publisher thread and close the publishing connection
53  * to the JMS server.</p>
54  *
55  * @author Fulco Houkes, Copyright (c) 2003 by Jahia Ltd.
56  * @version 1.0
57  * @since Jahia 4.0
58  */

59 class JMSHubPublisher {
60
61     /** logging. */
62     final private static org.apache.log4j.Logger logger =
63             org.apache.log4j.Logger.getLogger (JMSHubPublisher.class);
64
65     /** the associated thread handler. */
66     private Thread JavaDoc thread;
67
68     /** message handler. */
69     private JMSHubPublisherHandler handler;
70
71     /** Default constructor, creates a new <code>JMSHubPublisher</code> instance.*/
72     protected JMSHubPublisher () {
73
74         // log the instanciation
75
logger.debug ("JMSHubPublisher successfully instanciated!");
76     }
77
78
79     /** Try to initiate a Message Publisher connection to the JMS Server.
80      *
81      * @param hub the reference to the JMSHub, holding the topic connection and session.
82      *
83      * @throws JMSConnectionException
84      * when the JMS server is unreachable
85      */

86     public synchronized void connect (JMSHub hub)
87             throws JMSConnectionException
88     {
89         // no need to connect twice
90
if (((thread != null) && thread.isAlive()) && (handler != null)) {
91             return;
92         }
93
94         // cleanup existing handler
95
if (handler != null) {
96             handler.stop();
97         }
98
99         // cleanup existing thread
100
if (thread != null) {
101             try {
102                 thread.interrupt();
103                 thread.join();
104             } catch (InterruptedException JavaDoc ex) {
105                 // it's what we were waiting for
106
}
107         }
108
109         handler = new JMSHubPublisherHandler ();
110         handler.init (hub);
111
112         thread = new Thread JavaDoc (handler, "JMS Publisher");
113         thread.start();
114
115         logger.info("JMS Message Publisher successfully started.");
116     }
117
118     /** Disconnect the Message Publisher from the JMS server and frees
119      * all the allocated resources.
120      */

121     public synchronized void disconnect () {
122         if (thread == Thread.currentThread()) {
123             thread = null;
124             handler = null;
125             return;
126         }
127
128         if (handler != null)
129             handler.stop();
130
131         if (thread != null) {
132             try {
133                 thread.join();
134
135             } catch (InterruptedException JavaDoc ex) {
136                 // it's what we were waiting for anyway...
137
}
138         }
139
140         thread = null;
141         handler = null;
142     }
143
144     /** Publish the specified message. <code>null</code> messages will be ignored.
145      *
146      * @param message the message to publish
147      */

148     public synchronized void publishMessage (JMSCacheMessage message) {
149         if (message == null)
150             return;
151
152         if ((handler != null) && ((thread != null) && (thread.isAlive()))) {
153             handler.publishMessage (message);
154
155         } else {
156             logger.debug("The Message Publisher is not connected. Message dropped.");
157         }
158     }
159
160     /** Publish the specified message. <code>null</code> messages will be ignored.
161      *
162      * @param message the message to publish
163      */

164     public synchronized void sendNow () {
165
166         if ((handler != null) && ((thread != null) && (thread.isAlive()))) {
167             handler.sendNow();
168
169         } else {
170             logger.debug("The Message Publisher is not connected. Message dropped.");
171         }
172     }
173 }
174
Popular Tags