在WAS Liberty 17.0.0.3上创建非持久性计时器

问题描述 投票:2回答: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);

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

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

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

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

java ejb websphere-liberty java-ee-7 open-liberty
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>

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

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

© www.soinside.com 2019 - 2024. All rights reserved.