简体   繁体   English

在新线程中使用EJB或注入

[英]Using EJB or Inject in a new thread

I have a SOAP WebService class that has an EJB injected and some other class injected in it. 我有一个SOAP WebService类,其中已注入EJB,并且已注入其他一些类。 Like: 喜欢:

public class SuperWS {

    @EJB
    private SuperDaoEjb superDaoEjb;

    @Inject
    private Partner partnerComponent;

    //......

    public Response invoke(Request req) {

        //Some logic

        Thread t = new Thread(new SuperRunnable(superDaoEjb, partnerComponent));

        t.start();

        return req;

    }

}

I have to do some logic then return the response but start a thread to process some more. 我必须做一些逻辑,然后返回响应,但启动一个线程来处理更多内容。 In that thread I need both superEjbDao and partnerComponent. 在该线程中,我需要superEjbDao和partnerComponent。 But as I know those objects are container managed. 但据我所知,这些对象是容器管理的。 So I can't really say that container won't give the proxy to someone else or dispose it. 因此,我不能说容器不会将代理提供给其他人或将其处置。 I feel like there is a problem with my design. 我觉得我的设计有问题。 How can I solve the problem? 我该如何解决这个问题?

I guess there is no problem. 我想没有问题。 It is essential design. 这是必不可少的设计。 Just be sure that both SuperDaoEjb and Partner have thread-safe code and you'll be fine. 只要确保SuperDaoEjb和Partner都具有线程安全代码,就可以了。 You do need to care about what reference you will get from Container for SuperDaoEjb and Partner. 您确实需要在意从SuperDaoEjb和合作伙伴的Container中获得什么参考。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM