001: /*
002: * <Add library description here>
003: * Copyright (C) 2007 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * NamedQueueClient.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice.named;
024:
025: // java imports
026: import java.util.Date;
027: import javax.naming.Context;
028: import javax.naming.InitialContext;
029:
030: // logging import
031: import org.apache.log4j.Logger;
032:
033: // coadunation imports
034: import com.rift.coad.daemon.messageservice.Message;
035: import com.rift.coad.daemon.messageservice.MessageServiceException;
036: import com.rift.coad.daemon.messageservice.QueueManager;
037: import com.rift.coad.daemon.messageservice.NamedQueue;
038: import com.rift.coad.util.connection.ConnectionManager;
039:
040: /**
041: * This object is responsible for managing the connection to the named queue
042: * object.
043: *
044: * @author Brett Chaldecott
045: */
046: public class NamedQueueClient {
047:
048: // class constants
049: private static long MAX_TIMEOUT = 1000;
050:
051: // log refernce
052: private Logger log = Logger.getLogger(NamedQueueClient.class
053: .getName());
054:
055: // private member variables
056: private Context context = null;
057: private String jndiUrl = null;
058: private String name = null;
059: private NamedQueue namedQueue = null;
060:
061: /**
062: * Creates a new instance of NamedQueueClient
063: *
064: * @param name The name of the queue to make a connection to.
065: * @exception MessageServiceException
066: */
067: private NamedQueueClient(String name)
068: throws MessageServiceException {
069: try {
070: this .jndiUrl = QueueManager.JNDI_URL;
071: this .name = name;
072: } catch (Exception ex) {
073: throw new MessageServiceException(
074: "Failed to instanciate the NamedQueueClient : "
075: + ex.getMessage(), ex);
076: }
077: }
078:
079: /**
080: * Creates a new instance of NamedQueueClient
081: *
082: * @param context The context that should be used to make a connection to
083: * the message service.
084: * @param jndiUrl The url of the message queue manager.
085: * @param name The name of the queue to connect to.
086: * @exception MessageServiceException
087: */
088: private NamedQueueClient(Context context, String jndiUrl,
089: String name) throws MessageServiceException {
090: try {
091: context = new InitialContext();
092: this .jndiUrl = jndiUrl;
093: this .name = name;
094: } catch (Exception ex) {
095: throw new MessageServiceException(
096: "Failed to instanciate the NamedQueueClient : "
097: + ex.getMessage(), ex);
098: }
099: }
100:
101: /**
102: * This method returns an instance of the NamedQueueClient object.
103: *
104: * @return The reference to the NamedQueueClient object.
105: * @param name The name of the queue to return.
106: * @exception MessageServiceException
107: */
108: public static NamedQueueClient create(String name)
109: throws MessageServiceException {
110: return new NamedQueueClient(name);
111: }
112:
113: /**
114: * This method returns an instance of the NamedQueueClient object.
115: *
116: * @return The reference to the NamedQueueClient object.
117: * @param context The context that should be used to make a connection to
118: * the message service.
119: * @param jndiUrl The url of the message queue manager.
120: * @param name The name of the queue to connect to.
121: * @exception MessageServiceException
122: */
123: public static NamedQueueClient create(Context context,
124: String jndiUrl, String name) throws MessageServiceException {
125: return new NamedQueueClient(context, jndiUrl, name);
126: }
127:
128: /**
129: * This method returns the reference to the received message from the
130: * named queue, or null if not message is available.
131: *
132: * @return A reference to the retrieved message or NULL.
133: * @param delay The delay in processing.
134: * @exception MessageServiceException
135: */
136: public Message receive(long delay) throws MessageServiceException {
137: Date startTime = new Date();
138: Date currentTime = null;
139: while ((startTime.getTime() + delay) > (currentTime = new Date())
140: .getTime()) {
141: try {
142: NamedQueue namedQueue = getNamedQueue();
143: Message result = null;
144: if ((delay == 0) || (delay > MAX_TIMEOUT)) {
145: result = namedQueue.receive(MAX_TIMEOUT);
146: } else {
147: result = namedQueue.receive(delay);
148: }
149: if (result != null) {
150: return result;
151: }
152: } catch (java.rmi.RemoteException ex) {
153: log.error("Failed to retrieve the queue entry : "
154: + ex.getMessage(), ex);
155: this .namedQueue = null;
156: } catch (MessageServiceException ex) {
157: throw ex;
158: } catch (Exception ex) {
159: throw new MessageServiceException(
160: "Failed to retrieve a message : "
161: + ex.getMessage(), ex);
162: }
163: }
164: return null;
165: }
166:
167: /**
168: * This method returns the named queue
169: *
170: * @return The reference to the named queue.
171: * @exception MessageServiceException
172: */
173: private NamedQueue getNamedQueue() throws MessageServiceException {
174: try {
175: if (namedQueue != null) {
176: return namedQueue;
177: }
178: QueueManager queueManager = null;
179: if (context == null) {
180: queueManager = (QueueManager) ConnectionManager
181: .getInstance().getConnection(
182: QueueManager.class, this .jndiUrl);
183: } else {
184: queueManager = (QueueManager) ConnectionManager
185: .getInstance(context).getConnection(
186: QueueManager.class, this .jndiUrl);
187: }
188: return this .namedQueue = queueManager
189: .getNamedQueue(this .name);
190: } catch (Exception ex) {
191: throw new MessageServiceException(
192: "Failed to retrieve a named queue because : "
193: + ex.getMessage(), ex);
194: }
195: }
196: }
|