KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > ejb3 > test > timer > BaseTimerTesterBean


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.ejb3.test.timer;
23
24 import java.util.Date JavaDoc;
25
26 import javax.annotation.Resource;
27 import javax.ejb.SessionContext JavaDoc;
28 import javax.ejb.Timer JavaDoc;
29 import javax.ejb.TimerService JavaDoc;
30 import javax.ejb.TransactionAttribute JavaDoc;
31 import javax.ejb.TransactionAttributeType JavaDoc;
32
33 import org.jboss.logging.Logger;
34
35 /**
36  * This class forms the base of both an EJB3 timer tester bean and
37  * an EJB 2.1 timer tester bean.
38  *
39  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
40  * @author <a HREF="mailto:carlo@nerdnet.nl">Carlo de Wolf</a>
41  * @version $Revision: 56264 $
42  */

43 public abstract class BaseTimerTesterBean
44 {
45    @SuppressWarnings JavaDoc("unused")
46    private static final Logger log = Logger.getLogger(BaseTimerTesterBean.class);
47    
48    protected static boolean timerCalled = false;
49
50    private @Resource TimerService JavaDoc timerService;
51
52    protected @Resource SessionContext JavaDoc ctx;
53    
54    // TODO: fix this state in the stateless bean
55
private static Timer JavaDoc timer;
56
57    @TransactionAttribute JavaDoc(TransactionAttributeType.MANDATORY)
58    public void checkMandatoryTransaction()
59    {
60       
61    }
62    
63    private void reset()
64    {
65       timerCalled = false;
66       timer = null;
67    }
68    
69    public void setTimer(Date JavaDoc expiration)
70    {
71       reset();
72       System.out.println("************ set timer " + expiration);
73       timer = timerService.createTimer(expiration, "TimerSLSBean");
74    }
75    
76    public void startTimer(long pPeriod)
77    {
78       reset();
79       System.out.println("************ startTimer");
80       timer = timerService.createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + pPeriod), "TimerSLSBean");
81    }
82
83    public void startTimerAndRollback(long pPeriod)
84    {
85       reset();
86       System.out.println("************ startTimerAndRollback");
87       timer = ctx.getTimerService().createTimer(pPeriod, "TimerSLSBean");
88       ctx.setRollbackOnly();
89    }
90
91    public void startTimerViaEJBContext(long pPeriod)
92    {
93       reset();
94       System.out.println("************ startTimerViaEJBContext");
95       timer = ctx.getTimerService().createTimer(new Date JavaDoc(new Date JavaDoc().getTime() + pPeriod), "TimerSLSBean");
96    }
97
98    public void accessTimer()
99    {
100       //Access timer to make sure we have pushed the AllowedOperationsAssociation
101
timer.getTimeRemaining();
102       timer.getHandle();
103       timer.getInfo();
104    }
105    
106    public boolean isTimerCalled()
107    {
108       return timerCalled;
109    }
110 }
111
Popular Tags