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.timer.ejb;
023:
024: import java.util.Date;
025: import javax.ejb.EJBException;
026: import javax.ejb.EntityBean;
027: import javax.ejb.EntityContext;
028: import javax.ejb.TimedObject;
029: import javax.ejb.Timer;
030: import javax.ejb.TimerHandle;
031: import javax.ejb.TimerService;
032:
033: import org.jboss.logging.Logger;
034:
035: /**
036: * Entity Bean Timer Test
037: *
038: * @ejb:bean name="test/timer/TimerEntity"
039: * display-name="Timer in Entity Bean"
040: * type="BMP"
041: * transaction-type="Container"
042: * view-type="remote"
043: * jndi-name="ejb/test/timer/TimerEntity"
044: *
045: * @ejb:pk class="java.lang.Integer"
046: *
047: * @ejb:transaction type="Required"
048: * @author Thomas Diesler
049: * @author Scott.Stark@jboss.org
050: * @version $Revision: 57211 $
051: **/
052: public class TimerEntityBean implements EntityBean, TimedObject {
053:
054: // -------------------------------------------------------------------------
055: // Members
056: // -------------------------------------------------------------------------
057:
058: private EntityContext mContext;
059: private Timer sTimer;
060: private int sCounter;
061:
062: private Logger mLog = Logger.getLogger(this .getClass().getName());
063:
064: // -------------------------------------------------------------------------
065: // Methods
066: // -------------------------------------------------------------------------
067:
068: /**
069: * Start a single timer (if not already set) with the start date plus the period
070: *
071: * @param pPeriod Time that will elapse between now and the timed event in milliseconds
072: *
073: * @ejb:interface-method view-type="remote"
074: **/
075: public void startSingleTimer(long pPeriod) {
076: mLog
077: .info("TimerEntityBean.startSingleTimer(), try to get a Timer Service from the Entity Context");
078: TimerService lService = mContext.getTimerService();
079: mLog
080: .info("TimerEntityBean.startSingleTimer(), create a timer if not already done");
081: if (sTimer == null) {
082: sTimer = lService.createTimer(new Date(new Date().getTime()
083: + pPeriod), "TimerEntityBean");
084: sCounter = 0;
085: } else {
086: throw new EJBException("Timer is already set");
087: }
088: }
089:
090: /**
091: * Start a timer (if not already set) with the start date plus the period
092: * and an interval of the given period
093: *
094: * @param pPeriod Time that will elapse between two events in milliseconds
095: *
096: * @ejb:interface-method view-type="remote"
097: **/
098: public void startTimer(long pPeriod) {
099: mLog
100: .info("TimerEntityBean.startTimer(), try to get a Timer Service from the Entity Context");
101: TimerService lService = mContext.getTimerService();
102: mLog
103: .info("TimerEntityBean.startTimer(), create a timer if not already done");
104: if (sTimer == null) {
105: sTimer = lService.createTimer(new Date(new Date().getTime()
106: + pPeriod), pPeriod, "TimerEntityBean");
107: sCounter = 0;
108: } else {
109: throw new EJBException("Timer is already set");
110: }
111: }
112:
113: /**
114: * @ejb:interface-method view-type="remote"
115: **/
116: public void stopTimer() {
117: try {
118: if (sTimer != null) {
119: sTimer.cancel();
120: } else {
121: throw new EJBException("Timer is not available");
122: }
123: } finally {
124: sTimer = null;
125: }
126: }
127:
128: /**
129: * @ejb:interface-method view-type="remote"
130: **/
131: public int getTimeoutCount() {
132: mLog.info("TimerEntityBean.getTimeoutCount(): " + sCounter);
133: return sCounter;
134: }
135:
136: /**
137: * @return Date of the next timed event
138: *
139: * @ejb:interface-method view-type="remote"
140: **/
141: public Date getNextTimeout() {
142: if (sTimer != null) {
143: return sTimer.getNextTimeout();
144: } else {
145: return null;
146: }
147: }
148:
149: /**
150: * @return Time remaining until next timed event in milliseconds
151: *
152: * @ejb:interface-method view-type="remote"
153: **/
154: public long getTimeRemaining() {
155: if (sTimer != null) {
156: return sTimer.getTimeRemaining();
157: } else {
158: return -1L;
159: }
160: }
161:
162: /**
163: * @return User object of the timer
164: *
165: * @ejb:interface-method view-type="remote"
166: **/
167: public Object getInfo() {
168: if (sTimer != null) {
169: return (Object) sTimer.getInfo();
170: } else {
171: return null;
172: }
173: }
174:
175: /**
176: * @return Date of the next timed event
177: *
178: * @ejb:interface-method view-type="remote"
179: **/
180: public TimerHandle getTimerHandle() {
181: if (sTimer != null) {
182: return sTimer.getHandle();
183: } else {
184: return null;
185: }
186: }
187:
188: public void ejbTimeout(Timer pTimer) {
189: mLog.debug("ejbTimeout(), timer: " + pTimer);
190: sCounter++;
191: }
192:
193: /**
194: * Describes the instance and its content for debugging purpose
195: *
196: * @return Debugging information about the instance and its content
197: **/
198: public String toString() {
199: return "TimerEntityBean [ " + " ]";
200: }
201:
202: // -------------------------------------------------------------------------
203: // Framework Callbacks
204: // -------------------------------------------------------------------------
205:
206: /**
207: * @ejb:create-method view-type="both"
208: **/
209: public Integer ejbCreate(Integer pk) {
210: mLog.info("ejbCreate(" + pk + ")");
211: return pk;
212: }
213:
214: public void ejbPostCreate(Integer pk) {
215: mLog.info("ejbPostCreate(" + pk + ")");
216: }
217:
218: /**
219: * @ejb:finder view-type="both"
220: **/
221: public Integer ejbFindByPrimaryKey(Integer pk) {
222: mLog.info("ejbFindByPrimaryKey(" + pk + ")");
223: return pk;
224: }
225:
226: public void ejbActivate() {
227: mLog.info("ejbActivate");
228: }
229:
230: public void ejbLoad() {
231: mLog.info("ejbLoad");
232: }
233:
234: public void ejbPassivate() {
235: mLog.info("ejbPassivate");
236: }
237:
238: public void ejbRemove() {
239: mLog.info("ejbRemove");
240: }
241:
242: public void ejbStore() {
243: mLog.info("ejbStore");
244: }
245:
246: public void setEntityContext(EntityContext ctx) {
247: mLog.info("setEntityContext");
248: this .mContext = ctx;
249: }
250:
251: public void unsetEntityContext() {
252: mLog.info("unsetEntityContext");
253: }
254: }
|