Reputation: 5680
Quarkus has a https://quarkus.io/guides/scheduler to schedule tasks. However, I want to use ScheduledExecutorService
. Is this allowed in quarkus? For example, in wildfly there is ManagedScheduledExecutorService
which must be used because the server is managing the thread and it is not allowed for the user to manage threads. Is this also valid for quarkus?
Upvotes: 4
Views: 2582
Reputation: 1
This is a SimpleSheduler class
package : package io.quarkus.scheduler.runtime;
For developing the scheduler extension they used ScheduledExecutorService.
Here is one sheduled task using ScheduledExecutorService,
import javax.enterprise.context.ApplicationScoped;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ApplicationScoped
public class ScheduledExecutorRunnable {
List<String> list =
new ArrayList<String>();
public List<String> get() {
sheduleTask();
return list;
}
void sheduleTask() {
ScheduledExecutorService ses = Executors.newScheduledThreadPool(1);
Runnable task2 = () -> list.add("Running task2...");
task1();
ses.schedule(task2, 10, TimeUnit.SECONDS);
task3();
ses.shutdown();
}
void task1() {
list.add("Running task1...");
}
void task3() {
list.add("Running task3...");
}
}
Demo
import com.knf.dev.Resource.ScheduledExecutorService.ScheduledExecutorRunnable;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Path("/verify")
public class EndPoint {
@Inject
ScheduledExecutorRunnable scheduledExecutorRunnable;
@GET
@Path("/sheduler")
@Produces(MediaType.APPLICATION_JSON)
public List<String> sheduler() {
return scheduledExecutorRunnable.get();
}
}
Hit the Endpoint : http://localhost:8080/verify/sheduler
Output:
["Running task1...","Running task3..."]
Hit the Endpoint after 10s
Output:
["Running task1...","Running task3...","Running task2...","Running task1...","Running task3..."]
Upvotes: 1