简体   繁体   English

使用两个接口创建EJB。 Web模块的@Local接口和本地应用程序客户端的@Remote接口

[英]Create EJB with two interfaces. @Local interface for web module and @Remote interface for local app client

I have a EAR package that contains a web module and EJB. 我有一个包含Web模块和EJB的EAR软件包。 The EJB is currently exposed its contains to local app client via Remote interface EJB当前通过Remote接口向本地应用程序客户端公开其包含的内容

@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接口不在EAR中,看起来像这样

@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 应用客户端的Main内容如下

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. 现在,我想为EJB创建一个Local接口,以便在我的Web模块的Managed Bean中,我可以通过本地调用来访问EJB。

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 ? 我是否只是将CoreEJBpublic class CoreEJB implements CoreEJBRemotepublic class CoreEJB implements CoreEJBRemote, CoreEJBLocal并在EJB package内创建@Local接口调用CoreEJBLocal Or will there be something extra? 还是会有其他东西? I want my Managed Bean code to be like this 我希望我的Managed Bean代码像这样

@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? 我是否只是将CoreEJB从公共类CoreEJB实现CoreEJBRemote更改为公共类CoreEJB实现CoreEJBRemote,CoreEJBLocal并在EJB包内创建@Local接口调用CoreEJBLocal?

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. 如果调用者(或bean)使返回值(或分别为参数)的状态发生变化,那么您将在两者之间得到不同的行为。 You must manage this careful in your API contract. 您必须在API合同中对此进行谨慎处理。

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

相关问题 如果EJB暴露了@Remote接口,但是您注入了EJB bean而不是其Remote接口,那么这会触发远程或本地调用吗? - If EJB exposed @Remote interface, but you inject the EJB bean instead of its Remote interface, will this trigger a remote or local invocation? 使用远程接口正确配置JSF EJB应用程序 - Correct Configuration for JSF EJB application using remote interface Glassfish + NetBeans:为什么将JSF Web App作为表示模块需要同时引用EJB API模块和EJB实现模块 - Glassfish+NetBeans: Why does JSF Web App as presentation module need to reference both EJB API module and EJB implementation module 当我尝试注入Bean时,找不到具有类型接口的EJB - No EJB found with interface of type when I try to inject a Bean 如何在 JSF 中创建用户界面? - How to create User Interface in JSF? 通过使用ejb 3,jsf和jboss,可以从Web模块调用EJB方法吗? - By using ejb 3 , jsf and jboss is it possible to call an EJB method from web module? 两个不同的Maven Web项目如何共享同一个EJB - How Two different Maven Web project can share same EJB 在managedBean中注入远程EJB - Injecting a remote EJB in a managedBean JSF应用程序,Applet或JWS的条形码扫描仪界面? - Barcode scanner interface for JSF app, Applet or JWS? EJB-将数据发送到远程 - EJB - Send Data to remote
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM