Skip to content

Commit

Permalink
Allow timerservice invocations in lifecycle methods of singleton bean
Browse files Browse the repository at this point in the history
  • Loading branch information
jaikiran committed Jul 7, 2010
1 parent 6fad528 commit 1b8acb2
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,79 +85,56 @@ public void testTimeout() throws Exception
Date fiveSecondsFromNow = new Date(System.currentTimeMillis() + 5000);
long everyTwoSeconds = 2000;
int fiveTimes = 5;
timerUtil.createTimer(fiveSecondsFromNow, 0, fiveTimes);
// wait for the timers to be invoked
timerUtil.createTimer(fiveSecondsFromNow, everyTwoSeconds, fiveTimes);

// wait for the timers to be invoked
Thread.sleep(5000 + 10000 + 5000);
// TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();
//
// Assert.assertNotNull("Could not get the timeout tracker" , timeoutTracker);
// Assert.assertEquals("Unexpected number of timeouts",fiveTimes, timeoutTracker.getTimeoutCount());
// List<Date> timeouts = timeoutTracker.getTimeouts();
// // 1 second grace period
// Date lastExpectedTimeout = new Date(fiveSecondsFromNow.getTime() + (everyTwoSeconds * fiveTimes) + 1000);
// for (Date timeout : timeouts)
// {
// logger.debug("Timeout was tracked at " + timeout);
// Assert.assertFalse("Timeout " + timeout + " happened before the first timeout " + fiveSecondsFromNow, timeout.before(fiveSecondsFromNow));
// Assert.assertTrue("Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout, timeout.before(lastExpectedTimeout));
// }
TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();

Assert.assertNotNull("Could not get the timeout tracker", timeoutTracker);
Assert.assertEquals("Unexpected number of timeouts", fiveTimes, timeoutTracker.getTimeoutCount());
List<Date> timeouts = timeoutTracker.getTimeouts();
// 1 second grace period
Date lastExpectedTimeout = new Date(fiveSecondsFromNow.getTime() + (everyTwoSeconds * fiveTimes) + 1000);
for (Date timeout : timeouts)
{
logger.debug("Timeout was tracked at " + timeout);
Assert.assertFalse("Timeout " + timeout + " happened before the first timeout " + fiveSecondsFromNow, timeout
.before(fiveSecondsFromNow));
Assert.assertTrue("Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout,
timeout.before(lastExpectedTimeout));
}
}

// @Test
// public void testTimerInvocationOnRedeploy() throws Exception
// {
// TimerUtil timerUtil = (TimerUtil) this.getInitialContext().lookup(TimerSingleton.JNDI_NAME);
// Date tenSecondsFromNow = new Date(System.currentTimeMillis() + 10000);
// long everySecond = 1000;
// int twoTimes = 2;
// timerUtil.createTimer(tenSecondsFromNow, everySecond, twoTimes);
//
// // redeploy the bean
// this.redeploy(this.deployment);
//
// // wait for the timers to be invoked
// Thread.sleep(10000 + 3000);
// TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();
//
// Assert.assertNotNull("Could not get the timeout tracker" , timeoutTracker);
// Assert.assertEquals("Unexpected number of timeouts",twoTimes, timeoutTracker.getTimeoutCount());
// List<Date> timeouts = timeoutTracker.getTimeouts();
// // 1 second grace period
// Date lastExpectedTimeout = new Date(tenSecondsFromNow.getTime() + (everySecond * twoTimes) + 1000);
// for (Date timeout : timeouts)
// {
// logger.debug("Timeout was tracked at " + timeout);
// Assert.assertFalse("Timeout " + timeout + " happened before the first timeout " + tenSecondsFromNow, timeout.before(tenSecondsFromNow));
// Assert.assertTrue("Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout, timeout.before(lastExpectedTimeout));
// }
//}
//
// public static void main(String args[]) throws Exception
// {
// TimerUtil timerUtil = (TimerUtil) new InitialContext().lookup(TimerSingleton.JNDI_NAME);
// Date tenSecondsFromNow = new Date(System.currentTimeMillis() + 10000);
// long everySecond = 1000;
// int twoTimes = 2;
// timerUtil.createTimer(tenSecondsFromNow, everySecond, twoTimes);
//
//// // redeploy the bean
//// this.redeploy(this.deployment);
////
//// // wait for the timers to be invoked
//// Thread.sleep(10000 + 3000);
//// TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();
////
//// Assert.assertNotNull("Could not get the timeout tracker" , timeoutTracker);
//// Assert.assertEquals("Unexpected number of timeouts",twoTimes, timeoutTracker.getTimeoutCount());
//// List<Date> timeouts = timeoutTracker.getTimeouts();
//// // 1 second grace period
//// Date lastExpectedTimeout = new Date(tenSecondsFromNow.getTime() + (everySecond * twoTimes) + 1000);
//// for (Date timeout : timeouts)
//// {
//// logger.debug("Timeout was tracked at " + timeout);
//// Assert.assertFalse("Timeout " + timeout + " happened before the first timeout " + tenSecondsFromNow, timeout.before(tenSecondsFromNow));
//// Assert.assertTrue("Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout, timeout.before(lastExpectedTimeout));
//// }
// }

