我将单例EJB公开为restful服务;要求是通过Rest API启动计时器。因为这将是web应用程序,我在war文件中打包ejb类。我成功地能够部署bean并调用Web服务,但无法启动计时器,因为@Resource注释没有在前面提到的singleton ejb中注入SessionContext(也尝试使用无状态会话bean)。调试时,我看到SessionContext对象为null。有关如何在此方案中使计时器服务工作的任何建议?以下是详细信息:
TimerSessionBean.java:
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Singleton;
import javax.ejb.Timer;
import javax.ejb.Timeout;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
@Singleton
@Path("/config")
public class TimerSessionBean {
@Resource
private SessionContext context;
@GET
@Path("{id}")
public void createTimer(@PathParam("id") long duration) {
context.getTimerService().createTimer(duration, "Hello World!");
}
@Timeout
public void timeOutHandler(Timer timer) {
System.out.println("timeoutHandler : " + timer.getInfo());
}
}
的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<listener>
<listener-class>org.apache.shiro.web.env.EnvironmentLoaderListener</listener-class>
</listener>
<filter>
<filter-name>ShiroFilter</filter-name>
<filter-class>org.apache.shiro.web.servlet.ShiroFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ShiroFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
AppConfig.java:
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
@ApplicationPath("/")
public class AppConfig extends Application {
@Override
@SuppressWarnings("unchecked")
public Set<Class<?>> getClasses() {
Set<Class<?>> set = new HashSet<Class<?>>();
set.add(TimerSessionBean.class);
return set;
}
}
摘自服务器日志:
20:09:08,850 INFO [org.jboss.as.ejb3.deployment.processors.EjbJndiBindingsDeploymentUnitProcessor] (MSC service thread 1-1) JNDI bindings for session bean named TimerSessionBean in deployment unit deployment "myapp-1.0.war" are as follows:
java:global/myapp-1.0/TimerSessionBean!app.ws.resources.TimerSessionBean
java:app/myapp-1.0/TimerSessionBean!app.ws.resources.TimerSessionBean
java:module/TimerSessionBean!app.ws.resources.TimerSessionBean
java:global/myapp-1.0/TimerSessionBean
java:app/myapp-1.0/TimerSessionBean
java:module/TimerSessionBean
答案 0 :(得分:1)
您正在将您的ejb打包在war文件中,这意味着您正在使用EJB 3.1 Lite ejb功能reduced set。 我不确定为什么没有注入SessionContext实例, 但请记住,在这个简化版本的ejb规范中,TimerService不可用。