I'm trying to implement Java EE's managed executor ManagedExecutorService
to submit callable tasks, where each task makes a call to an injected bean method.
I'm using the Instance
class to make the container aware of the task object, but when get()
is executed the following exception is thrown:
Caused by: javax.ejb.EJBException: org.jboss.weld.exceptions.UnsatisfiedResolutionException: WELD-001334: Unsatisfied dependencies for type MyTask with qualifiers @Default
I'm running this on WildFly 14.
The injected bean:
@Stateless
public class MyBean {
public void print() {
System.out.println("MyBean printed");
}
}
The task:
@Stateless
public class MyTask implements Callable<String> {
@Inject
MyBean myBean;
@Override
public String call() throws Exception {
System.out.println("MyTask called");
myBean.print();
return "Task called";
}
}
The task invoker:
@Stateless
public class TestBean {
@Inject
Instance<MyTask> myTaskInstance;
@Resource
private ManagedExecutorService executor;
public void test() throws InterruptedException, ExecutionException {
List<Callable<String>> tasks = new ArrayList<>();
MyTask task = myTaskInstance.get(); // <------- Exception is thrown here
tasks.add(task);
MyTask task2 = myTaskInstance.get();
tasks.add(task2);
List<Future<String>> taskResults = null;
taskResults = executor.invokeAll(tasks);
List<String> results = new ArrayList<>();
for(Future<String> taskResult : taskResults) {
results.add(taskResult.get());
}
}
}
Why is the exception thrown and how to fix this problem? Is there a library missing in the classpath?