001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.txtimer.ejb;
023:
024: import java.io.Serializable;
025: import java.rmi.RemoteException;
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.ejb.CreateException;
030: import javax.ejb.EJBException;
031: import javax.ejb.SessionBean;
032: import javax.ejb.SessionContext;
033: import javax.ejb.TimedObject;
034: import javax.ejb.Timer;
035:
036: import org.jboss.logging.Logger;
037:
038: /**
039: * TxTimer test bean
040: *
041: * @author Dimitris.Andreadis@jboss.org
042: * @version $Revision: 57211 $
043: */
044: public class TimerTestBean implements SessionBean, TimedObject {
045: private Logger log = Logger
046: .getLogger(TimerTestBean.class.getName());
047: private SessionContext context;
048:
049: public TimerTestBean() {
050: // empty
051: }
052:
053: public void setSessionContext(SessionContext newContext)
054: throws EJBException {
055: context = newContext;
056: }
057:
058: public void ejbCreate() throws CreateException {
059: }
060:
061: public void ejbRemove() throws EJBException, RemoteException {
062: }
063:
064: public void ejbActivate() throws EJBException, RemoteException {
065: }
066:
067: public void ejbPassivate() throws EJBException, RemoteException {
068: }
069:
070: public void ejbTimeout(Timer timer) {
071: log.info("ejbTimeout: " + timer);
072: }
073:
074: // Business Interface --------------------------------------------
075:
076: /**
077: * @ejb.interface-method
078: * @ejb.transaction type = "Required"
079: * @throws EJBException Thrown if method fails due to system-level error.
080: */
081: public void startTimerInTxRequired() throws EJBException {
082: startTimer("Required");
083: }
084:
085: /**
086: * @ejb.interface-method
087: * @ejb.transaction type = "RequiresNew"
088: * @throws EJBException Thrown if method fails due to system-level error.
089: */
090: public void startTimerInTxRequiresNew() throws EJBException {
091: startTimer("RequiresNew");
092: }
093:
094: /**
095: * @ejb.interface-method
096: * @ejb.transaction type = "Never"
097: * @throws EJBException Thrown if method fails due to system-level error.
098: */
099: public void startTimerInTxNever() throws EJBException {
100: startTimer("Never");
101: }
102:
103: /**
104: * @ejb.interface-method
105: * @ejb.transaction type = "NotSupported"
106: * @throws EJBException Thrown if method fails due to system-level error.
107: */
108: public void startTimerInTxNotSupported() throws EJBException {
109: startTimer("NotSupported");
110: }
111:
112: /**
113: * @ejb.interface-method
114: */
115: public int listAllTimers() {
116: Collection timers = context.getTimerService().getTimers();
117: String s = "Timers: ";
118: for (Iterator it = timers.iterator(); it.hasNext();) {
119: Timer t = (Timer) it.next();
120: s = s + t.toString() + " ";
121: try {
122: s += t.getInfo();
123: } catch (Exception ignore) {
124: // empty
125: }
126: s += "\n";
127: }
128: log.info(s);
129:
130: return timers.size();
131: }
132:
133: /**
134: * @ejb.interface-method
135: * @ejb.transaction type = "Required"
136: * @throws EJBException Thrown if method fails due to system-level error.
137: */
138: public void cancelTimerInTxRequired() throws EJBException {
139: cancelTimers();
140: }
141:
142: /**
143: * @ejb.interface-method
144: * @ejb.transaction type = "RequiresNew"
145: * @throws EJBException Thrown if method fails due to system-level error.
146: */
147: public void cancelTimerInTxRequiresNew() throws EJBException {
148: cancelTimers();
149: }
150:
151: /**
152: * @ejb.interface-method
153: * @ejb.transaction type = "Never"
154: * @throws EJBException Thrown if method fails due to system-level error.
155: */
156: public void cancelTimerInTxNever() throws EJBException {
157: cancelTimers();
158: }
159:
160: /**
161: * @ejb.interface-method
162: * @ejb.transaction type = "NotSupported"
163: * @throws EJBException Thrown if method fails due to system-level error.
164: */
165: public void cancelTimerInTxNotSupported() throws EJBException {
166: cancelTimers();
167: }
168:
169: // Private -------------------------------------------------------
170:
171: private void startTimer(Serializable info) {
172: log.info("Starting timer, info=" + info);
173: context.getTimerService().createTimer(10000, info);
174: }
175:
176: private void cancelTimers() {
177: Collection timers = context.getTimerService().getTimers();
178: for (Iterator it = timers.iterator(); it.hasNext();) {
179: Timer t = (Timer) it.next();
180: log.info("Cancelling timer " + t + " " + t.getInfo());
181: t.cancel();
182: log.info("Timer is now " + t);
183: }
184: }
185:
186: }
|