01: /**
02: * EasyBeans
03: * Copyright (C) 2007 Bull S.A.S.
04: * Contact: easybeans@ow2.org
05: *
06: * This library is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU Lesser General Public
08: * License as published by the Free Software Foundation; either
09: * 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: TimedObjectBean.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.examples.timerservice;
25:
26: import java.util.Date;
27:
28: import javax.annotation.Resource;
29: import javax.ejb.Stateless;
30: import javax.ejb.TimedObject;
31: import javax.ejb.Timer;
32: import javax.ejb.TimerService;
33:
34: /**
35: * Bean that implements the TimedObject interface. <br/>
36: * The timeout method will be ejbTimeout().
37: * @author Florent Benoit
38: */
39: @Stateless
40: public class TimedObjectBean implements TimedObject, TimedLocal {
41:
42: /**
43: * Time before firing a new Timer.
44: */
45: private static final long FIRING_TIME = 3000;
46:
47: /**
48: * Timer service. (injected by a setter method)
49: */
50: private TimerService timerService;
51:
52: /**
53: * Local method that is used by the other bean in order to start a timer on this bean.
54: */
55: public void startTimer() {
56: Date expiration = new Date(System.currentTimeMillis()
57: + FIRING_TIME);
58: timerService.createTimer(expiration,
59: "Timer on TimedBean object");
60: }
61:
62: /**
63: * Invoked by the EJB container upon timer expiration.
64: * @param timer timer whose expiration caused this notification.
65: */
66: public void ejbTimeout(final Timer timer) {
67: System.out.println(" TimedBean -> Got a timer with value '"
68: + timer + "'.");
69: }
70:
71: /**
72: * Use a setter method to inject the timer service.
73: * @param timerService the instance of the timer service.
74: */
75: @Resource
76: public void setTimerService(final TimerService timerService) {
77: this.timerService = timerService;
78: }
79: }
|