2016-09-28 93 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