EJB之Timer
Posted bjfarmer
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EJB之Timer相关的知识,希望对你有一定的参考价值。
EJB Timer
- 要么: Annotation @Schedule 或者方法前声明@Timeout
- 要么: 在部署描述中定义timeout-method
- 如果是使用@Schedule, Timer在一个ejb中可以支持多个,如:
@Schedule(second="*/2", minute="*",hour="*", persistent=false,info="timer1")
public void doWork(){
System.out.println( "Hi from the EJB timer example!" );
}
@Schedule(second="*/1", minute="*",hour="*", persistent=false,info="timer2")
public void doWork2(){
System.out.println( "Hi .... 2nd timer!" );
}
以上分别启动了2个timer,一个每隔2秒执行一次,一个每隔1秒执行一次. 执行时container做了并发访问控制.也就是说callback方法一个时刻只能执行一个方法.
- 如果是使用@Timeout, 也就是Programmatic Timers, callback方法有且只有一个.
All timers created via one of the TimerService timer creation methods for a particular component use a single timeout callback method. --From ejb spec 3.1 chapter 18
如果前一个调用没有完成,而后面的timer过来了,则后面的timer会等待(也就是在等锁). 如下:
09:20:59,045 ERROR [org.jboss.as.ejb3] (EJB default - 2) JBAS014122: Error during retrying timeout for timer: [id=c2a31520-2c55-4b01-b362-d8dfdb73a2d0 timedObjectId=jboss-ejb-timer.jboss-ejb-timer.TimeoutExample auto-timer?:false persistent?:false [email protected]86ae8 initialExpiration=Tue Mar 29 09:20:48 CST 2016 intervalDuration(in milli sec)=0 nextExpiration=null timerState=RETRY_TIMEOUT: javax.ejb.ConcurrentAccessTimeoutException: JBAS014373: EJB 3.1 PFD2 4.8.5.5.1 concurrent access timeout on [email protected] - could not obtain lock within 5000MILLISECONDS
at org.jboss.as.ejb3.concurrency.ContainerManagedConcurrencyInterceptor.processInvocation(ContainerManagedConcurrencyInterceptor.java:100) [jboss-as-ejb3-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
后面的timer等待5秒后, 还会retry一次(JBoss的实现). 如果还是timeout,则会放弃.
TimerService的createSingleActionTimer()和createIntervalTimer()
- createSingleActionTimer
只触发一次,如果触发时等不到锁,timeout, 则如上所述,再试一次,如果还不行,直接放弃。
- createIntervalTimer
周期性触发,到点触发发现上一个还没搞完,则直接skip,放弃了,接着等下一个,如下code
@Timeout
public void scheduler(Timer timer) {
System.out.println("EJB Timer: Info=" + timer.getInfo() + " " + Thread.currentThread().getName());
//added by xilun
try {
Thread.sleep(1000*30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//
@PostConstruct
public void initialize( InvocationContext ctx ) {
//...
timerService.createIntervalTimer(2000, 10*1000, new TimerConfig("Single Timer #2", false));
//....
}
输出的log如下:
10:21:47,864 INFO [stdout] (EJB default - 7) EJB Timer: Info=Single Timer #2 EJB default - 7
10:21:57,864 WARN [org.jboss.as.ejb3] (EJB default - 9) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:21:57 CST 2016
10:22:07,864 WARN [org.jboss.as.ejb3] (EJB default - 6) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:22:07 CST 2016
10:22:17,864 WARN [org.jboss.as.ejb3] (EJB default - 8) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:22:17 CST 2016
10:22:27,865 INFO [stdout] (EJB default - 3) EJB Timer: Info=Single Timer #2 EJB default - 3
10:22:37,864 WARN [org.jboss.as.ejb3] (EJB default - 10) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:22:37 CST 2016
10:22:47,864 WARN [org.jboss.as.ejb3] (EJB default - 2) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:22:47 CST 2016
10:22:57,864 WARN [org.jboss.as.ejb3] (EJB default - 1) JBAS014143: A previous execution of timer [jboss-ejb-timer.jboss-ejb-timer.TimeoutExample 8ba38f5a-0da7-4993-8b0b-e69854f61ee4] is still in progress, skipping this overlapping scheduled execution at: Tue Mar 29 10:22:57 CST 2016
Jboss quickstart中有timer的例子。
以上是关于EJB之Timer的主要内容,如果未能解决你的问题,请参考以下文章