Timer event is always handled by a new instance?
granby Sep 8, 2005 3:53 PMI modified the timer example so that the timer is restarted. I also kept track of instances of the statless bean:
@Stateless
@Remote(ExampleTimer.class)
public class ExampleTimerBean implements ExampleTimer
{
private static int instanceCount=0;
private @Resource SessionContext ctx;
public void scheduleTimer(long milliseconds)
{
ctx.getTimerService().createTimer(new Date(new Date().getTime() + milliseconds), "Hello World");
}
@Timeout
public void timeoutHandler(Timer timer)
{
timer.cancel();
ctx.getTimerService().createTimer(new Date(new Date().getTime() + 50), "Hello World");
}
@PostConstruct
public void postConstruct(){
instanceCount++;
System.out.println("New instance: "+instanceCount);
}
@PreDestroy
public void preDestroy(){
instanceCount++;
System.out.println("Instance removed: "+instanceCount);
}
}
I removed some lines of the output but as you can see each timeout seems to be handled by a new statless instance. How come that they never get reused?
I guess I pushed it a bit with the low timeout value but I was expecting another type of error not duplicate key?
21:37:35,218 INFO [STDOUT] New instance: 1406
21:37:35,234 INFO [STDOUT] New instance: 1407
21:37:35,281 INFO [STDOUT] New instance: 1408
21:37:35,296 INFO [STDOUT] New instance: 1409
21:37:35,343 INFO [STDOUT] New instance: 1410
21:37:35,359 INFO [STDOUT] New instance: 1411
21:37:35,406 INFO [STDOUT] New instance: 1412
21:37:35,421 INFO [STDOUT] New instance: 1413
21:37:35,468 INFO [STDOUT] New instance: 1414
21:37:35,468 ERROR [TimerServiceImpl] Cannot create txtimer
java.lang.IllegalStateException: Unable to persist timer
at org.jboss.ejb.txtimer.DatabasePersistencePolicy.insertTimer(DatabaseP
ersistencePolicy.java:138)
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:245)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
at org.jboss.mx.util.JMXInvocationHandler.invoke(JMXInvocationHandler.ja
va:273)
at $Proxy45.insertTimer(Unknown Source)
at org.jboss.ejb.txtimer.TimerServiceImpl.createTimer(TimerServiceImpl.j
ava:212)
at org.jboss.ejb.txtimer.TimerServiceImpl.createTimer(TimerServiceImpl.j
ava:178)
at org.jboss.tutorial.timer.bean.ExampleTimerBean.timeoutHandler(Example
TimerBean.java:41)
at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:99)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsI
nterceptor.java:32)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java
:134)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInt
erceptor.java:61)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(Stateles
sInstanceInterceptor.java:39)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(Authentic
ationInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(Asynchrono
usInterceptor.java:91)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.stateless.StatelessContainer.callTimeout(StatelessCont
ainer.java:96)
at org.jboss.ejb.txtimer.TimerImpl$TimerTaskImpl.run(TimerImpl.java:473)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Caused by: java.sql.SQLException: Unique constraint violation: in statement [in
sert into TIMERS (TIMERID,TARGETID,INITIALDATE,TIMERINTERVAL,INSTANCEPK,INFO) va
lues (?,?,?,?,?,?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdat
e(WrappedPreparedStatement.java:227)
at org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin.insertT
imer(GeneralPurposeDatabasePersistencePlugin.java:156)
at org.jboss.ejb.txtimer.DatabasePersistencePolicy.insertTimer(DatabaseP
ersistencePolicy.java:134)
... 34 more
21:37:35,484 INFO [STDOUT] New instance: 1415
21:37:35,484 ERROR [TimerServiceImpl] Cannot create txtimer
java.lang.IllegalStateException: Unable to persist timer
at org.jboss.ejb.txtimer.DatabasePersistencePolicy.insertTimer(DatabaseP
ersistencePolicy.java:138)
at sun.reflect.GeneratedMethodAccessor58.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:141)
at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:245)
at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
at org.jboss.mx.util.JMXInvocationHandler.invoke(JMXInvocationHandler.ja
va:273)
at $Proxy45.insertTimer(Unknown Source)
at org.jboss.ejb.txtimer.TimerServiceImpl.createTimer(TimerServiceImpl.j
ava:212)
at org.jboss.ejb.txtimer.TimerServiceImpl.createTimer(TimerServiceImpl.j
ava:178)
at org.jboss.tutorial.timer.bean.ExampleTimerBean.timeoutHandler(Example
TimerBean.java:41)
at sun.reflect.GeneratedMethodAccessor68.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:99)
at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsI
nterceptor.java:32)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:66)
at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java
:134)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInt
erceptor.java:61)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(Stateles
sInstanceInterceptor.java:39)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.aspects.security.AuthenticationInterceptor.invoke(Authentic
ationInterceptor.java:63)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.asynchronous.AsynchronousInterceptor.invoke(Asynchrono
usInterceptor.java:91)
at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.
java:88)
at org.jboss.ejb3.stateless.StatelessContainer.callTimeout(StatelessCont
ainer.java:96)
at org.jboss.ejb.txtimer.TimerImpl$TimerTaskImpl.run(TimerImpl.java:473)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
Caused by: java.sql.SQLException: Unique constraint violation: in statement [in
sert into TIMERS (TIMERID,TARGETID,INITIALDATE,TIMERINTERVAL,INSTANCEPK,INFO) va
lues (?,?,?,?,?,?)]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.executeUpdate(Unknown Source)
at org.jboss.resource.adapter.jdbc.WrappedPreparedStatement.executeUpdat
e(WrappedPreparedStatement.java:227)
at org.jboss.ejb.txtimer.GeneralPurposeDatabasePersistencePlugin.insertT
imer(GeneralPurposeDatabasePersistencePlugin.java:156)
at org.jboss.ejb.txtimer.DatabasePersistencePolicy.insertTimer(DatabaseP
ersistencePolicy.java:134)
... 34 more