001: /*
002: * JFox - The most lightweight Java EE Application Server!
003: * more details please visit http://www.huihoo.org/jfox or http://www.jfox.org.cn.
004: *
005: * JFox is licenced and re-distributable under GNU LGPL.
006: */
007: package org.jfox.ejb3.timer;
008:
009: import java.io.Serializable;
010: import java.lang.reflect.Method;
011: import java.util.ArrayList;
012: import java.util.Arrays;
013: import java.util.Date;
014: import java.util.List;
015: import java.util.concurrent.ScheduledFuture;
016: import java.util.concurrent.TimeUnit;
017: import javax.ejb.EJBException;
018: import javax.ejb.NoSuchObjectLocalException;
019: import javax.ejb.Timer;
020: import javax.ejb.TimerHandle;
021:
022: import org.jfox.ejb3.EJBObjectId;
023: import org.jfox.ejb3.SimpleEJB3Container;
024: import org.jfox.mvc.SessionContext;
025:
026: /**
027: * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
028: */
029:
030: public class EJBTimerTask implements Timer, TimerHandle, Runnable {
031:
032: private EJBObjectId ejbObjectId;
033:
034: private final List<Method> timeoutMethods = new ArrayList<Method>();
035:
036: private Serializable info;
037:
038: private SimpleEJB3Container.ContainerTimerService timerService;
039:
040: /**
041: * Scheduled future
042: */
043: private ScheduledFuture future;
044:
045: private SessionContext sessionContext;
046:
047: public EJBTimerTask(
048: SimpleEJB3Container.ContainerTimerService timerService,
049: Serializable info) {
050: this .timerService = timerService;
051: this .info = info;
052: }
053:
054: public EJBObjectId getEJBObjectId() {
055: return ejbObjectId;
056: }
057:
058: public void setEJBObjectId(EJBObjectId ejbObjectId) {
059: this .ejbObjectId = ejbObjectId;
060: }
061:
062: public SessionContext getSessionContext() {
063: return sessionContext;
064: }
065:
066: // set security context when createTimer
067: public void setSessionContext(SessionContext sessionContext) {
068: this .sessionContext = sessionContext;
069: }
070:
071: public void setFuture(ScheduledFuture future) {
072: this .future = future;
073: }
074:
075: public long getTimeRemaining() throws IllegalStateException,
076: NoSuchObjectLocalException, EJBException {
077: return future.getDelay(TimeUnit.MILLISECONDS);
078: }
079:
080: public Date getNextTimeout() throws IllegalStateException,
081: NoSuchObjectLocalException, EJBException {
082: return new Date(System.currentTimeMillis()
083: + future.getDelay(TimeUnit.MILLISECONDS));
084: }
085:
086: public Serializable getInfo() throws IllegalStateException,
087: NoSuchObjectLocalException, EJBException {
088: return info;
089: }
090:
091: public TimerHandle getHandle() throws IllegalStateException,
092: NoSuchObjectLocalException, EJBException {
093: return this ;
094: }
095:
096: public Timer getTimer() throws IllegalStateException,
097: NoSuchObjectLocalException, EJBException {
098: return this ;
099: }
100:
101: /**
102: * need remove from EJBTimerService's timers
103: */
104: public void cancel() {
105: future.cancel(false);
106: }
107:
108: public void addTimeoutMethod(Method[] timeoutMethods) {
109: this .timeoutMethods.addAll(Arrays.asList(timeoutMethods));
110: }
111:
112: public Method[] getTimeoutMethods() {
113: return timeoutMethods
114: .toArray(new Method[timeoutMethods.size()]);
115: }
116:
117: public void run() {
118: // 执行 @Timeout 方法
119: timerService.timeout(this );
120: }
121:
122: public static void main(String[] args) {
123:
124: }
125: }
|