我有以下的情況,在客戶耳邊應用程序有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
?
在此先感謝
感謝您的回答,我的疑問是,如果可以安全地將野蠅代理引用存儲在同時訪問的單一bean(CDI ApplicationScoped)中,換句話說,野蠅代理實現是線程安全的? – landal79
我已將更多信息添加到答案 –
它不是控制併發的@Singleton,它是會話bean。一個會話bean引用(您的代理)=>一個會話bean實例 –