我想打印字符串" qui"每5秒钟。 我创建了一个EJB单例,并通过注释定义了一个超时方法。我期待着字符串" qui"每5秒打印一次,但这不会发生。字符串" qui"是连续印刷的。我的应用程序服务器是Glassfish。 下面是我的代码:
import EJBData.AuctionFrontEndLocal;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
/**
*
* @author melix
*/
@Startup
@Singleton
public class Timer{
@EJB
private AuctionFrontEndLocal auctionFrontEnd;
private Timer timer;
@Resource TimerService tservice;
@PostConstruct
public void initTimer(){
tservice.createIntervalTimer(0,5000,new TimerConfig());
}
@Timeout
public void timeout() {
System.out.println("QUI!");
}
}
答案 0 :(得分:3)
尝试一下更简单。
import javax.ejb.Schedule;
import javax.ejb.Singleton;
import org.slf4j.LoggerFactory;
@Singleton
public class Timer {
public Timer () {
LoggerFactory.getLogger(this.getClass()).info("Starting timer");
}
@Schedule (persistent = false, second = "*", minute = "*", hour = "*")
private void second () {
LoggerFactory.getLogger(this.getClass()).info("Second");
}
@Schedule (persistent = false, second = "*/2", minute = "*", hour = "*", info = "Second Second")
private void secondSecond () {
LoggerFactory.getLogger(this.getClass()).info("Second Second");
}
@Schedule (persistent = false, second = "*/3", minute = "*", hour = "*")
private void thirdSecond () {
LoggerFactory.getLogger(this.getClass()).info("Third Second");
}
}
答案 1 :(得分:0)
您的代码似乎没问题......这是我的工作示例:
package com.mycompany.mavenproject2;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.ejb.Timeout;
import javax.ejb.TimerConfig;
import javax.ejb.TimerService;
@Startup
@Singleton
public class NewSessionBean {
@Resource
private TimerService ts;
@PostConstruct
public void init() {
final TimerConfig tc = new TimerConfig();
tc.setPersistent(false);
ts.createIntervalTimer(0, 5000, tc);
}
@Timeout
public void timeout() {
Logger.getLogger(NewSessionBean.class.getName()).severe("==> timeout called...");
}
}
我在玻璃鱼4.1中运行控制台输出
Information: mavenproject2 was successfully deployed in 721 milliseconds.
Schwerwiegend: ==> timeout called...
Schwerwiegend: ==> timeout called...
Schwerwiegend: ==> timeout called...