使用Jax-RS和CDI调度任务

huangapple go评论79阅读模式
英文:

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 依赖项:

<!-- com.google.code.gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
</dependency>

<!-- com.fasterxml.jackson.core -->
<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>
<!-- ... (其他依赖项) ... -->

这是我的 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 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;
    }
}
英文:

I'm try to understand how to manage scheduled tasks in my project with Jax-RS and CDI.
With Spring I was easily able to achive that with ThreadPoolTaskScheduler or @Scheduled annotation and I'm trying to replicate both ways without success.

First of all, I'm using Wildfly 14 and this seems to cause some issues because I've tryied to inject with @Resource both ManagedScheduledExecutorService and TimerService but Wildfly throws exception of missing dependencies (but the Admin guide didn't help me about that).

Instead of inject resources I've tryied to use a singleton object like this:

@Singleton
public class CacheManager {
	private final static Logger log = LogManager.getLogger();

	public CacheManager() {
		 log.error(&quot;######## Init&quot; + LocalTime.now());
	}
	
   	 @Schedule(hour = &quot;*&quot;, minute = &quot;*&quot;, second = &quot;*/1&quot;)
	 private void timeout() {
		 log.error(&quot;######## &quot; + LocalTime.now());
	 }
	
}

But the method is never called.

So I'm not understanding what I'm missing. Maybe I have wrongly configured the project so this is my pom.xml dependencies:

	&lt;dependency&gt;
		&lt;groupId&gt;com.google.code.gson&lt;/groupId&gt;
		&lt;artifactId&gt;gson&lt;/artifactId&gt;
		&lt;version&gt;2.8.2&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
		&lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
		&lt;version&gt;2.10.2&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;!-- Import the JAX-RS API, we use provided scope as the API is included in JBoss WildFly --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
		&lt;artifactId&gt;resteasy-jaxrs&lt;/artifactId&gt;
		&lt;version&gt;3.10.0.Final&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
		&lt;artifactId&gt;jaxrs-api&lt;/artifactId&gt;
		&lt;version&gt;3.0.12.Final&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.jboss.resteasy&lt;/groupId&gt;
		&lt;artifactId&gt;resteasy-multipart-provider&lt;/artifactId&gt;
		&lt;version&gt;3.0.12.Final&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;!-- Import the CDI API, we use provided scope as the API is included in JBoss WildFly --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;javax.enterprise&lt;/groupId&gt;
		&lt;artifactId&gt;cdi-api&lt;/artifactId&gt;
		&lt;version&gt;2.0&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;!-- Import the JSF API, we use provided scope as the API is included in JBoss WildFly --&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.jboss.spec.javax.faces&lt;/groupId&gt;
		&lt;artifactId&gt;jboss-jsf-api_2.2_spec&lt;/artifactId&gt;
		&lt;version&gt;2.2.14&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
		&lt;artifactId&gt;log4j-api&lt;/artifactId&gt;
		&lt;version&gt;2.11.2&lt;/version&gt;
	&lt;/dependency&gt;
	&lt;dependency&gt;
		&lt;groupId&gt;org.apache.logging.log4j&lt;/groupId&gt;
		&lt;artifactId&gt;log4j-core&lt;/artifactId&gt;
		&lt;version&gt;2.11.2&lt;/version&gt;
	&lt;/dependency&gt;

	&lt;dependency&gt;
		&lt;groupId&gt;javax.ejb&lt;/groupId&gt;
		&lt;artifactId&gt;ejb-api&lt;/artifactId&gt;
		&lt;version&gt;3.0&lt;/version&gt;
		&lt;scope&gt;provided&lt;/scope&gt;
	&lt;/dependency&gt;
&lt;/dependencies&gt;

This is my beans.xml

&lt;beans xmlns=&quot;http://xmlns.jcp.org/xml/ns/javaee&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot; http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd&quot; bean-discovery-mode=&quot;annotated&quot;&gt;
&lt;/beans&gt;

I am using Java 8.

EDIT: the CacheManager is instantiated in the JAX-WS Application

@ApplicationScoped
@ApplicationPath(&quot;/rest&quot;)
public class JaxRsActivator extends Application {
	
	private static final Logger logger = LogManager.getLogger();

    private Set&lt;Object&gt; singletons = new HashSet&lt;Object&gt;();
    private HashSet&lt;Class&lt;?&gt;&gt; classes = new HashSet&lt;Class&lt;?&gt;&gt;();

    public JaxRsActivator() {
    	singletons.add(new CorsFilter());
        singletons.add(new CacheManager());      
    }	

    @Override
    public Set&lt;Object&gt; getSingletons() {
        return singletons;
    }

    @Override
    public HashSet&lt;Class&lt;?&gt;&gt; getClasses(){
      return classes;
    }
}

答案1

得分: 1

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

英文:

I've found the solutions: @Singleton must be javax.ejb.Singleton and not javax.inject.Singleton.

huangapple
  • 本文由 发表于 2020年4月3日 19:38:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61011026.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定