使用两个接口创建EJB。 @本地接口为web模块和@Remote接口为本地应用客户端 [英] Create EJB with two interfaces. @Local interface for web module and @Remote interface for local app client
问题描述
远程
接口 @Stateless
public class CoreEJB实现CoreEJBRemote {
@PersistenceContext(unitName =CoreWeb-ejbPU)
private EntityManager em;
@Override
public void packageProcess(String configFileName)throws异常{
//进程逻辑
}
@Override $ b $公共< T> T create(T t){
em.persist(t);
return t;
}
@Override
public< T> T find(Class< T> type,Object id){
return em.find(type,id);
}
...
}
远程
界面不在EAR中,看起来像这样
@远程
public interface CoreEJBRemote {
public void packageProcess(java.lang.String configFileName)throws java.lang.Exception;
public< T extends java.lang.Object> T创建(T t);
public< T extends java.lang.Object> T find(java.lang.Class< T> type,java.lang.Object id);
}
主要
的应用程序客户端在
public class Main {
@EJB
private static CoreEJBRemote coreEJB;
public static void main(String [] args){
coreEJB.packageProcess(path / to / a / file);
}
}
现在我想创建一个本地
接口,以便在我的Web模块的 Managed Bean
中,我可以通过本地调用访问EJB。
我只是从更改
to CoreEJB
public class CoreEJB实现CoreEJBRemote public class CoreEJB实现CoreEJBRemote,CoreEJBLocal
并创建 @Local
接口调用 CoreEJBLocal
内部 EJB包
?还是会有额外的东西?我想要我的Managed Bean代码就像这样
@ManagedBean
@RequestScoped
public void testView {
@EJB
私人CoreEJBLocal coreEJB;
public void add(Something something){
coreEJB.add(something); //本地调用
}
}
我只是从public
更改CoreEJB CoreEJB实现CoreEJBRemote
到公共类CoreEJB实现
CoreEJBRemote,CoreEJBLocal并创建
@Local接口在EJB包中调用CoreEJBLocal
?
是的,这应该是足够的。你试过了吗?失败了吗请记住,本地接口是通过引用的,远程接口是通过值的。如果调用者(或bean)在返回值(或分别为参数)上突变状态,那么您将在两者之间获得不同的行为。您必须在您的API合同中仔细管理。
I have a EAR package that contains a web module and EJB. The EJB is currently exposed its contains to local app client via Remote
interface
@Stateless
public class CoreEJB implements CoreEJBRemote {
@PersistenceContext(unitName = "CoreWeb-ejbPU")
private EntityManager em;
@Override
public void packageProcess(String configFileName) throws Exception {
//Process logics
}
@Override
public <T> T create(T t) {
em.persist(t);
return t;
}
@Override
public <T> T find(Class<T> type, Object id) {
return em.find(type, id);
}
...
}
The Remote
interface is not in EAR, and look like this
@Remote
public interface CoreEJBRemote {
public void packageProcess(java.lang.String configFileName) throws java.lang.Exception;
public <T extends java.lang.Object> T create(T t);
public <T extends java.lang.Object> T find(java.lang.Class<T> type, java.lang.Object id);
}
The Main
of the app client is below
public class Main {
@EJB
private static CoreEJBRemote coreEJB;
public static void main(String[] args) {
coreEJB.packageProcess("path/to/a/file");
}
}
Now i want to create an Local
interface for the EJB so that in the Managed Bean
of my web module, I can access the EJB via local invocation.
Do I just change CoreEJB
from public class CoreEJB implements CoreEJBRemote
to public class CoreEJB implements CoreEJBRemote, CoreEJBLocal
and create @Local
interface call CoreEJBLocal
inside EJB package
? Or will there be something extra? I want my Managed Bean code to be like this
@ManagedBean
@RequestScoped
public void testView{
@EJB
private CoreEJBLocal coreEJB;
public void add(Something something){
coreEJB.add(something); //Local invocation
}
}
Do I just change CoreEJB from public class CoreEJB implements CoreEJBRemote to public class CoreEJB implements CoreEJBRemote, CoreEJBLocal and create @Local interface call CoreEJBLocal inside EJB package?
Yes, this should be sufficient. Did you try? Did it fail? Keep in mind that local interfaces are pass-by-reference and remote interfaces are pass-by-value. If callers (or the bean) mutate state on the return value (or arguments, respectively), then you're going to get different behavior between the two. You must manage this careful in your API contract.
这篇关于使用两个接口创建EJB。 @本地接口为web模块和@Remote接口为本地应用客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!