【问题标题】:JavaEE - Singleton EJB Timer doesn't workJavaEE - 单例 EJB 计时器不起作用
【发布时间】:2015-01-16 19:09:22
【问题描述】:

我想每 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!");
    }

}

【问题讨论】:

  • 你能用应用服务器日志代替println吗?
  • 尝试删除您的 AS 的 tmp 和 data 文件夹,因为您的计时器的早期版本可能会混淆。您也可以尝试使用新项目部署代码。我在 JBoss EAP 6.2 上看到过这个问题,虽然我不知道实际原因,但在一个部署中不起作用的相同代码在另一个部署中起作用。
  • 亲爱的@Nunzio Meli,我有一个问题:当我使用@Timeout 时,即使我使用@Startup,我也无法在启动时加载调度程序。请看:stackoverflow.com/questions/42242037/…

标签: jakarta-ee ejb


【解决方案1】:

尝试简单一点。

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

【讨论】:

    【解决方案2】:

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

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

    当我在 glassfish 4.1 中运行时的控制台输出

    Information:   mavenproject2 was successfully deployed in 721 milliseconds.
    Schwerwiegend:   ==> timeout called...
    Schwerwiegend:   ==> timeout called...
    Schwerwiegend:   ==> timeout called...
    

    【讨论】:

    猜你喜欢
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    相关资源
    最近更新 更多