I have a problem with Injecting @RequestScoped
Bean into Runnable
class.
This is my Resource class
@ApplicationScoped
@Path("/process")
public class TestResource {
private static ExecutorService executor = Executors.newFixedThreadPool(20);
@POST
public void process(Integer id, @Suspended AsyncResponse ar) {
TestRunnable testRunnable = new TestRunnable();
testRunnable.setId(id);
executor.execute(() -> {
testRunnable.run();
ar.resume("OK");
});
}
And here is my TestRunnable class:
public class TestRunnable implements Runnable {
private Integer id;
private ServiceBean serviceBean;
public void asyncProcess() {
serviceBean = CDI.current().select(ServiceBean.class).get();
serviceBean.process(id);
}
@Override
public void run() {
asyncProcess();
}
public void setId(Integer id) {
this.id = id;
}
}
When I try to connect to the endpoint I get the following error?
WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
I am sure that the problem lies in wrong injection of my ServiceBean...
testRunnable.run();
AND you are running on a fairly recent application server, you can try injecting theManagedExecutorService
, instead of creating your own:@Resource ManagedExecutorService executor
. The rest of the code remains the same. If this works for you, you can even configure the application server to produce a customizedManagedExecutorService
(e.g. with 20 threads). – Nikos Paraskevopoulos