簡體   English   中英

使用兩個接口創建EJB。 Web模塊的@Local接口和本地應用程序客戶端的@Remote接口

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

我有一個包含Web模塊和EJB的EAR軟件包。 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);
    }

    ...
}

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);

}

應用客戶端的Main內容如下

public class Main {
    @EJB
    private static CoreEJBRemote coreEJB;

    public static void main(String[] args) {
        coreEJB.packageProcess("path/to/a/file");
    }
}

現在,我想為EJB創建一個Local接口,以便在我的Web模塊的Managed Bean中,我可以通過本地調用來訪問EJB。

我是否只是將CoreEJBpublic class CoreEJB implements CoreEJBRemotepublic class CoreEJB implements CoreEJBRemote, CoreEJBLocal並在EJB package內創建@Local接口調用CoreEJBLocal 還是會有其他東西? 我希望我的Managed Bean代碼像這樣

@ManagedBean
@RequestScoped
public void testView{
    @EJB
    private CoreEJBLocal coreEJB;

    public void add(Something something){
        coreEJB.add(something); //Local invocation
    }
}

我是否只是將CoreEJB從公共類CoreEJB實現CoreEJBRemote更改為公共類CoreEJB實現CoreEJBRemote,CoreEJBLocal並在EJB包內創建@Local接口調用CoreEJBLocal?

是的,這應該足夠了。 你試過了嗎? 失敗了嗎? 請記住,本地接口是按引用傳遞的,而遠程接口是按值傳遞的。 如果調用者(或bean)使返回值(或分別為參數)的狀態發生變化,那么您將在兩者之間得到不同的行為。 您必須在API合同中對此進行謹慎處理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM