我正在尝试实现Java EE的托管执行程序ManagedExecutorService
来提交可调用任务,其中每个任务都调用一个注入的bean方法。
我正在使用Instance
类使容器知道任务对象,但是当执行get()
时,抛出以下异常:
引起:javax.ejb.EJBException:org.jboss.weld.exceptions.UnsatisfiedResolutionException:WELD-001334:类型MyTask与限定符@Default的不满意依赖关系
我在WildFly 14上运行它。
注入的bean:
@Stateless
public class MyBean {
public void print() {
System.out.println("MyBean printed");
}
}
任务:
@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";
}
}
任务调用者:
@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());
}
}
}
为什么抛出异常以及如何解决这个问题?类路径中是否缺少库?