1 25 26 package org.objectweb.jonas.jtests.beans.message; 27 28 import java.rmi.RemoteException ; 29 import javax.ejb.CreateException ; 30 import javax.ejb.FinderException ; 31 import javax.ejb.MessageDrivenBean ; 32 import javax.ejb.MessageDrivenContext ; 33 import javax.ejb.TimedObject ; 34 import javax.ejb.Timer ; 35 import javax.ejb.TimerService ; 36 import javax.jms.MapMessage ; 37 import javax.jms.Message ; 38 import javax.jms.MessageListener ; 39 import javax.jms.JMSException ; 40 import javax.naming.InitialContext ; 41 import javax.naming.NamingException ; 42 import javax.rmi.PortableRemoteObject ; 43 import javax.transaction.Transaction ; 44 45 import org.objectweb.jonas.common.Log; 46 import org.objectweb.jonas.jtests.util.JBean; 47 import org.objectweb.util.monolog.api.Logger; 48 import org.objectweb.util.monolog.api.BasicLevel; 49 50 54 public abstract class Listener extends JBean implements MessageDrivenBean , MessageListener , TimedObject { 55 protected static Logger logger = null; 56 protected transient MessageDrivenContext mdbContext; 57 protected transient MRecordHome arh = null; 58 protected String myname; 59 60 abstract protected String getMyDest(); 61 62 public static final int CREATE_TIMER_MIN = 1500; 63 public static final int CREATE_TIMER_MAX = 1600; 64 65 68 public Listener() { 69 } 70 71 75 86 87 public void setMessageDrivenContext(MessageDrivenContext ctx) { 88 if (logger == null) { 89 logger = Log.getLogger(Log.JONAS_TESTS_PREFIX); 90 } 91 logger.log(BasicLevel.DEBUG, ""); 92 mdbContext = ctx; 93 } 94 95 104 public void ejbRemove() { 105 logger.log(BasicLevel.DEBUG, ""); 106 } 107 108 111 public void ejbCreate() { 112 logger.log(BasicLevel.DEBUG, ""); 113 114 InitialContext ictx = null; 116 try { 117 ictx = new InitialContext (); 118 arh = (MRecordHome) PortableRemoteObject.narrow(ictx.lookup("messageMRecordECHome"), MRecordHome.class); 119 } catch (NamingException e) { 120 logger.log(BasicLevel.ERROR, "Listener : Cannot get messageMRecordHome:" + e); 121 } 122 123 try { 126 myname = (String ) ictx.lookup("java:comp/env/mdbname"); 127 } catch (NamingException e) { 128 logger.log(BasicLevel.ERROR, "Listener : Cannot access java:comp/env/mdbname from ejbCreate:" + e); 129 } 130 } 131 132 135 public void onMessage(Message message) { 136 logger.log(BasicLevel.DEBUG, ""); 137 138 sleep(20); 139 140 String uuid = null; 142 String dest = null; 143 int value = 0; 144 MapMessage msg = (MapMessage ) message; 145 try { 146 uuid = msg.getString("Id"); 147 dest = msg.getString("Text"); 148 value = msg.getInt("Value"); 149 } catch (JMSException e) { 150 logger.log(BasicLevel.ERROR, "Listener exception:" + e); 151 return; 152 } 153 154 if (! dest.equals(getMyDest())) { 156 logger.log(BasicLevel.ERROR, "Bad destination: " + dest + ". Expected is " + getMyDest()); 157 return; 158 } 159 160 if (value >= CREATE_TIMER_MIN && value <= CREATE_TIMER_MAX) { 162 TimerService timerservice = mdbContext.getTimerService(); 163 int dur = value - CREATE_TIMER_MIN; 164 Info info = new Info(uuid, dest, value, myname); 165 Timer mt = timerservice.createTimer(dur * 1000, dur * 1000, info); 166 return; 167 } 168 169 try { 172 Transaction t1 = getCurrentTransaction(); 173 arh.create(uuid, dest, value, myname); 174 if (t1 != getCurrentTransaction()) { 175 logger.log(BasicLevel.ERROR, "Error in transaction association"); 176 } 177 } catch (CreateException e) { 178 logger.log(BasicLevel.ERROR, "Listener exception:" + e); 179 } catch (RemoteException e) { 180 logger.log(BasicLevel.ERROR, "Listener exception:" + e); 181 } 182 183 184 } 185 186 190 193 public void ejbTimeout(Timer timer) { 194 logger.log(BasicLevel.DEBUG, ""); 195 Info info = (Info) timer.getInfo(); 196 try { 198 MRecord mr = arh.findByPrimaryKey(new MRecordPK(info.uuid, info.ejbname)); 199 int cnt = mr.getCount(); 200 if (cnt <= 3) { 201 mr.updateCount(); 202 } else { 203 timer.cancel(); 204 } 205 } catch (RemoteException e) { 206 logger.log(BasicLevel.ERROR, "Listener exception :" + e); 207 } catch (FinderException e) { 208 try { 209 arh.create(info.uuid, info.dest, info.value, info.ejbname); 210 } catch (CreateException ee) { 211 logger.log(BasicLevel.ERROR, "Listener exception:" + ee); 212 } catch (RemoteException ee) { 213 logger.log(BasicLevel.ERROR, "Listener exception on create:" + ee); 214 } 215 } 216 217 } 218 219 } 220 | Popular Tags |