@Test
public void testTimerInvocationOnRedeploy() throws Exception
{
TimerUtil timerUtil = (TimerUtil) this.getInitialContext().lookup(TimerSingleton.JNDI_NAME);
Date tenSecondsFromNow = new Date(System.currentTimeMillis() + 10000);
long everySecond = 1000;
int twoTimes = 2;
timerUtil.createTimer(tenSecondsFromNow, everySecond, twoTimes);

// redeploy the bean
this.redeploy(this.deployment);

// wait for the timers to be invoked
Thread.sleep(10000 + 3000);
TimeoutTracker timeoutTracker = timerUtil.getTimeoutTracker();

Assert.assertNotNull("Could not get the timeout tracker", timeoutTracker);
Assert.assertEquals("Unexpected number of timeouts", twoTimes, timeoutTracker.getTimeoutCount());
List<Date> timeouts = timeoutTracker.getTimeouts();
// 1 second grace period
Date lastExpectedTimeout = new Date(tenSecondsFromNow.getTime() + (everySecond * twoTimes) + 1000);
for (Date timeout : timeouts)
{
logger.debug("Timeout was tracked at " + timeout);
Assert.assertFalse("Timeout " + timeout + " happened before the first timeout " + tenSecondsFromNow, timeout
.before(tenSecondsFromNow));
Assert.assertTrue("Timeout " + timeout + " happened after the last expected timeout " + lastExpectedTimeout,
timeout.before(lastExpectedTimeout));
}
}

}
4 changes: 2 additions & 2 deletions timerservice-mk2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<url>http://labs.jboss.com/jbossejb3/</url>

<properties>
<version.org.jboss.ejb3.context>0.1.1</version.org.jboss.ejb3.context>
<version.org.jboss.ejb3.context>0.2.0-SNAPSHOT</version.org.jboss.ejb3.context>
</properties>

<build>
Expand Down Expand Up @@ -119,7 +119,7 @@
<dependency>
<groupId>org.jboss.ejb3</groupId>
<artifactId>jboss-ejb3-timerservice-spi</artifactId>
<version>1.0.2</version>
<version>1.0.3-SNAPSHOT</version>
<exclusions>
<!-- Make sure the EJB 3.0 API doesn't get in here -->
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import org.jboss.ejb3.timerservice.mk2.persistence.TimeoutMethod;
import org.jboss.ejb3.timerservice.mk2.persistence.TimerEntity;
import org.jboss.ejb3.timerservice.spi.TimedObjectInvoker;
import org.jboss.ejb3.timerservice.spi.TimerServiceInvocationContext;
import org.jboss.logging.Logger;

/**
Expand Down Expand Up @@ -321,10 +322,10 @@ public org.jboss.ejb3.timerservice.extension.Timer getAutoTimer(ScheduleExpressi
@Override
public Collection<Timer> getTimers() throws IllegalStateException, EJBException
{
if (this.isLifecycleCallbackInvocation())
if (this.isLifecycleCallbackInvocation() && this.isSingletonBeanInvocation() == false)
{
throw new IllegalStateException(
"getTimers() method invocation is not allowed during lifecycle callback of EJBs");
"getTimers() method invocation is not allowed during lifecycle callback of non-singleton EJBs");
}

Set<Timer> activeTimers = new HashSet<Timer>();
Expand Down Expand Up @@ -367,9 +368,9 @@ public Collection<Timer> getTimers() throws IllegalStateException, EJBException
*/
private Timer createTimer(Date initialExpiration, long intervalDuration, Serializable info, boolean persistent)
{
if (this.isLifecycleCallbackInvocation())
if (this.isLifecycleCallbackInvocation() && this.isSingletonBeanInvocation() == false)
{
throw new IllegalStateException("Creation of timers is not allowed during lifecycle callback of EJBs");
throw new IllegalStateException("Creation of timers is not allowed during lifecycle callback of non-singleton EJBs");
}
if (initialExpiration == null)
{
Expand Down Expand Up @@ -412,9 +413,9 @@ private Timer createTimer(Date initialExpiration, long intervalDuration, Seriali
private org.jboss.ejb3.timerservice.extension.Timer createCalendarTimer(ScheduleExpression schedule,
Serializable info, boolean persistent, Method timeoutMethod)
{
if (this.isLifecycleCallbackInvocation())
if (this.isLifecycleCallbackInvocation() && this.isSingletonBeanInvocation() == false)
{
throw new IllegalStateException("Creation of timers is not allowed during lifecycle callback of EJBs");
throw new IllegalStateException("Creation of timers is not allowed during lifecycle callback of non-singleton EJBs");
}
if (schedule == null)
{
Expand Down Expand Up @@ -852,6 +853,27 @@ protected boolean isLifecycleCallbackInvocation()
// not an lifecycle callback
return false;
}

private boolean isSingletonBeanInvocation()
{
InvocationContext currentInvocationContext = null;
try
{
currentInvocationContext = CurrentInvocationContext.get();

if (currentInvocationContext instanceof TimerServiceInvocationContext)
{
TimerServiceInvocationContext timerserviceInvocationCtx = (TimerServiceInvocationContext) currentInvocationContext;
return timerserviceInvocationCtx.isSingleton();
}
return false;
}
catch (IllegalStateException ise)
{
// no context info available so return false
return false;
}
}

private TimerImpl getPersistedTimer(TimerHandleImpl timerHandle)
{
Expand Down

0 comments on commit 1b8acb2

Please sign in to comment.