将 @RequestScoped Bean 注入到 Runnable 类中

huangapple go评论54阅读模式
英文:

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>

&gt; *I am sure that the problem lies in wrong injection of my ServiceBean*

Nope. The problem is exactly as the exception message says:

&lt;!-- language: lang-none --&gt;

    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&#39;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>



huangapple
  • 本文由 发表于 2020年1月3日 18:58:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577354.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定