1 21 22 package com.rift.coad.daemon.messageservice.named; 24 25 import java.util.Date ; 27 import javax.naming.Context ; 28 import javax.naming.InitialContext ; 29 30 import org.apache.log4j.Logger; 32 33 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 46 public class NamedQueueClient { 47 48 private static long MAX_TIMEOUT = 1000; 50 51 private Logger log = Logger.getLogger(NamedQueueClient.class.getName()); 53 54 private Context context = null; 56 private String jndiUrl = null; 57 private String name = null; 58 private NamedQueue namedQueue = null; 59 60 61 67 private NamedQueueClient(String name) throws MessageServiceException { 68 try { 69 this.jndiUrl = QueueManager.JNDI_URL; 70 this.name = name; 71 } catch (Exception ex) { 72 throw new MessageServiceException( 73 "Failed to instanciate the NamedQueueClient : " + 74 ex.getMessage(),ex); 75 } 76 } 77 78 79 88 private NamedQueueClient(Context context,String jndiUrl, String name) throws 89 MessageServiceException { 90 try { 91 context = new InitialContext (); 92 this.jndiUrl = jndiUrl; 93 this.name = name; 94 } catch (Exception ex) { 95 throw new MessageServiceException( 96 "Failed to instanciate the NamedQueueClient : " + 97 ex.getMessage(),ex); 98 } 99 } 100 101 102 109 public static NamedQueueClient create(String name) throws 110 MessageServiceException { 111 return new NamedQueueClient(name); 112 } 113 114 115 125 public static NamedQueueClient create(Context context,String jndiUrl, 126 String name) throws MessageServiceException { 127 return new NamedQueueClient(context,jndiUrl,name); 128 } 129 130 131 139 public Message receive(long delay) throws MessageServiceException { 140 Date startTime = new Date (); 141 Date currentTime = null; 142 while((startTime.getTime() + delay) > 143 (currentTime = new Date ()).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 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 ex) { 162 throw new MessageServiceException( 163 "Failed to retrieve a message : " + ex.getMessage(),ex); 164 } 165 } 166 return null; 167 } 168 169 170 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 ex) { 193 throw new MessageServiceException( 194 "Failed to retrieve a named queue because : " + 195 ex.getMessage(),ex); 196 } 197 } 198 } 199 | Popular Tags |