001: /*
002: * JOnAS: Java(TM) Open Application Server
003: * Copyright (C) 1999 Bull S.A.
004: * Contact: jonas-team@objectweb.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
019: * USA
020: *
021: * --------------------------------------------------------------------------
022: * $Id: Listener.java 5995 2004-12-17 15:08:36Z joaninh $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.message;
027:
028: import java.rmi.RemoteException;
029: import javax.ejb.CreateException;
030: import javax.ejb.FinderException;
031: import javax.ejb.MessageDrivenBean;
032: import javax.ejb.MessageDrivenContext;
033: import javax.ejb.TimedObject;
034: import javax.ejb.Timer;
035: import javax.ejb.TimerService;
036: import javax.jms.MapMessage;
037: import javax.jms.Message;
038: import javax.jms.MessageListener;
039: import javax.jms.JMSException;
040: import javax.naming.InitialContext;
041: import javax.naming.NamingException;
042: import javax.rmi.PortableRemoteObject;
043: import javax.transaction.Transaction;
044:
045: import org.objectweb.jonas.common.Log;
046: import org.objectweb.jonas.jtests.util.JBean;
047: import org.objectweb.util.monolog.api.Logger;
048: import org.objectweb.util.monolog.api.BasicLevel;
049:
050: /**
051: * Common code for all Message Driven Beans
052: * @author Philippe Durieux, Philippe Coq
053: */
054: public abstract class Listener extends JBean implements
055: MessageDrivenBean, MessageListener, TimedObject {
056: protected static Logger logger = null;
057: protected transient MessageDrivenContext mdbContext;
058: protected transient MRecordHome arh = null;
059: protected String myname;
060:
061: abstract protected String getMyDest();
062:
063: public static final int CREATE_TIMER_MIN = 1500;
064: public static final int CREATE_TIMER_MAX = 1600;
065:
066: /**
067: * Default constructor
068: */
069: public Listener() {
070: }
071:
072: // ------------------------------------------------------------------
073: // MessageDrivenBean implementation
074: // ------------------------------------------------------------------
075:
076: /**
077: * Set the associated context. The container call this method
078: * after the instance creation.
079: * The enterprise Bean instance should store the reference to the context
080: * object in an instance variable.
081: * This method is called with no transaction context.
082: *
083: * @param ctx A MessageDrivenContext interface for the instance.
084: * @throws EJBException Thrown by the method to indicate a failure caused by
085: * a system-level error.
086: */
087:
088: public void setMessageDrivenContext(MessageDrivenContext ctx) {
089: if (logger == null) {
090: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
091: }
092: logger.log(BasicLevel.DEBUG, "");
093: mdbContext = ctx;
094: }
095:
096: /**
097: * A container invokes this method before it ends the life of the message-driven object.
098: * This happens when a container decides to terminate the message-driven object.
099: *
100: * This method is called with no transaction context.
101: *
102: * @throws EJBException Thrown by the method to indicate a failure caused by
103: * a system-level error.
104: */
105: public void ejbRemove() {
106: logger.log(BasicLevel.DEBUG, "");
107: }
108:
109: /**
110: * The Message driven bean must define an ejbCreate methods with no args.
111: */
112: public void ejbCreate() {
113: logger.log(BasicLevel.DEBUG, "");
114:
115: // Get a ref on MRecordHome
116: InitialContext ictx = null;
117: try {
118: ictx = new InitialContext();
119: arh = (MRecordHome) PortableRemoteObject.narrow(ictx
120: .lookup("messageMRecordECHome"), MRecordHome.class);
121: } catch (NamingException e) {
122: logger.log(BasicLevel.ERROR,
123: "Listener : Cannot get messageMRecordHome:" + e);
124: }
125:
126: // Accesses the bean env to get the bean name
127: // This test also that we can access java:comp/env from here.
128: try {
129: myname = (String) ictx.lookup("java:comp/env/mdbname");
130: } catch (NamingException e) {
131: logger.log(BasicLevel.ERROR,
132: "Listener : Cannot access java:comp/env/mdbname from ejbCreate:"
133: + e);
134: }
135: }
136:
137: /**
138: * onMessage method
139: */
140: public void onMessage(Message message) {
141: logger.log(BasicLevel.DEBUG, "");
142:
143: sleep(20);
144:
145: // Decode the message (MapMessage)
146: String uuid = null;
147: String dest = null;
148: int value = 0;
149: MapMessage msg = (MapMessage) message;
150: try {
151: uuid = msg.getString("Id");
152: dest = msg.getString("Text");
153: value = msg.getInt("Value");
154: } catch (JMSException e) {
155: logger.log(BasicLevel.ERROR, "Listener exception:" + e);
156: return;
157: }
158:
159: // Check destination
160: if (!dest.equals(getMyDest())) {
161: logger.log(BasicLevel.ERROR, "Bad destination: " + dest
162: + ". Expected is " + getMyDest());
163: return;
164: }
165:
166: // Create a timer if required
167: if (value >= CREATE_TIMER_MIN && value <= CREATE_TIMER_MAX) {
168: TimerService timerservice = mdbContext.getTimerService();
169: int dur = value - CREATE_TIMER_MIN;
170: Info info = new Info(uuid, dest, value, myname);
171: Timer mt = timerservice.createTimer(dur * 1000, dur * 1000,
172: info);
173: return;
174: }
175:
176: // Create a new Entity bean for this message
177: // Check that transaction association did not change after create.
178: try {
179: Transaction t1 = getCurrentTransaction();
180: arh.create(uuid, dest, value, myname);
181: if (t1 != getCurrentTransaction()) {
182: logger.log(BasicLevel.ERROR,
183: "Error in transaction association");
184: }
185: } catch (CreateException e) {
186: logger.log(BasicLevel.ERROR, "Listener exception:" + e);
187: } catch (RemoteException e) {
188: logger.log(BasicLevel.ERROR, "Listener exception:" + e);
189: }
190:
191: }
192:
193: // -----------------------------------------------------------
194: // TimedObject implementation
195: // -----------------------------------------------------------
196:
197: /**
198: * A timer is expired.
199: */
200: public void ejbTimeout(Timer timer) {
201: logger.log(BasicLevel.DEBUG, "");
202: Info info = (Info) timer.getInfo();
203: // Create a new Entity bean for this message
204: try {
205: MRecord mr = arh.findByPrimaryKey(new MRecordPK(info.uuid,
206: info.ejbname));
207: int cnt = mr.getCount();
208: if (cnt <= 3) {
209: mr.updateCount();
210: } else {
211: timer.cancel();
212: }
213: } catch (RemoteException e) {
214: logger.log(BasicLevel.ERROR, "Listener exception :" + e);
215: } catch (FinderException e) {
216: try {
217: arh.create(info.uuid, info.dest, info.value,
218: info.ejbname);
219: } catch (CreateException ee) {
220: logger
221: .log(BasicLevel.ERROR, "Listener exception:"
222: + ee);
223: } catch (RemoteException ee) {
224: logger.log(BasicLevel.ERROR,
225: "Listener exception on create:" + ee);
226: }
227: }
228:
229: }
230:
231: }
|