与Jax RS和CDI一起安排任务



我试图了解如何使用Jax RS和CDI管理项目中的计划任务。有了Spring,我可以很容易地通过ThreadPoolTaskScheduler@Scheduled注释实现这一点,并且我正试图复制这两种方法,但都没有成功。

首先,我使用的是Wildfly 14,这似乎会引起一些问题,因为我曾尝试用@Resource注入ManagedScheduledExecutorServiceTimerService,但Wildfly抛出了缺失依赖项的异常(但管理员指南对此没有帮助(。

我没有注入资源,而是尝试使用这样的单例对象:

@Singleton
public class CacheManager {
private final static Logger log = LogManager.getLogger();
public CacheManager() {
log.error("######## Init" + LocalTime.now());
}
@Schedule(hour = "*", minute = "*", second = "*/1")
private void timeout() {
log.error("######## " + LocalTime.now());
}
}

但该方法从未被调用。

所以我不明白我错过了什么。也许我错误地配置了项目,所以这是我的pom.xml依赖项:

<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.2</version>
</dependency>
<!-- Import the JAX-RS API, we use provided scope as the API is included in JBoss WildFly -->
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.10.0.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>jaxrs-api</artifactId>
<version>3.0.12.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>3.0.12.Final</version>
<scope>provided</scope>
</dependency>
<!-- Import the CDI API, we use provided scope as the API is included in JBoss WildFly -->
<dependency>
<groupId>javax.enterprise</groupId>
<artifactId>cdi-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<!-- Import the JSF API, we use provided scope as the API is included in JBoss WildFly -->
<dependency>
<groupId>org.jboss.spec.javax.faces</groupId>
<artifactId>jboss-jsf-api_2.2_spec</artifactId>
<version>2.2.14</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>javax.ejb</groupId>
<artifactId>ejb-api</artifactId>
<version>3.0</version>
<scope>provided</scope>
</dependency>
</dependencies>

这是我的beans.xml

<beans xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd" bean-discovery-mode="annotated">
</beans>

我正在使用Java 8。

EDIT:CacheManager在JAX-WS应用程序中实例化

@ApplicationScoped
@ApplicationPath("/rest")
public class JaxRsActivator extends Application {
private static final Logger logger = LogManager.getLogger();
private Set<Object> singletons = new HashSet<Object>();
private HashSet<Class<?>> classes = new HashSet<Class<?>>();
public JaxRsActivator() {
singletons.add(new CorsFilter());
singletons.add(new CacheManager());      
}   
@Override
public Set<Object> getSingletons() {
return singletons;
}
@Override
public HashSet<Class<?>> getClasses(){
return classes;
}
}

我找到了解决方案:@Singleton必须是javax.ejb.Singleton,而不是javax.inject.Singleton.

最新更新