【问题标题】:Invoke EJB Remote Bean in Web App在 Web 应用程序中调用 EJB 远程 Bean
【发布时间】:2016-09-28 04:43:37
【问题描述】:

我刚刚开始阅读 EJB。

是否可以在单独的 Web 应用程序(战争)中调用远程 bean?如果可能的话,如何实现它。我试过了,如果两者都在同一个 EAR 中,web 应用程序可以调用远程 bean,但我想在单独的 EJB + WAR 中调用它。

ps。我使用 glassfish 4.0

【问题讨论】:

    标签: java ejb war


    【解决方案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 模块来构建通道

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 2012-08-04
      • 1970-01-01
      相关资源
      最近更新 更多