1 25 26 29 package org.objectweb.jonas.jtests.beans.message; 30 31 import java.rmi.RemoteException ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 35 import javax.ejb.CreateException ; 36 import javax.ejb.EJBException ; 37 import javax.ejb.FinderException ; 38 import javax.ejb.SessionBean ; 39 import javax.ejb.SessionContext ; 40 import javax.jms.ConnectionFactory ; 41 import javax.jms.Destination ; 42 import javax.jms.JMSException ; 43 import javax.jms.MapMessage ; 44 import javax.jms.MessageProducer ; 45 import javax.jms.Session ; 46 import javax.naming.InitialContext ; 47 import javax.naming.NamingException ; 48 import javax.rmi.PortableRemoteObject ; 49 50 import org.objectweb.jonas.common.Log; 51 import org.objectweb.jonas.jtests.util.Env; 52 import org.objectweb.util.monolog.api.BasicLevel; 53 import org.objectweb.util.monolog.api.Logger; 54 55 60 public class Sender1_1SF implements SessionBean { 61 62 static protected Logger logger = null; 63 SessionContext ejbContext; 64 private transient InitialContext ictx = null; 65 private transient ConnectionFactory cf = null; 66 private transient javax.jms.Connection c = null; 67 private transient MRecordHome ehome = null; 68 private static int count = 1; 69 70 74 85 public void setSessionContext(SessionContext ctx) { 86 if (logger == null) 87 logger = Log.getLogger(Log.JONAS_TESTS_PREFIX); 88 logger.log(BasicLevel.DEBUG, ""); 89 ejbContext = ctx; 90 } 91 92 101 public void ejbRemove() { 102 logger.log(BasicLevel.DEBUG, ""); 103 try { 104 c.close(); 105 } catch (Exception e) { 106 logger.log(BasicLevel.ERROR, "Exception on close:" + e); 107 } 108 } 109 110 115 public void ejbCreate() throws CreateException { 116 logger.log(BasicLevel.DEBUG, ""); 117 try { 119 ictx = new InitialContext (); 120 cf = (ConnectionFactory ) ictx.lookup("CF"); 121 } catch (NamingException e) { 122 logger.log(BasicLevel.ERROR, "SenderSF : Cannot lookup Connection Factories: "+e); 123 throw new CreateException ("SenderSF: Cannot lookup Connection Factories"); 124 } 125 126 try { 128 129 c = cf.createConnection(); 130 } catch (JMSException e) { 131 logger.log(BasicLevel.ERROR, "SenderSF : Cannot create connections: "+e); 132 throw new CreateException ("SenderSF: Cannot create connections"); 133 } 134 135 String BEAN_HOME = "messageMRecordECHome"; 137 try { 138 ehome = (MRecordHome) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME), MRecordHome.class); 139 } catch (NamingException e) { 140 logger.log(BasicLevel.ERROR, "SenderSF ejbCreate: Cannot get entity home: "+e); 141 throw new CreateException ("SenderSF: Cannot get entity home"); 142 } 143 } 144 145 149 public void ejbPassivate() { 150 logger.log(BasicLevel.DEBUG, ""); 151 } 152 153 158 public void ejbActivate() { 159 logger.log(BasicLevel.DEBUG, ""); 160 } 161 162 166 169 private String getUUID() { 170 long uuid; 171 synchronized(getClass()) { 172 uuid = System.currentTimeMillis() * 256 + count; 173 count++; 174 } 175 return String.valueOf(uuid); 176 } 177 178 182 188 public void sendOnDestination(String dst, int val, int nb) { 189 logger.log(BasicLevel.DEBUG, ""); 190 Destination dest = null; 192 try { 193 dest = (Destination ) ictx.lookup(dst); 194 } catch (NamingException e) { 195 throw new EJBException ("sendOnDestination: Cannot lookup "+dest); 196 } 197 198 Session ss = null; 202 try { 203 ss = c.createSession(true, 0); 206 } catch (JMSException e) { 207 throw new EJBException ("Cannot create Session: "+e); 208 } 209 210 MessageProducer producer = null; 212 try { 213 producer = ss.createProducer(dest); 214 } catch (JMSException e) { 215 throw new EJBException ("Cannot create MessageProducer: "+e); 216 } 217 218 try { 220 for (int i = 0; i < nb; i++) { 221 MapMessage mess = ss.createMapMessage(); 222 mess.setString("Id", getUUID()); 223 mess.setString("Text",dst); 224 mess.setInt("Value", val); 225 producer.send(mess); 226 } 227 } catch (JMSException e) { 228 throw new EJBException ("Cannot send message: "+e); 229 } 230 231 try { 234 ss.close(); 235 } catch (JMSException e) { 236 throw new EJBException ("Cannot close session: "+e); 237 } 238 } 239 240 246 public void sendOnDestinationTx(String dest, int val, int nb) { 247 sendOnDestination(dest, val, nb); 248 } 249 250 251 252 259 public int check(int val, int nb, int sec) { 260 Collection elist = null; 261 int retval = 0; 262 for (int i = 0; i <= sec; i++) { 263 logger.log(BasicLevel.DEBUG, "sec : " + i + "/" + sec); 264 try { 265 elist = ehome.findByValue(val); 266 retval = elist.size(); 267 if (retval >= nb) { 268 Iterator it = elist.iterator(); 270 while (it.hasNext()) { 271 MRecord ent = (MRecord) PortableRemoteObject.narrow(it.next(), MRecord.class); 272 try { 273 ent.remove(); 274 } catch (Exception e) { 275 throw new EJBException ("Error on remove"); 276 } 277 } 278 return retval; 279 } 280 } catch (FinderException e) { 281 } catch (RemoteException e) { 282 return retval; 283 } 284 try { 285 Thread.sleep(1000); 286 } catch (InterruptedException e) { 287 } 288 } 289 return retval; 290 } 291 292 295 public void clean(int val) { 296 logger.log(BasicLevel.DEBUG, ""); 297 Collection elist = null; 298 try { 299 elist = ehome.findByValue(val); 300 } catch (FinderException e) { 301 return; 302 } catch (Exception e) { 303 throw new EJBException ("Error on find"); 304 } 305 Iterator it = elist.iterator(); 306 while (it.hasNext()) { 307 MRecord ent = (MRecord) PortableRemoteObject.narrow(it.next(), MRecord.class); 308 try { 309 ent.remove(); 310 } catch (Exception e) { 311 throw new EJBException ("Error on remove"); 312 } 313 } 314 } 315 316 } 317 | Popular Tags |