Skip to content

Commit

Permalink
JBAS-8232 Fixed timer cancellation - The Future wasn't being made ava…
Browse files Browse the repository at this point in the history
…ilable to the timer when the timer was created out of a persisted timer
  • Loading branch information
jaikiran committed Jul 22, 2010
1 parent 095be37 commit 1f9bafe
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 60 deletions.
@@ -0,0 +1,39 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ejb3.timerservice.integration.test.cancel;

/**
* SimpleTimer
*
* @author Jaikiran Pai
* @version $Revision: $
*/
public interface SimpleTimer
{
void createTimer(long intialDuration, long intervalDurationMillis);

void stopTimers();

boolean timersCreated();

int getTimeoutCount();
}
@@ -0,0 +1,109 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ejb3.timerservice.integration.test.cancel;

import java.util.Collection;

import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;

import org.jboss.ejb3.annotation.RemoteBinding;
import org.jboss.logging.Logger;

/**
* SimpleTimerSLSB
*
* @author Jaikiran Pai
* @version $Revision: $
*/
@Stateless
@Remote(SimpleTimer.class)
@RemoteBinding(jndiBinding = SimpleTimerSLSB.JNDI_NAME)
public class SimpleTimerSLSB implements SimpleTimer
{

private static Logger logger = Logger.getLogger(SimpleTimerSLSB.class);

public static final String JNDI_NAME = "CancelTimerTestCaseBean";

@Resource
private TimerService timerService;

@EJB
private TimeoutTracker timeoutTracker;

@Override
public void createTimer(long intialDuration, long intervalDurationMillis)
{
// cancel existing timers
cancelTimers();
logger.info("Creating timer starting at " + intialDuration
+ " milli. sec from now and with a recurring duration of " + intervalDurationMillis + " milli. sec");
timerService.createTimer(intialDuration, intervalDurationMillis, "Test Timer");

}

@Override
public void stopTimers()
{
cancelTimers();
}

@Override
public boolean timersCreated()
{
Collection<Timer> timers = timerService.getTimers();
logger.info("Number of timers for bean: " + this.getClass().getSimpleName() + " = " + timers.size());
return (timers.size() > 0);
}

@Override
public int getTimeoutCount()
{
return this.timeoutTracker.getTimeoutCount();
}

private void cancelTimers()
{
logger.info("Canceling all existing timers: ");
Collection<Timer> timers = timerService.getTimers();
for (Timer timer : timers)
{
logger.info("Canceling timer: " + timer);
timer.cancel();

}
}

@Timeout
public void handleTimeout(Timer timer)
{
logger.info("Timeout called on bean: " + this.getClass().getSimpleName() + " for timer " + timer);
this.timeoutTracker.trackTimeout(timer);
}

}
@@ -0,0 +1,48 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ejb3.timerservice.integration.test.cancel;

import javax.ejb.Singleton;
import javax.ejb.Timer;

/**
* TimeoutTracker
*
* @author Jaikiran Pai
* @version $Revision: $
*/
@Singleton
public class TimeoutTracker
{

private int count;

public void trackTimeout(Timer timer)
{
this.count++;
}

public int getTimeoutCount()
{
return this.count;
}
}
@@ -0,0 +1,126 @@
/*
* JBoss, Home of Professional Open Source.
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ejb3.timerservice.integration.test.cancel.unit;

import java.io.File;
import java.net.URL;
import java.util.Date;

import junit.framework.Assert;

import org.jboss.ejb3.timerservice.integration.test.cancel.SimpleTimer;
import org.jboss.ejb3.timerservice.integration.test.cancel.SimpleTimerSLSB;
import org.jboss.ejb3.timerservice.integration.test.common.AbstractTimerServiceTestCase;
import org.jboss.logging.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
* Tests that timers which have been cancelled will no longer fire timeouts
*
* @see https://jira.jboss.org/browse/JBAS-8232
*
* @author Jaikiran Pai
* @version $Revision: $
*/
public class CancelTimerTestCase extends AbstractTimerServiceTestCase
{
private static Logger logger = Logger.getLogger(CancelTimerTestCase.class);

private URL deployment;

/**
*
* @return
* @throws Exception
*/
@Before
public void before() throws Exception
{
String jarName = "cancel-timer-test.jar";
File jar = buildSimpleJar(jarName, SimpleTimerSLSB.class.getPackage());
this.deployment = jar.toURI().toURL();
this.redeploy(deployment);
}

@After
public void after() throws Exception
{
if (this.deployment != null)
{
this.undeploy(deployment);
}
}

/**
* Tests that a non-calendar expression based timer, when cancelled, will no longer
* fire timeouts
* @see JBAS-8232 https://jira.jboss.org/browse/JBAS-8232
* @throws Exception
*/
@Test
public void testCancelOfSimpleTimer() throws Exception
{
SimpleTimer bean = (SimpleTimer) this.getInitialContext().lookup(SimpleTimerSLSB.JNDI_NAME);

long twoSeconds = 2000;
long everySecond = 1000;

bean.createTimer(twoSeconds, everySecond);
logger.debug("Created timer to fire every second starting at " + new Date(System.currentTimeMillis() + twoSeconds));

// check that the timers were created
Assert.assertTrue("No timers were created for bean " + SimpleTimerSLSB.class.getSimpleName(), bean
.timersCreated());

final long THREE_SECONDS = 3000;

// now wait for atleast 1 timeout to happen
logger.info("Sleeping for 3 seconds for timeout to happen");
Thread.sleep(THREE_SECONDS);

// check that atleast one timeout occured
int timeoutCount = bean.getTimeoutCount();
Assert.assertTrue("Not even 1 timeout occured", timeoutCount > 0);

// now cancel the timer
bean.stopTimers();

int timeoutCountImmidiatelyAfterCancel = bean.getTimeoutCount();

// check that there are no more active timers for the bean
Assert.assertFalse("Active timers found, even after cancelling the timers, on bean "
+ SimpleTimerSLSB.class.getSimpleName(), bean.timersCreated());

// wait for a few more seconds and check whether the timer was indeed cancelled
// or whether it is still firing timeouts
logger
.info("Sleeping for 3 more seconds after cancelling the timer, to make sure the timers were really cancelled");
Thread.sleep(THREE_SECONDS);

int finalTimeoutCount = bean.getTimeoutCount();
// make sure that the timeout count immidiately after cancellation of timers is the same as the latest timeout count
Assert.assertEquals("Timers wasn't really cancelled. Timeouts are still happening",
timeoutCountImmidiatelyAfterCancel, finalTimeoutCount);
}
}
4 changes: 3 additions & 1 deletion testsuite/src/test/resources/test.xml
Expand Up @@ -254,7 +254,9 @@
<sysproperty key="log4j.configuration" value="file:${location.resources.test}/log4j.xml"/>

<jvmarg line="${jvmargs}" />
<!-- <jvmarg line="${jvmargs} -Xrunjdwp:transport=dt_socket,address=8788,server=y,suspend=y" /> -->
<!--
<jvmarg line="-Xrunjdwp:transport=dt_socket,address=5005,server=y,suspend=y"/>
-->
<classpath>

<path refid="test.classpath" />
Expand Down

0 comments on commit 1f9bafe

Please sign in to comment.