在Liberty中创建EJB计时器

sch*_*uno 4 java ejb timer java-ee websphere-liberty

我需要创建一个EJB计时器(使用@Schedule),但是已经读过Websphere Liberty配置文件中不支持这个?根据之前在StackOverflow上发布的问题,截至2013年8月,它不受支持:

Websphere Liberty Profile中的Java EE-Timer/@Schedule

目前,当我尝试使用@Schedule注释时,我得到以下异常:

[ERROR   ] CWWKZ0004E: An exception occurred while starting the application 
<EAR>. The exception message was: com.ibm.ws.container.service.state.StateChangeException: com.ibm.ws.exception.RuntimeError: java.lang.IllegalStateException: The ejbPersistentTimer feature is enabled, but the defaultEJBPersistentTimerExecutor persistent executor cannot be resolved. The most likely cause is that the DefaultDataSource datasource has not been configured. Persistent EJB timers require a datasource configuration for persistence.
Run Code Online (Sandbox Code Playgroud)

问题是我确定了默认数据源.这是EJB代码 - 它非常简单,因为我只是试图测试计时器功能:

import javax.ejb.Schedule;
import javax.ejb.Stateless;

@Stateless
public class TimerBean {

    @Schedule(second="*/10", persistent=false)
    public void doSomething() {
        System.out.println("Hello World!");
    }

}
Run Code Online (Sandbox Code Playgroud)

更新:

我将我的dataSource id更改为"DefaultDataSource",现在我在启动服务器时在控制台中获得了不同的异常:

[ERROR   ] WTRN0078E: An attempt by the transaction manager to call start on a transactional resource has resulted in an error. The error code was XAER_RMERR. The exception stack trace follows: javax.transaction.xa.XAException: com.microsoft.sqlserver.jdbc.SQLServerException: Could not find stored procedure 'master..xp_sqljdbc_xa_start'.
at com.microsoft.sqlserver.jdbc.SQLServerXAResource.DTC_XA_Interface(SQLServerXAResource.java:647)
at com.microsoft.sqlserver.jdbc.SQLServerXAResource.start(SQLServerXAResource.java:679)
at com.ibm.ws.rsadapter.spi.WSRdbXaResourceImpl.start(WSRdbXaResourceImpl.java:1189)
at [internal classes]

[ERROR   ] J2CA0030E: Method enlist caught javax.transaction.SystemException: XAResource start association error:XAER_RMERR
at com.ibm.tx.jta.impl.RegisteredResources.startRes(RegisteredResources.java:1048)
at [internal classes]
Caused by: javax.transaction.xa.XAException: com.microsoft.sqlserver.jdbc.SQLServerException: Could not find stored procedure 'master..xp_sqljdbc_xa_start'.
at com.microsoft.sqlserver.jdbc.SQLServerXAResource.DTC_XA_Interface(SQLServerXAResource.java:647)
at com.microsoft.sqlserver.jdbc.SQLServerXAResource.start(SQLServerXAResource.java:679)
Run Code Online (Sandbox Code Playgroud)

这是计时器尝试写入我的SQL DB的结果,如果是,有没有办法避免这种情况?

And*_*ert 5

看起来您已ejbPersistentTimer-3.2打开该功能,因为您在配置DataSource时遇到异常.

如果要使用ejbPersistentTimer-3.2(或ejb-3.2包含它),则需要配置用于持久定时器的数据源.

既然你不需要持久EJB计时器(因为你必须persistent=false在你的@Schedule注释),你可以删除ejbPersistentTimer-3.2功能,只使用该ejbLite-3.2功能(不包括持久性计时功能).

ejbLite-3.2功能包括对非持久性计时器的支持,您无需担心配置DataSource.