Abner Bessi
Abner Bessi

Reputation: 51

How to schedule job by helidon MP?

Im Try to create schedule job sample, buy task dont execute.

what's wrong?

@ApplicationScoped
public class CustomApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(FileService.class);
        return classes;
    }

}
public class FileService {

    public void schedulerFeature() throws InterruptedException {
        Scheduling.fixedRateBuilder()
                .delay(4)
                .initialDelay(2)
                .timeUnit(TimeUnit.SECONDS)
                .task(inv -> {
                    System.out.println("Running in:" + Thread.currentThread().getName());
                    System.out.println("Every 4 seconds an action, with an initial delay");
                })
                .build();

        Thread.sleep(12000);
    }

}

Im Try to create schedule job sample, buy task dont execute.

Upvotes: 1

Views: 621

Answers (2)

Abner Bessi
Abner Bessi

Reputation: 51

I revised my application and i had imported wrong maven lib. To schedule a job It is necessary import microprofile scheduling maven artifact:

<dependency>
    <groupId>io.helidon.microprofile.scheduling</groupId>
    <artifactId>helidon-microprofile-scheduling</artifactId>
    <version>3.0.2</version>
</dependency>
import java.util.HashSet;
import java.util.Set;

@ApplicationScoped
public class CustomApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<Class<?>>();
        classes.add(FileService.class);
        return classes;
    }

}
import io.helidon.microprofile.scheduling.Scheduled;
import jakarta.enterprise.context.ApplicationScoped;


/**
 * File service.
 */

@ApplicationScoped
public class FileService {

    @Scheduled("0/2 * * * * ? *")
    //@FixedRate(1)
    public void schedulerFeature()  {
        System.out.println("Running");
    }

}

Upvotes: 4

Laird Nelson
Laird Nelson

Reputation: 16238

I don't know which version of Helidon you're using, and hence I don't know which version of JAX-RS/Jakarta RESTful Web Services you're using. For simplicity and brevity, I will assume you are using Helidon 3.x and therefore Jakarta RESTful Web Services 3.0.0.

This is not a Helidon question, but rather a basic JAX-RS/Jakarta RESTful Web Services question. You are really asking: "Why is my FileService class not instantiated by Jersey?"

Checking the documentation for Application#getClasses(), we can see that it reads: "Get a set of root resource, provider and feature classes." Your FileService class does not meet any of these requirements so it is simply ignored.

Guessing some more: I see you use the word "feature" in your example. This suggests that perhaps you want this class to actually be a true Jakarta RESTful Web Services Feature. Once again, the documentation here will tell you what you need to know to do next.

Upvotes: 4

Related Questions