2016-11-17 58 views
2

我有以下的情況,在客戶耳邊應用程序有CDI @ApplicationScoped Bean上的@PostConstruct回調執行遠程SLSB查找並緩存所獲得的代理:蜻蜓8:ejb遠程代理線程安全嗎?

@ApplicationScoped 
@Typed({ ServiceInterface.class }) 
public class RemoteServiceProxy implements ServiceInterface 
{ 
    /** 
    * Remote service. 
    */ 
    private RemoteService remoteService; 

    /** 
    * Default constructor. 
    */ 
    public RemoteServiceProxy() 
    { 
     super(); 
    } 

    /** 
    * PostConstruct callback. 
    * 
    * @throws RuntimeException 
    *    Error while looking up remote proxy 
    */ 
    @PostConstruct 
    protected void onPostConstruct() 
    { 
     try 
     { 
      remoteService = serviceLocator.lookup(ActivityRemoteEntityService.class); 

      Properties jndiProps = new Properties(); 
      jndiProps.put(Context.URL_PKG_PREFIXES, "org.jboss.ejb.client.naming"); 
      jndiProps.put(Context.PROVIDER_URL, "http-remoting://localhost:8080"); 
      jndiProps.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory"); 
      jndiProps.put("jboss.naming.client.ejb.context", "true"); 

      Context context = new InitialContext(jndiProps); 

      remoteService = (RemoteService) context.lookup(
       "application.backend/application.backend-service//RemoteServiceImpl!com.application.remote.RemoteService"); 
     } catch (NamingException e) 
     { 
      throw new RuntimeException(e); 
     } 
    } 

    ... 

} 

我想知道,如果緩存代理中字段remoteService是線程安全的,因此RemoteServiceProxy可以使用@ApplicationScoped進行註釋,或者我必須爲每個調用執行一次新的代理查找?或者最好使用@Stateless

在此先感謝

回答

1

的EJB 3.2規範有以下說:

3.4.9到Session Bean的併發訪問參考

它是允許獲取會話bean和參考嘗試從多個線程同時調用同一個引用對象。但是,每個線程上產生的客戶端行爲取決於目標bean的併發語義。有關會話bean的併發行爲的詳細信息,請參見第4.3.13節和第4.8.5節。

§4.3.13基本上說會話bean的併發調用將被容器序列化。

§4.8.5描述了對Singleton會話Bean的併發訪問的語義。

因此,爲了符合遠程代理的需要,必須具有固有的線程安全性,因爲它必須遵循「會話bean引用」所需的語義。也就是說,如果你在EJB中存儲了這樣一個引用,那麼這個引用將永遠只能同時處理一個方法調用(因爲這樣的調用是「序列化的」)。這可能會導致應用程序出現不良的瓶頸。

+0

感謝您的回答,我的疑問是,如果可以安全地將野蠅代理引用存儲在同時訪問的單一bean(CDI ApplicationScoped)中,換句話說,野蠅代理實現是線程安全的? – landal79

+0

我已將更多信息添加到答案 –

+0

它不是控制併發的@Singleton,它是會話bean。一個會話bean引用(您的代理)=>一個會話bean實例 –