英文:
Inject @RequestScoped Bean into Runnable class
问题
我在将@RequestScoped
的Bean注入到Runnable
类中时遇到了问题。
这是我的资源类:
@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");
});
}
}
这是我的TestRunnable类:
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;
}
}
当我尝试连接到端点时,我收到以下错误:
WELD-001303: No active contexts for scope type javax.enterprise.context.RequestScoped
我确信问题出在错误地注入了我的ServiceBean...
英文:
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...
答案1
得分: 1
> *我确信问题出在我ServiceBean的错误注入*
不是的。问题正如异常消息所说:
<!-- language: lang-none -->
无法找到scope类型javax.enterprise.context.RequestScoped的活动上下文
根本没有可用的请求范围。
请求范围仅对在HTTP请求创建的原始线程内运行的代码可用。但是在这里,您基本上正在创建一个完全独立(异步!)于由HTTP请求创建的原始线程的新线程。
将所需的请求范围数据作为构造函数参数传递。
TestRunnable testRunnable = new TestRunnable(serviceBean);
或者更好的是,将`process()`逻辑移至`TestRunnable`本身。
TestRunnable testRunnable = new TestRunnable(id);```
<details>
<summary>英文:</summary>
> *I am sure that the problem lies in wrong injection of my ServiceBean*
Nope. The problem is exactly as the exception message says:
<!-- language: lang-none -->
No active contexts for scope type javax.enterprise.context.RequestScoped
There is no request scope avaliable at all.
The request scope is only available to the code running within the original thread created by the HTTP request. But you're here basically creating a new thread which runs completely independently (asynchronously!) from the original thread created by the HTTP request.
Pass the desired data from the request scope as a constructor argument instead.
TestRunnable testRunnable = new TestRunnable(serviceBean);
Or even better, move the `process()` logic into `TestRunnable` itself.
TestRunnable testRunnable = new TestRunnable(id);
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论