2016-09-28 45 views
0

我剛開始閱讀EJB。在Web應用程序中調用EJB遠程Bean

是否有可能在單獨的web應用(戰爭)中調用遠程bean?如果它可能如何實現它。我試了一下,如果在同一個EAR中,Web應用程序可以調用遠程bean,但是我想在單獨的EJB + WAR中調用它。

ps。我使用的GlassFish 4.0

回答

1

對於GlassFish你可以試試這個: https://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB

下面我舉的例子爲WebLogic。

1)在你的EAR你應該有@Remote註釋的接口和它的實現

@Remote 
public interface Calculator { 

    public int add(int a, int b); 

} 

@Stateless(mappedName = "myCalculator") 
public class CalculatorImpl implements Calculator { 
    @Override 
    public int add(int a, int b) { 
     return a + b; 
    } 
} 

2)你應該有一個客戶端,這將調用遠程計算器

private static Calculator getRemoteCalculator() { 
    Hashtable<String, String> props = new Hashtable<String, String>(); 

    props.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory"); 
    props.put(Context.PROVIDER_URL, "t3://localhost:7001");  


    try { 
     InitialContext ctx = new InitialContext(props); 
     return (Calculator) ctx.lookup("myCalculator#com.javaee.Calculator"); 
    } catch (NamingException e) { 
     throw new RuntimeException(e); 
    } 
} 

3)客戶端你應該添加你的遠程計算器EJB模塊來構建pass