在将应用程序迁移到最新的 Liberty 期间,我在创建计时器时遇到了一些问题。 Timer 是在 @Singleton 注解的类中的initialize() 方法(@PostConstruct)中创建的。代码很简单:
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)。现在我有异常(exception): CNTR4019E:无法创建或访问持久计时器。 server.xml 文件中配置的任何功能都不支持持久 EJB 计时器。
所以,看起来服务器忽略了我创建非持久性计时器的要求,并且总是尝试创建持久性。这段代码之前可以在一些旧的 WAS (JEE6) 上运行,但现在我无法部署它。
有人遇到过这个问题吗?也许我做错了什么? 预先感谢您。
最佳答案
我已经在本地测试过了,它对我来说工作正常。以下是我用于比较的完整 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>
关于java - WAS Liberty 17.0.0.3 上的非持久性计时器创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47016335/