KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfox > ejb > timer > EJBTimerTask


1 package org.jfox.ejb.timer;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.TimerTask JavaDoc;
5 import javax.ejb.EJBException JavaDoc;
6 import javax.ejb.TimedObject JavaDoc;
7 import javax.ejb.Timer JavaDoc;
8
9 import org.jfox.ejb.Bucket;
10 import org.jfox.ejb.EJBObjectId;
11 import org.jfox.ejb.connector.EJBContainerImpl;
12 import org.jfox.ejb.connector.EJBInvocation;
13 import org.jfox.ioc.ComponentName;
14 import org.jfox.ioc.Registry;
15 import org.jfox.ioc.connector.ConnectorRemote;
16 import org.jfox.ioc.connector.Container;
17 import org.jfox.ioc.connector.local.LOCALConnectorRemote;
18 import org.jfox.ioc.util.Methods;
19
20 /**
21  * @author <a HREF="mailto:yy.young@gmail.com">Young Yang</a>
22  */

23
24 public class EJBTimerTask extends TimerTask JavaDoc {
25
26     private EJBTimerService timerService;
27     private EJBTimer timer;
28
29
30     public EJBTimerTask(EJBTimerService timerService, Serializable JavaDoc info) {
31         this.timerService = timerService;
32         timer = new EJBTimer(this, info);
33     }
34
35     public Timer JavaDoc getTimer() {
36         return timer;
37     }
38
39     public void run() {
40         String JavaDoc ejbName = timerService.getEjbName();
41
42         try {
43             Bucket bucket = ((EJBContainerImpl) Registry.getInstance().getComponentInstance(ComponentName.parseString(Container.class.getName() + "@EJB"))).getBucket(ejbName);
44
45             if(!TimedObject JavaDoc.class.isAssignableFrom(bucket.getBeanClass())) {
46                 throw new EJBException JavaDoc("ejb " + ejbName + " not implements " + TimedObject JavaDoc.class.getName());
47             }
48             // bucause Timer used by StatelessBean, so objectId can be random
49
EJBObjectId objectId = bucket.nextObjectId();
50             EJBInvocation invocation = new EJBInvocation(objectId, "" + Methods.getMethodHash(TimedObjectMethod.TimeOut), new Object JavaDoc[]{timer});
51
52             // use LOCALContainerService invoke the timout
53
//TODO: Ƿ滻ȷ
54
// ContainerRemote local = (ContainerRemote) (Registry.getInstance().getFirstComponentMetabyImplemetation(LOCALContainerService.class).getComponentInstance());
55
ConnectorRemote local = LOCALConnectorRemote.getInstance();
56             local.invoke(invocation);
57         }
58         catch(Exception JavaDoc e) {
59             e.printStackTrace();
60         }
61     }
62
63     public boolean cancel() {
64         timerService.cancel(this);
65         return super.cancel();
66     }
67
68     EJBTimerService getTimerService() {
69         return timerService;
70     }
71
72     public static void main(String JavaDoc[] args) {
73
74     }
75 }
76
77
Popular Tags