KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > txtimer > ejb > TimerTestBean


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.test.txtimer.ejb;
23
24 import java.io.Serializable JavaDoc;
25 import java.rmi.RemoteException JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28
29 import javax.ejb.CreateException JavaDoc;
30 import javax.ejb.EJBException JavaDoc;
31 import javax.ejb.SessionBean JavaDoc;
32 import javax.ejb.SessionContext JavaDoc;
33 import javax.ejb.TimedObject JavaDoc;
34 import javax.ejb.Timer JavaDoc;
35
36 import org.jboss.logging.Logger;
37
38 /**
39  * TxTimer test bean
40  *
41  * @author Dimitris.Andreadis@jboss.org
42  * @version $Revision: 38205 $
43  */

44 public class TimerTestBean implements SessionBean JavaDoc, TimedObject JavaDoc
45 {
46    private Logger log = Logger.getLogger(TimerTestBean.class.getName());
47    private SessionContext JavaDoc context;
48
49    public TimerTestBean()
50    {
51       // empty
52
}
53    
54    public void setSessionContext(SessionContext JavaDoc newContext) throws EJBException JavaDoc
55    {
56       context = newContext;
57    }
58
59    public void ejbCreate() throws CreateException JavaDoc {}
60    public void ejbRemove() throws EJBException JavaDoc, RemoteException JavaDoc {}
61    public void ejbActivate() throws EJBException JavaDoc, RemoteException JavaDoc {}
62    public void ejbPassivate() throws EJBException JavaDoc, RemoteException JavaDoc {}
63     
64    public void ejbTimeout(Timer JavaDoc timer)
65    {
66       log.info("ejbTimeout: " + timer);
67    }
68    
69    // Business Interface --------------------------------------------
70

71    /**
72     * @ejb.interface-method
73     * @ejb.transaction type = "Required"
74     * @throws EJBException Thrown if method fails due to system-level error.
75     */

76    public void startTimerInTxRequired() throws EJBException JavaDoc
77    {
78       startTimer("Required");
79    }
80     
81    /**
82     * @ejb.interface-method
83     * @ejb.transaction type = "RequiresNew"
84     * @throws EJBException Thrown if method fails due to system-level error.
85     */

86    public void startTimerInTxRequiresNew() throws EJBException JavaDoc
87    {
88       startTimer("RequiresNew");
89    }
90    
91    /**
92     * @ejb.interface-method
93     * @ejb.transaction type = "Never"
94     * @throws EJBException Thrown if method fails due to system-level error.
95     */

96    public void startTimerInTxNever() throws EJBException JavaDoc
97    {
98       startTimer("Never");
99    }
100     
101    /**
102     * @ejb.interface-method
103     * @ejb.transaction type = "NotSupported"
104     * @throws EJBException Thrown if method fails due to system-level error.
105     */

106    public void startTimerInTxNotSupported() throws EJBException JavaDoc
107    {
108       startTimer("NotSupported");
109    }
110
111    /**
112     * @ejb.interface-method
113     */

114    public int listAllTimers()
115    {
116       Collection JavaDoc timers = context.getTimerService().getTimers();
117       String JavaDoc s = "Timers: ";
118       for (Iterator JavaDoc it = timers.iterator();it.hasNext();)
119       {
120          Timer JavaDoc t = (Timer JavaDoc)it.next();
121          s = s + t.toString() + " ";
122          try
123          {
124             s += t.getInfo();
125          }
126          catch (Exception JavaDoc ignore)
127          {
128             // empty
129
}
130          s += "\n";
131        }
132        log.info(s);
133        
134        return timers.size();
135    }
136
137    /**
138     * @ejb.interface-method
139     * @ejb.transaction type = "Required"
140     * @throws EJBException Thrown if method fails due to system-level error.
141     */

142    public void cancelTimerInTxRequired() throws EJBException JavaDoc
143    {
144       cancelTimers();
145    }
146    
147    /**
148     * @ejb.interface-method
149     * @ejb.transaction type = "RequiresNew"
150     * @throws EJBException Thrown if method fails due to system-level error.
151     */

152    public void cancelTimerInTxRequiresNew() throws EJBException JavaDoc
153    {
154        cancelTimers();
155    }
156    
157    /**
158     * @ejb.interface-method
159     * @ejb.transaction type = "Never"
160     * @throws EJBException Thrown if method fails due to system-level error.
161     */

162    public void cancelTimerInTxNever() throws EJBException JavaDoc
163    {
164        cancelTimers();
165    }
166    
167    /**
168     * @ejb.interface-method
169     * @ejb.transaction type = "NotSupported"
170     * @throws EJBException Thrown if method fails due to system-level error.
171     */

172    public void cancelTimerInTxNotSupported() throws EJBException JavaDoc
173    {
174        cancelTimers();
175    }
176    
177    // Private -------------------------------------------------------
178

179    private void startTimer(Serializable JavaDoc info)
180    {
181       log.info("Starting timer, info=" + info);
182       context.getTimerService().createTimer(10000, info);
183    }
184    
185    private void cancelTimers()
186    {
187       Collection JavaDoc timers = context.getTimerService().getTimers();
188       for (Iterator JavaDoc it = timers.iterator(); it.hasNext(); )
189       {
190          Timer JavaDoc t = (Timer JavaDoc)it.next();
191          log.info("Cancelling timer " + t + " " + t.getInfo());
192          t.cancel();
193          log.info("Timer is now " + t);
194       }
195    }
196
197 }
198
Popular Tags