JavaEE - 单例 EJB 计时器不起作用

JavaEE - Singleton EJB Timer doesn't work

我想每 5 秒打印一次字符串 "qui"。 我创建了一个 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!");
    }

}

您的代码似乎没问题...这是我的工作示例:

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...");
    }

}

当我在 glass fish 4.1

中 运行 控制台输出
Information:   mavenproject2 was successfully deployed in 721 milliseconds.
Schwerwiegend:   ==> timeout called...
Schwerwiegend:   ==> timeout called...
Schwerwiegend:   ==> timeout called...

尝试简单一点。

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");
    }
}