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: WorkerSF.java 6515 2005-04-07 09:37:22Z durieuxp $
023: * --------------------------------------------------------------------------
024: */
025:
026: package org.objectweb.jonas.jtests.beans.worker;
027:
028: import java.rmi.RemoteException;
029:
030: import javax.ejb.CreateException;
031: import javax.ejb.EJBException;
032: import javax.ejb.SessionBean;
033: import javax.ejb.SessionContext;
034: import javax.resource.spi.work.Work;
035: import javax.resource.spi.work.WorkEvent;
036: import javax.resource.spi.work.WorkException;
037: import javax.resource.spi.work.WorkListener;
038: import javax.resource.spi.work.WorkManager;
039:
040: import org.objectweb.jonas.common.Log;
041: import org.objectweb.jonas_ejb.container.JSessionContext;
042: import org.objectweb.util.monolog.api.BasicLevel;
043: import org.objectweb.util.monolog.api.Logger;
044:
045: /**
046: * Worker Implementation.
047: * This bean is JONAS specific because it uses the JSessionContext
048: * to retrieve the WorkManager.
049: * @author Philippe Durieux
050: */
051: public class WorkerSF implements SessionBean, Work, WorkListener {
052:
053: private static final long serialVersionUID = 1L;
054: private static Logger logger = null;
055: private JSessionContext ejbContext;
056: private WorkManager workManager;
057: private int wcount;
058: private int notifyAccepted = 0;
059: private int notifyStarted = 0;
060: private int notifyRejected = 0;
061: private int notifyCompleted = 0;
062:
063: // ------------------------------------------------------------------
064: // SessionBean implementation
065: // ------------------------------------------------------------------
066:
067: /**
068: * Set the associated session context. The container calls this method
069: * after the instance creation.
070: * The enterprise Bean instance should store the reference to the context
071: * object in an instance variable.
072: * This method is called with no transaction context.
073: *
074: * @param ctx A SessionContext interface for the instance.
075: * @throws EJBException Thrown by the method to indicate a failure caused by
076: * a system-level error.
077: */
078: public void setSessionContext(SessionContext ctx) {
079: if (logger == null) {
080: logger = Log.getLogger(Log.JONAS_TESTS_PREFIX);
081: }
082: logger.log(BasicLevel.DEBUG, "");
083: ejbContext = (JSessionContext) ctx;
084: }
085:
086: /**
087: * A container invokes this method before it ends the life of the session object.
088: * This happens as a result of a client's invoking a remove operation, or when a
089: * container decides to terminate the session object after a timeout.
090: * This method is called with no transaction context.
091: *
092: * @throws EJBException Thrown by the method to indicate a failure caused by
093: * a system-level error.
094: */
095: public void ejbRemove() {
096: logger.log(BasicLevel.DEBUG, "");
097: }
098:
099: /**
100: * Create a session.
101: * @throws CreateException Failure to create a session EJB object.
102: */
103: public void ejbCreate() throws CreateException {
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 {
146: wcount = c;
147: }
148:
149: /**
150: * get the count
151: */
152: public int getwcount() throws RemoteException {
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 {
188: logger.log(BasicLevel.DEBUG, "");
189: for (int i = 0; i < n; i++) {
190: try {
191: getWM().doWork(this );
192: } catch (WorkException e) {
193: throw new RemoteException("doWork failed:" + e);
194: }
195: }
196: }
197:
198: /**
199: * Start n works
200: */
201: public void startWorks(int n) throws RemoteException {
202: logger.log(BasicLevel.DEBUG, "");
203: for (int i = 0; i < n; i++) {
204: try {
205: getWM().startWork(this );
206: } catch (WorkException e) {
207: throw new RemoteException("doWork failed:" + e);
208: }
209: }
210: }
211:
212: /**
213: * Schedule n works
214: */
215: public void scheduleWorks(int n, long timeout)
216: throws RemoteException {
217: logger.log(BasicLevel.DEBUG, "");
218: for (int i = 0; i < n; i++) {
219: try {
220: getWM().scheduleWork(this , timeout, null, this );
221: } catch (WorkException e) {
222: throw new RemoteException("doWork failed:" + e);
223: }
224: }
225: }
226:
227: /**
228: * Run 1 work synchronously in a TX
229: * The tx attribute is set as "Required"
230: */
231: public void doWorkInTx() throws RemoteException {
232: logger.log(BasicLevel.DEBUG, "");
233: try {
234: getWM().doWork(this );
235: } catch (WorkException e) {
236: throw new RemoteException("doWork failed:" + e);
237: }
238: }
239:
240: /**
241: * Start 1 work in a TX
242: * The tx attribute is set as "Required"
243: */
244: public void startWorkInTx() throws RemoteException {
245: logger.log(BasicLevel.DEBUG, "");
246: try {
247: getWM().startWork(this );
248: } catch (WorkException e) {
249: throw new RemoteException("doWork failed:" + e);
250: }
251: }
252:
253: /**
254: * Schedule 1 work in a TX
255: * The tx attribute is set as "Required"
256: */
257: public void scheduleWorkInTx() throws RemoteException {
258: logger.log(BasicLevel.DEBUG, "");
259: try {
260: getWM().scheduleWork(this );
261: } catch (WorkException e) {
262: throw new RemoteException("doWork failed:" + e);
263: }
264: }
265:
266: // ------------------------------------------------------------------
267: // Private Methods
268: // ------------------------------------------------------------------
269:
270: /**
271: * Get the WorkManager
272: */
273: private WorkManager getWM() {
274: if (workManager == null) {
275: workManager = ejbContext.getWorkManager();
276: }
277: return workManager;
278: }
279:
280: /**
281: * sleep n seconds
282: * @param n seconds
283: */
284: private void sleep(int n) {
285: try {
286: Thread.sleep(1000 * n);
287: } catch (InterruptedException e) {
288: }
289: }
290:
291: // ------------------------------------------------------------------------
292: // WorkListener implementation
293: // ------------------------------------------------------------------------
294:
295: public void workAccepted(WorkEvent we) {
296: logger.log(BasicLevel.DEBUG, "");
297: notifyAccepted++;
298: }
299:
300: public void workRejected(WorkEvent we) {
301: logger.log(BasicLevel.DEBUG, "");
302: notifyRejected++;
303: }
304:
305: public void workStarted(WorkEvent we) {
306: logger.log(BasicLevel.DEBUG, "");
307: notifyStarted++;
308: }
309:
310: public void workCompleted(WorkEvent we) {
311: logger.log(BasicLevel.DEBUG, "");
312: notifyCompleted++;
313: }
314:
315: }
|