使用 Jax-RS 和 CDI 安排任务

Schedule Task with Jax-RS and 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.

编辑: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。