2

在将应用程序迁移到最新的 Liberty 期间,我在创建 Timer 时遇到了一些问题。Timer 在initialize() 方法(@PostConstruct) 中的@Singleton 注释类中创建。代码很简单:

ScheduleExpression schedule = new ScheduleExpression();
setScheduleExpressionTime(schedule);

TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
timerScheduled = timerService.createCalendarTimer(schedule, timerConfig);

当我部署应用程序时,我收到了为我的持久性计时器创建数据源的建议的异常。我知道 - 默认情况下,计时器是持久性的,需要数据源和表来保持它的状态,但我要求创建非持久性。

我试图从服务器功能中删除对持久性计时器的支持(我将 Java EE 7 Full Platform 功能更改为 Java™ EE 7 Web Profile,因此不再需要 ejb-3.2)。现在我有例外: CNTR4019E:无法创建或访问持久计时器。server.xml 文件中配置的任何功能都不支持持久 EJB 计时器。

所以,看起来服务器忽略了我创建非持久性计时器的要求并总是试图创建持久性。这段代码以前可以与一些旧的 WAS (JEE6) 一起使用,但现在我无法部署它。

有人遇到过这个问题吗?可能是我做错了什么?先感谢您。

4

2 回答 2

2

我已经在本地对此进行了测试,并且对我来说效果很好。这是我用于比较的完整 EJB 和 server.xml 配置。

如果这对您不起作用,您将需要提供有关如何创建/提交计时器的更多详细信息以及有关服务器配置的更多详细信息。

EJB 类:

@Singleton
@Startup
public class MyEJB {

    @Resource
    TimerService timerService;

    @PostConstruct
    @Timeout
    public void doThing() {
        System.out.println("starting EJB post-construct");
        ScheduleExpression schedule = new ScheduleExpression();
        schedule.second(5);

        TimerConfig timerConfig = new TimerConfig();
        timerConfig.setPersistent(false);
        Timer timerScheduled = timerService.createCalendarTimer(schedule, timerConfig);
        System.out.println("Is persistent: " + timerScheduled.isPersistent());
    }
}

服务器配置:

<server>    
    <featureManager>
        <feature>webProfile-7.0</feature>
    </featureManager>

    <application location="app1.war"/>    
</server>
于 2017-10-30T14:44:58.357 回答
0

我找到了原因。这真的是我的错。我错过了一个创建计时器的地方。timerService 被使用两次来创建定时器。第一次在我上面描述的地方,第二次在一个活动中。第二次看起来像:

timerService.createTimer(NB_OF_MILLISECONDS_UNTIL_FIRST_START, null);

对于某些旧的 WAS 版本,此代码可能会创建一个非持久性计时器,但现在应该将其更改为:

TimerConfig timerConfig = new TimerConfig();
timerConfig.setPersistent(false);
timer = timerService.createSingleActionTimer(NB_OF_MILLISECONDS_UNTIL_FIRST_START, timerConfig);

创建计时器时要小心。:-) 谢谢你的帮助。

于 2017-10-30T16:50:31.710 回答