KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jonas > jtests > beans > worker > WorkerSF


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: WorkerSF.java,v 1.1 2005/04/07 09:37:22 durieuxp Exp $
23  * --------------------------------------------------------------------------
24  */

25
26 package org.objectweb.jonas.jtests.beans.worker;
27
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.ejb.CreateException JavaDoc;
31 import javax.ejb.EJBException JavaDoc;
32 import javax.ejb.SessionBean JavaDoc;
33 import javax.ejb.SessionContext JavaDoc;
34 import javax.resource.spi.work.Work JavaDoc;
35 import javax.resource.spi.work.WorkEvent JavaDoc;
36 import javax.resource.spi.work.WorkException JavaDoc;
37 import javax.resource.spi.work.WorkListener JavaDoc;
38 import javax.resource.spi.work.WorkManager JavaDoc;
39
40 import org.objectweb.jonas.common.Log;
41 import org.objectweb.jonas_ejb.container.JSessionContext;
42 import org.objectweb.util.monolog.api.BasicLevel;
43 import org.objectweb.util.monolog.api.Logger;
44
45 /**
46  * Worker Implementation.
47  * This bean is JONAS specific because it uses the JSessionContext
48  * to retrieve the WorkManager.
49  * @author Philippe Durieux
50  */

51 public class WorkerSF implements SessionBean JavaDoc, Work JavaDoc, WorkListener JavaDoc {
52
53     private static final long serialVersionUID = 1L;
54     private static Logger logger = null;
55     private JSessionContext ejbContext;
56     private WorkManager JavaDoc workManager;
57     private int wcount;
58     private int notifyAccepted = 0;
59     private int notifyStarted = 0;
60     private int notifyRejected = 0;
61     private int notifyCompleted = 0;
62
63     // ------------------------------------------------------------------
64
// SessionBean implementation
65
// ------------------------------------------------------------------
66

67     /**
68      * Set the associated session context. The container calls this method
69      * after the instance creation.
70      * The enterprise Bean instance should store the reference to the context
71      * object in an instance variable.
72      * This method is called with no transaction context.
73      *
74      * @param ctx A SessionContext interface for the instance.
75      * @throws EJBException Thrown by the method to indicate a failure caused by
76      * a system-level error.
77      */

78     public void setSessionContext(SessionContext JavaDoc ctx) {
79         if (logger == null) {
80             logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
81         }
82         logger.log(BasicLevel.DEBUG, "");
83         ejbContext = (JSessionContext) ctx;
84     }
85
86     /**
87      * A container invokes this method before it ends the life of the session object.
88      * This happens as a result of a client's invoking a remove operation, or when a
89      * container decides to terminate the session object after a timeout.
90      * This method is called with no transaction context.
91      *
92      * @throws EJBException Thrown by the method to indicate a failure caused by
93      * a system-level error.
94      */

95     public void ejbRemove() {
96         logger.log(BasicLevel.DEBUG, "");
97     }
98
99     /**
100      * Create a session.
101      * @throws CreateException Failure to create a session EJB object.
102      */

103     public void ejbCreate() throws CreateException JavaDoc {
104         logger.log(BasicLevel.DEBUG, "");
105         wcount = 0;
106     }
107
108     /**
109      * A container invokes this method on an instance before the instance
110      * becomes disassociated with a specific EJB object.
111      */

112     public void ejbPassivate() {
113         logger.log(BasicLevel.DEBUG, "");
114     }
115
116     /**
117      * A container invokes this method when the instance is taken out of
118      * the pool of available instances to become associated with a specific
119      * EJB object.
120      */

121     public void ejbActivate() {
122         logger.log(BasicLevel.DEBUG, "");
123     }
124
125     // ------------------------------------------------------------------
126
// Work implementation
127
// ------------------------------------------------------------------
128

129     public void release() {
130         logger.log(BasicLevel.DEBUG, "");
131     }
132
133     public void run() {
134         logger.log(BasicLevel.DEBUG, "");
135         wcount++;
136     }
137
138     // ------------------------------------------------------------------
139
// Worker implementation
140
// ------------------------------------------------------------------
141

142     /**
143      * set the count
144      */

145     public void setwcount(int c) throws RemoteException JavaDoc {
146         wcount = c;
147     }
148
149     /**
150      * get the count
151      */

152     public int getwcount() throws RemoteException JavaDoc {
153         return wcount;
154     }
155
156     /**
157      * @return the notify-accepted count
158      */

159     public int getNotifyAccepted() {
160         return notifyAccepted;
161     }
162
163     /**
164      * @return the notify-rejected count
165      */

166     public int getNotifyRejected() {
167         return notifyRejected;
168     }
169
170     /**
171      * @return the notify-completed count
172      */

173     public int getNotifyCompleted() {
174         return notifyCompleted;
175     }
176
177     /**
178      * @return the notify-started count
179      */

180     public int getNotifyStarted() {
181         return notifyStarted;
182     }
183
184     /**
185      * Run n works, synchronously.
186      */

187     public void doWorks(int n) throws RemoteException JavaDoc {
188         logger.log(BasicLevel.DEBUG, "");
189         for (int i = 0; i < n; i++) {
190             try {
191                 getWM().doWork(this);
192             } catch (WorkException JavaDoc e) {
193                 throw new RemoteException JavaDoc("doWork failed:" + e);
194             }
195         }
196     }
197
198     /**
199      * Start n works
200      */

201     public void startWorks(int n) throws RemoteException JavaDoc {
202         logger.log(BasicLevel.DEBUG, "");
203         for (int i = 0; i < n; i++) {
204             try {
205                 getWM().startWork(this);
206             } catch (WorkException JavaDoc e) {
207                 throw new RemoteException JavaDoc("doWork failed:" + e);
208             }
209         }
210     }
211
212     /**
213      * Schedule n works
214      */

215     public void scheduleWorks(int n, long timeout) throws RemoteException JavaDoc {
216         logger.log(BasicLevel.DEBUG, "");
217         for (int i = 0; i < n; i++) {
218             try {
219                 getWM().scheduleWork(this, timeout, null, this);
220             } catch (WorkException JavaDoc e) {
221                 throw new RemoteException JavaDoc("doWork failed:" + e);
222             }
223         }
224     }
225
226     /**
227      * Run 1 work synchronously in a TX
228      * The tx attribute is set as "Required"
229      */

230     public void doWorkInTx() throws RemoteException JavaDoc {
231         logger.log(BasicLevel.DEBUG, "");
232         try {
233             getWM().doWork(this);
234         } catch (WorkException JavaDoc e) {
235             throw new RemoteException JavaDoc("doWork failed:" + e);
236         }
237     }
238
239     /**
240      * Start 1 work in a TX
241      * The tx attribute is set as "Required"
242      */

243     public void startWorkInTx() throws RemoteException JavaDoc {
244         logger.log(BasicLevel.DEBUG, "");
245         try {
246             getWM().startWork(this);
247         } catch (WorkException JavaDoc e) {
248             throw new RemoteException JavaDoc("doWork failed:" + e);
249         }
250     }
251
252     /**
253      * Schedule 1 work in a TX
254      * The tx attribute is set as "Required"
255      */

256     public void scheduleWorkInTx() throws RemoteException JavaDoc {
257         logger.log(BasicLevel.DEBUG, "");
258         try {
259             getWM().scheduleWork(this);
260         } catch (WorkException JavaDoc e) {
261             throw new RemoteException JavaDoc("doWork failed:" + e);
262         }
263     }
264
265     // ------------------------------------------------------------------
266
// Private Methods
267
// ------------------------------------------------------------------
268

269     /**
270      * Get the WorkManager
271      */

272     private WorkManager JavaDoc getWM() {
273         if (workManager == null) {
274             workManager = ejbContext.getWorkManager();
275         }
276         return workManager;
277     }
278
279     /**
280      * sleep n seconds
281      * @param n seconds
282      */

283     private void sleep(int n) {
284         try {
285             Thread.sleep(1000 * n);
286         } catch (InterruptedException JavaDoc e) {
287         }
288     }
289
290     // ------------------------------------------------------------------------
291
// WorkListener implementation
292
// ------------------------------------------------------------------------
293

294     public void workAccepted(WorkEvent JavaDoc we) {
295         logger.log(BasicLevel.DEBUG, "");
296         notifyAccepted++;
297     }
298
299     public void workRejected(WorkEvent JavaDoc we) {
300         logger.log(BasicLevel.DEBUG, "");
301         notifyRejected++;
302     }
303
304     public void workStarted(WorkEvent JavaDoc we) {
305         logger.log(BasicLevel.DEBUG, "");
306         notifyStarted++;
307     }
308
309     public void workCompleted(WorkEvent JavaDoc we) {
310         logger.log(BasicLevel.DEBUG, "");
311         notifyCompleted++;
312     }
313
314 }
315
Popular Tags