KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > named > NamedQueueClient


1 /*
2  * <Add library description here>
3  * Copyright (C) 2007 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * NamedQueueClient.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.named;
24
25 // java imports
26
import java.util.Date JavaDoc;
27 import javax.naming.Context JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29
30 // logging import
31
import org.apache.log4j.Logger;
32
33 // coadunation imports
34
import com.rift.coad.daemon.messageservice.Message;
35 import com.rift.coad.daemon.messageservice.MessageServiceException;
36 import com.rift.coad.daemon.messageservice.QueueManager;
37 import com.rift.coad.daemon.messageservice.NamedQueue;
38 import com.rift.coad.util.connection.ConnectionManager;
39
40 /**
41  * This object is responsible for managing the connection to the named queue
42  * object.
43  *
44  * @author Brett Chaldecott
45  */

46 public class NamedQueueClient {
47     
48     // class constants
49
private static long MAX_TIMEOUT = 1000;
50     
51     // log refernce
52
private Logger log = Logger.getLogger(NamedQueueClient.class.getName());
53     
54     // private member variables
55
private Context JavaDoc context = null;
56     private String JavaDoc jndiUrl = null;
57     private String JavaDoc name = null;
58     private NamedQueue namedQueue = null;
59     
60     
61     /**
62      * Creates a new instance of NamedQueueClient
63      *
64      * @param name The name of the queue to make a connection to.
65      * @exception MessageServiceException
66      */

67     private NamedQueueClient(String JavaDoc name) throws MessageServiceException {
68         try {
69             this.jndiUrl = QueueManager.JNDI_URL;
70             this.name = name;
71         } catch (Exception JavaDoc ex) {
72             throw new MessageServiceException(
73                     "Failed to instanciate the NamedQueueClient : " +
74                     ex.getMessage(),ex);
75         }
76     }
77     
78     
79     /**
80      * Creates a new instance of NamedQueueClient
81      *
82      * @param context The context that should be used to make a connection to
83      * the message service.
84      * @param jndiUrl The url of the message queue manager.
85      * @param name The name of the queue to connect to.
86      * @exception MessageServiceException
87      */

88     private NamedQueueClient(Context JavaDoc context,String JavaDoc jndiUrl, String JavaDoc name) throws
89             MessageServiceException {
90         try {
91             context = new InitialContext JavaDoc();
92             this.jndiUrl = jndiUrl;
93             this.name = name;
94         } catch (Exception JavaDoc ex) {
95             throw new MessageServiceException(
96                     "Failed to instanciate the NamedQueueClient : " +
97                     ex.getMessage(),ex);
98         }
99     }
100     
101     
102     /**
103      * This method returns an instance of the NamedQueueClient object.
104      *
105      * @return The reference to the NamedQueueClient object.
106      * @param name The name of the queue to return.
107      * @exception MessageServiceException
108      */

109     public static NamedQueueClient create(String JavaDoc name) throws
110             MessageServiceException {
111         return new NamedQueueClient(name);
112     }
113     
114     
115     /**
116      * This method returns an instance of the NamedQueueClient object.
117      *
118      * @return The reference to the NamedQueueClient object.
119      * @param context The context that should be used to make a connection to
120      * the message service.
121      * @param jndiUrl The url of the message queue manager.
122      * @param name The name of the queue to connect to.
123      * @exception MessageServiceException
124      */

125     public static NamedQueueClient create(Context JavaDoc context,String JavaDoc jndiUrl,
126             String JavaDoc name) throws MessageServiceException {
127         return new NamedQueueClient(context,jndiUrl,name);
128     }
129     
130     
131     /**
132      * This method returns the reference to the received message from the
133      * named queue, or null if not message is available.
134      *
135      * @return A reference to the retrieved message or NULL.
136      * @param delay The delay in processing.
137      * @exception MessageServiceException
138      */

139     public Message receive(long delay) throws MessageServiceException {
140         Date JavaDoc startTime = new Date JavaDoc();
141         Date JavaDoc currentTime = null;
142         while((startTime.getTime() + delay) >
143                 (currentTime = new Date JavaDoc()).getTime()) {
144             try {
145                 NamedQueue namedQueue = getNamedQueue();
146                 Message result = null;
147                 if ((delay == 0) || (delay > MAX_TIMEOUT)) {
148                     result = namedQueue.receive(MAX_TIMEOUT);
149                 } else {
150                     result = namedQueue.receive(delay);
151                 }
152                 if (result != null) {
153                     return result;
154                 }
155             } catch (java.rmi.RemoteException JavaDoc ex) {
156                 log.error("Failed to retrieve the queue entry : " +
157                         ex.getMessage(),ex);
158                 this.namedQueue = null;
159             } catch (MessageServiceException ex) {
160                 throw ex;
161             } catch (Exception JavaDoc ex) {
162                 throw new MessageServiceException(
163                         "Failed to retrieve a message : " + ex.getMessage(),ex);
164             }
165         }
166         return null;
167     }
168     
169     
170     /**
171      * This method returns the named queue
172      *
173      * @return The reference to the named queue.
174      * @exception MessageServiceException
175      */

176     private NamedQueue getNamedQueue() throws MessageServiceException {
177         try {
178             if (namedQueue != null) {
179                 return namedQueue;
180             }
181             QueueManager queueManager = null;
182             if (context == null) {
183                 queueManager= (QueueManager)ConnectionManager
184                         .getInstance().getConnection(QueueManager.class,
185                         this.jndiUrl);
186             } else {
187                 queueManager= (QueueManager)ConnectionManager
188                         .getInstance(context).getConnection(QueueManager.class,
189                         this.jndiUrl);
190             }
191             return this.namedQueue = queueManager.getNamedQueue(this.name);
192         } catch (Exception JavaDoc ex) {
193             throw new MessageServiceException(
194                     "Failed to retrieve a named queue because : " +
195                     ex.getMessage(),ex);
196         }
197     }
198 }
199
Popular Tags