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: MessageDrivenBean.java 1970 2007-10-16 11:49:25Z benoitf $
23: * --------------------------------------------------------------------------
24: */package org.ow2.easybeans.examples.timerservice;
25:
26: import javax.annotation.Resource;
27: import javax.ejb.ActivationConfigProperty;
28: import javax.ejb.MessageDriven;
29: import javax.ejb.MessageDrivenContext;
30: import javax.ejb.Timeout;
31: import javax.ejb.Timer;
32: import javax.ejb.TimerService;
33: import javax.jms.Message;
34: import javax.jms.MessageListener;
35:
36: /**
37: * Example of Message Driven Bean.<br />
38: * This is an JMS MDB, implementing the MessageListener interface.
39: * @author Florent Benoit
40: */
41: @MessageDriven(activationConfig={@ActivationConfigProperty(propertyName="destination",propertyValue="SampleTimerQueue"),@ActivationConfigProperty(propertyName="destinationType",propertyValue="javax.jms.Queue")})
42: public class MessageDrivenBean implements MessageListener {
43:
44: /**
45: * Time before firing a new Timer.
46: */
47: private static final long FIRING_TIME = 3000;
48:
49: /**
50: * MDB context used to access to the timer service. (timer service can also be injected).
51: */
52: @Resource
53: private MessageDrivenContext messageDrivenContext = null;
54:
55: /**
56: * Passes a message to the listener.
57: * @param message - the message passed to the listener
58: */
59: public void onMessage(final Message message) {
60: // Get the timer service
61: TimerService timerService = messageDrivenContext
62: .getTimerService();
63:
64: // Calling the timer service
65: timerService.createTimer(FIRING_TIME,
66: "Timer started by the onMessage() method");
67: }
68:
69: /**
70: * Timeout method that will be called by the timer service when a timer is expiring.
71: * @param timer the timer object containing some data.
72: */
73: @Timeout
74: public void theMDBtimerMethod(final Timer timer) {
75: System.out
76: .println(" MDB -> Timer method called by the Timer Service.");
77: System.out
78: .println(" MDB -> Timer received = '" + timer + "'.");
79: System.out
80: .println(" MDB -> Info object inside the timer object is '"
81: + timer.getInfo() + "'.");
82: }
83:
84: }
|