KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > message > Listener


1 /*
2  * JOnAS: Java(TM) Open Application Server
3  * Copyright (C) 1999 Bull S.A.
4  * Contact: jonas-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: Listener.java,v 1.8 2004/12/17 15:08:35 joaninh Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.message;
27
28 import java.rmi.RemoteException JavaDoc;
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.FinderException JavaDoc;
31 import javax.ejb.MessageDrivenBean JavaDoc;
32 import javax.ejb.MessageDrivenContext JavaDoc;
33 import javax.ejb.TimedObject JavaDoc;
34 import javax.ejb.Timer JavaDoc;
35 import javax.ejb.TimerService JavaDoc;
36 import javax.jms.MapMessage JavaDoc;
37 import javax.jms.Message JavaDoc;
38 import javax.jms.MessageListener JavaDoc;
39 import javax.jms.JMSException JavaDoc;
40 import javax.naming.InitialContext JavaDoc;
41 import javax.naming.NamingException JavaDoc;
42 import javax.rmi.PortableRemoteObject JavaDoc;
43 import javax.transaction.Transaction JavaDoc;
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 /**
51  * Common code for all Message Driven Beans
52  * @author Philippe Durieux, Philippe Coq
53  */

54 public abstract class Listener extends JBean implements MessageDrivenBean JavaDoc, MessageListener JavaDoc, TimedObject JavaDoc {
55     protected static Logger logger = null;
56     protected transient MessageDrivenContext JavaDoc mdbContext;
57     protected transient MRecordHome arh = null;
58     protected String JavaDoc myname;
59
60     abstract protected String JavaDoc getMyDest();
61     
62     public static final int CREATE_TIMER_MIN = 1500;
63     public static final int CREATE_TIMER_MAX = 1600;
64
65     /**
66      * Default constructor
67      */

68     public Listener() {
69     }
70
71     // ------------------------------------------------------------------
72
// MessageDrivenBean implementation
73
// ------------------------------------------------------------------
74

75     /**
76      * Set the associated context. The container call this method
77      * after the instance creation.
78      * The enterprise Bean instance should store the reference to the context
79      * object in an instance variable.
80      * This method is called with no transaction context.
81      *
82      * @param ctx A MessageDrivenContext interface for the instance.
83      * @throws EJBException Thrown by the method to indicate a failure caused by
84      * a system-level error.
85      */

86
87     public void setMessageDrivenContext(MessageDrivenContext JavaDoc 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     /**
96      * A container invokes this method before it ends the life of the message-driven object.
97      * This happens when a container decides to terminate the message-driven object.
98      *
99      * This method is called with no transaction context.
100      *
101      * @throws EJBException Thrown by the method to indicate a failure caused by
102      * a system-level error.
103      */

104     public void ejbRemove() {
105         logger.log(BasicLevel.DEBUG, "");
106     }
107
108     /**
109      * The Message driven bean must define an ejbCreate methods with no args.
110      */

111     public void ejbCreate() {
112         logger.log(BasicLevel.DEBUG, "");
113
114         // Get a ref on MRecordHome
115
InitialContext JavaDoc ictx = null;
116         try {
117             ictx = new InitialContext JavaDoc();
118             arh = (MRecordHome) PortableRemoteObject.narrow(ictx.lookup("messageMRecordECHome"), MRecordHome.class);
119         } catch (NamingException JavaDoc e) {
120             logger.log(BasicLevel.ERROR, "Listener : Cannot get messageMRecordHome:" + e);
121         }
122
123         // Accesses the bean env to get the bean name
124
// This test also that we can access java:comp/env from here.
125
try {
126             myname = (String JavaDoc) ictx.lookup("java:comp/env/mdbname");
127         } catch (NamingException JavaDoc e) {
128             logger.log(BasicLevel.ERROR, "Listener : Cannot access java:comp/env/mdbname from ejbCreate:" + e);
129         }
130     }
131
132     /**
133      * onMessage method
134      */

135     public void onMessage(Message JavaDoc message) {
136         logger.log(BasicLevel.DEBUG, "");
137
138         sleep(20);
139
140         // Decode the message (MapMessage)
141
String JavaDoc uuid = null;
142         String JavaDoc dest = null;
143         int value = 0;
144         MapMessage JavaDoc msg = (MapMessage JavaDoc) message;
145         try {
146             uuid = msg.getString("Id");
147             dest = msg.getString("Text");
148             value = msg.getInt("Value");
149         } catch (JMSException JavaDoc e) {
150             logger.log(BasicLevel.ERROR, "Listener exception:" + e);
151             return;
152         }
153
154         // Check destination
155
if (! dest.equals(getMyDest())) {
156             logger.log(BasicLevel.ERROR, "Bad destination: " + dest + ". Expected is " + getMyDest());
157             return;
158         }
159
160         // Create a timer if required
161
if (value >= CREATE_TIMER_MIN && value <= CREATE_TIMER_MAX) {
162             TimerService JavaDoc timerservice = mdbContext.getTimerService();
163             int dur = value - CREATE_TIMER_MIN;
164             Info info = new Info(uuid, dest, value, myname);
165             Timer JavaDoc mt = timerservice.createTimer(dur * 1000, dur * 1000, info);
166             return;
167         }
168
169         // Create a new Entity bean for this message
170
// Check that transaction association did not change after create.
171
try {
172             Transaction JavaDoc 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 JavaDoc e) {
178             logger.log(BasicLevel.ERROR, "Listener exception:" + e);
179         } catch (RemoteException JavaDoc e) {
180             logger.log(BasicLevel.ERROR, "Listener exception:" + e);
181         }
182
183         
184     }
185
186     // -----------------------------------------------------------
187
// TimedObject implementation
188
// -----------------------------------------------------------
189

190     /**
191      * A timer is expired.
192      */

193     public void ejbTimeout(Timer JavaDoc timer) {
194         logger.log(BasicLevel.DEBUG, "");
195         Info info = (Info) timer.getInfo();
196         // Create a new Entity bean for this message
197
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 JavaDoc e) {
206             logger.log(BasicLevel.ERROR, "Listener exception :" + e);
207         } catch (FinderException JavaDoc e) {
208             try {
209                 arh.create(info.uuid, info.dest, info.value, info.ejbname);
210             } catch (CreateException JavaDoc ee) {
211                 logger.log(BasicLevel.ERROR, "Listener exception:" + ee);
212             } catch (RemoteException JavaDoc ee) {
213                 logger.log(BasicLevel.ERROR, "Listener exception on create:" + ee);
214             }
215         }
216         
217     }
218     
219 }
220
Popular Tags