Create TimerServer in Stateless Session
package com.xxx.test;
import javax.ejb.Local;
@Local
public interface TimerTestBeanLocal {
public void startTimer(long initialDurationMillis,long intervalDurationMillis);
public void stopTimer();
public boolean timerStarted();
}
package com.xxx.test;
import javax.ejb.Remote;
@Remote
public interface TimerTestBeanRemote {
public void startTimer(long initialDurationMillis,long intervalDurationMillis);
public void stopTimer();
public boolean timerStarted();
}
package com.xxx.test;
import java.util.Collection;
import javax.annotation.Resource;
import javax.ejb.EJBException;
import javax.ejb.Stateless;
import javax.ejb.Timeout;
import javax.ejb.Timer;
import javax.ejb.TimerService;
import org.apache.log4j.Logger;
@Stateless
public class TimerTestBean implements TimerTestBeanRemote, TimerTestBeanLocal {
private final Logger logger = Logger.getLogger(this.getClass());
@Resource
private TimerService timerService;
@Override
public void startTimer( long initialDurationMillis,long intervalDurationMillis)
{
cancelTimers();
createTimers(initialDurationMillis,intervalDurationMillis);
}
@Override
public void stopTimer()
{
cancelTimers();
}
@Override
public boolean timerStarted()
{
logger.debug("Checking if Test timer is started");
Collection<Timer> timers = timerService.getTimers();
return (timers.size() > 0);
}
private void createTimers(long initialDurationMillis,long intervalDurationMillis)
{
logger.info("TestTimerBean: Timer started: First expire after " + initialDurationMillis + " milliseconds.");
logger.info("TestTimerBean: Timer started: Repeat after " + intervalDurationMillis + " milliseconds.");
timerService.createTimer(initialDurationMillis, intervalDurationMillis, "Test Timer");
}
private void cancelTimers()
{
logger.info("[A2] Canceling all existing timers: ");
Collection<Timer> timers = timerService.getTimers();
for (Timer timer : timers)
{
logger.info("[A2] \tCanceling timer: " + timer.getInfo().toString());
try
{
// Timer still runs, it does not stop process
// This worked in Jboss 6.0.0 - M3.....
timer.cancel();
}
catch (Exception ex)
{
logger.info(ex.toString());
}
}
}
@Timeout
public void handleTimeout(Timer timer)
{
logger.info("[A2] TestTimerBean: handleTimeout() called");
}
}
// groovy script Application to start/stop timer
package javaapplication1
import javax.naming.*
import java.util.Collection
import java.util.Properties
import javax.rmi.PortableRemoteObject
import com.itt.test.TimerTestBeanRemote
Properties properties = new Properties()
properties.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory")
properties.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces")
properties.put("java.naming.provider.url", "X.X.X.X:1099")
try
{
println("[ProducerS] get initial context :: ")
InitialContext jndiContext = new InitialContext(properties)
def remoteObj = jndiContext.lookup("TimerTestBean/remote")
println remoteObj
println remoteObj.class
TimerTestBeanRemote tb = (TimerTestBeanRemote) PortableRemoteObject.narrow(remoteObj, TimerTestBeanRemote.class)
tb.stopTimer()
// tb.startTimer(1000,5000)
}
catch(Exception ex) {
}