KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > timer > ejb > TimerEntityBean


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.timer.ejb;
23
24 import java.util.Date JavaDoc;
25 import javax.ejb.EJBException JavaDoc;
26 import javax.ejb.EntityBean JavaDoc;
27 import javax.ejb.EntityContext JavaDoc;
28 import javax.ejb.TimedObject JavaDoc;
29 import javax.ejb.Timer JavaDoc;
30 import javax.ejb.TimerHandle JavaDoc;
31 import javax.ejb.TimerService JavaDoc;
32
33 import org.jboss.logging.Logger;
34
35 /**
36  * Entity Bean Timer Test
37  *
38  * @ejb:bean name="test/timer/TimerEntity"
39  * display-name="Timer in Entity Bean"
40  * type="BMP"
41  * transaction-type="Container"
42  * view-type="remote"
43  * jndi-name="ejb/test/timer/TimerEntity"
44  *
45  * @ejb:pk class="java.lang.Integer"
46  *
47  * @ejb:transaction type="Required"
48  * @author Thomas Diesler
49  * @author Scott.Stark@jboss.org
50  * @version $Revision: 37406 $
51  **/

52 public class TimerEntityBean
53         implements EntityBean JavaDoc, TimedObject JavaDoc
54 {
55
56    // -------------------------------------------------------------------------
57
// Members
58
// -------------------------------------------------------------------------
59

60    private EntityContext JavaDoc mContext;
61    private Timer JavaDoc sTimer;
62    private int sCounter;
63
64    private Logger mLog = Logger.getLogger(this.getClass().getName());
65
66    // -------------------------------------------------------------------------
67
// Methods
68
// -------------------------------------------------------------------------
69

70    /**
71     * Start a single timer (if not already set) with the start date plus the period
72     *
73     * @param pPeriod Time that will elapse between now and the timed event in milliseconds
74     *
75     * @ejb:interface-method view-type="remote"
76     **/

77    public void startSingleTimer(long pPeriod)
78    {
79       mLog.info("TimerEntityBean.startSingleTimer(), try to get a Timer Service from the Entity Context");
80       TimerService JavaDoc lService = mContext.getTimerService();
81       mLog.info("TimerEntityBean.startSingleTimer(), create a timer if not already done");
82       if (sTimer == null)
83       {
84          sTimer = lService.createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + pPeriod), "TimerEntityBean");
85          sCounter = 0;
86       }
87       else
88       {
89          throw new EJBException JavaDoc("Timer is already set");
90       }
91    }
92
93    /**
94     * Start a timer (if not already set) with the start date plus the period
95     * and an interval of the given period
96     *
97     * @param pPeriod Time that will elapse between two events in milliseconds
98     *
99     * @ejb:interface-method view-type="remote"
100     **/

101    public void startTimer(long pPeriod)
102    {
103       mLog.info("TimerEntityBean.startTimer(), try to get a Timer Service from the Entity Context");
104       TimerService JavaDoc lService = mContext.getTimerService();
105       mLog.info("TimerEntityBean.startTimer(), create a timer if not already done");
106       if (sTimer == null)
107       {
108          sTimer = lService.createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + pPeriod), pPeriod, "TimerEntityBean");
109          sCounter = 0;
110       }
111       else
112       {
113          throw new EJBException JavaDoc("Timer is already set");
114       }
115    }
116
117    /**
118     * @ejb:interface-method view-type="remote"
119     **/

120    public void stopTimer()
121    {
122       try
123       {
124          if (sTimer != null)
125          {
126             sTimer.cancel();
127          }
128          else
129          {
130             throw new EJBException JavaDoc("Timer is not available");
131          }
132       }
133       finally
134       {
135          sTimer = null;
136       }
137    }
138
139    /**
140     * @ejb:interface-method view-type="remote"
141     **/

142    public int getTimeoutCount()
143    {
144       mLog.info("TimerEntityBean.getTimeoutCount(): " + sCounter);
145       return sCounter;
146    }
147
148    /**
149     * @return Date of the next timed event
150     *
151     * @ejb:interface-method view-type="remote"
152     **/

153    public Date JavaDoc getNextTimeout()
154    {
155       if (sTimer != null)
156       {
157          return sTimer.getNextTimeout();
158       }
159       else
160       {
161          return null;
162       }
163    }
164
165    /**
166     * @return Time remaining until next timed event in milliseconds
167     *
168     * @ejb:interface-method view-type="remote"
169     **/

170    public long getTimeRemaining()
171    {
172       if (sTimer != null)
173       {
174          return sTimer.getTimeRemaining();
175       }
176       else
177       {
178          return -1L;
179       }
180    }
181
182    /**
183     * @return User object of the timer
184     *
185     * @ejb:interface-method view-type="remote"
186     **/

187    public Object JavaDoc getInfo()
188    {
189       if (sTimer != null)
190       {
191          return (Object JavaDoc) sTimer.getInfo();
192       }
193       else
194       {
195          return null;
196       }
197    }
198
199    /**
200     * @return Date of the next timed event
201     *
202     * @ejb:interface-method view-type="remote"
203     **/

204    public TimerHandle JavaDoc getTimerHandle()
205    {
206       if (sTimer != null)
207       {
208          return sTimer.getHandle();
209       }
210       else
211       {
212          return null;
213       }
214    }
215
216    public void ejbTimeout(Timer JavaDoc pTimer)
217    {
218       mLog.debug("ejbTimeout(), timer: " + pTimer);
219       sCounter++;
220    }
221
222    /**
223     * Describes the instance and its content for debugging purpose
224     *
225     * @return Debugging information about the instance and its content
226     **/

227    public String JavaDoc toString()
228    {
229       return "TimerEntityBean [ " + " ]";
230    }
231
232    // -------------------------------------------------------------------------
233
// Framework Callbacks
234
// -------------------------------------------------------------------------
235

236    /**
237     * @ejb:create-method view-type="both"
238     **/

239    public Integer JavaDoc ejbCreate(Integer JavaDoc pk)
240    {
241       mLog.info("ejbCreate(" + pk + ")");
242       return pk;
243    }
244
245    public void ejbPostCreate(Integer JavaDoc pk)
246    {
247       mLog.info("ejbPostCreate(" + pk + ")");
248    }
249
250    /**
251     * @ejb:finder view-type="both"
252     **/

253    public Integer JavaDoc ejbFindByPrimaryKey(Integer JavaDoc pk)
254    {
255       mLog.info("ejbFindByPrimaryKey(" + pk + ")");
256       return pk;
257    }
258
259    public void ejbActivate()
260    {
261       mLog.info("ejbActivate");
262    }
263
264    public void ejbLoad()
265    {
266       mLog.info("ejbLoad");
267    }
268
269    public void ejbPassivate()
270    {
271       mLog.info("ejbPassivate");
272    }
273
274    public void ejbRemove()
275    {
276       mLog.info("ejbRemove");
277    }
278
279    public void ejbStore()
280    {
281       mLog.info("ejbStore");
282    }
283
284    public void setEntityContext(EntityContext JavaDoc ctx)
285    {
286       mLog.info("setEntityContext");
287       this.mContext = ctx;
288    }
289
290    public void unsetEntityContext()
291    {
292       mLog.info("unsetEntityContext");
293    }
294 }
295
Popular Tags