0
votes

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("######## Init" + LocalTime.now());
    }

     @Schedule(hour = "*", minute = "*", second = "*/1")
     private void timeout() {
         log.error("######## " + 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:

    <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>

This is my 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>

I am using Java 8.

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

@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;
    }
}
1
Do you get it to work with a plain servlet? or plain jsp? Or a jax-ws? Or JSF? Or even a unit test? With 99% certainty this is not jax-rs related. - Kukeltje
Jax-ws which works correctly. - DeooK
That is not what I mean. By stating it does not work with jax-rs you sort of implicitly state you have no problem when you want to start it via a servlet or something else. There is effectively nothing in your question jax-rs related. Create a servlet, does it work there? If not, your problem is more generic and you can search for more generic solutions/problems. - Kukeltje
Ok, now there is, but you do a 'new' of the CacheManager... Then it is not a managed instance, managed by a container like CDI or whatever. Effectively you'd have the same problem in a unittest or a servlet or... At least that is what I think. I use jax-rs, but use the Application extended class only for pure jax-rs things. Other real application scoped things are in generic @ApplicationScoped things. So I'm not sure if you are trying to use something JaxRS specific but design wise, I'd not do it like this - Kukeltje
Yes, you are right. I've removed the CacheManager from the Jax-rs initializator and I put a @Startup annotation on CacheManager so now starts with the application on it's own. - DeooK

1 Answers

1
votes

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