Glassfish 4 上 EJB 无状态 bean 运行 的 JNDI 查找

JNDI lookup of EJB Stateless bean running on Glassfish 4

我正在学习 EJB,我想让下面的代码工作,但到目前为止没有成功。 这是我的 EJB 项目代码:

@Stateless
public class CalcBean implements ICalcRemote {

    private static final long serialVersionUID = 5571798968598315142L;

    @Override
    public int add(int a, int b) {
        return a + b;
    }

}


package com.ejb.test.pckg;

import javax.ejb.Remote;

@Remote
public interface ICalcRemote extends ICalculator {

}


package com.ejb.test.pckg;

import java.io.Serializable;

public interface ICalculator extends Serializable {

    public int add(int a, int b);

}

我在 Eclipse Neon 中 运行 glassfish-4.1.1。

当我部署 EJB 项目时,我可以在日志中看到以下内容:

    2017-02-24T21:18:09.036-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean, java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote]

    2017-02-24T21:18:09.036-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean: [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]

这是我的客户端代码:

    import java.util.Properties;

    import javax.naming.Context;
    import javax.naming.InitialContext;

    import com.ejb.test.pckg.ICalcRemote;

    public class Main {
        public static void main(String[] args) {

            try {

                Properties props = new Properties();
                props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
                props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
                props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
                //
                props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
                props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

                Context ctx = new InitialContext();
                ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
                System.out.println(calc.add(5, 7));

            } catch (Exception e) {
                e.printStackTrace();
            }

        }

        /*
         * (non-Java-doc)
         *
         * @see java.lang.Object#Object()
         */
        public Main() {
            super();
        }

    }

但我运气不好。有什么关于如何让它工作的建议吗?

谢谢!

编辑

这是我的主(客户端),其中包含来自 EJBDemo 部署日志的信息:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import com.ejb.test.pckg.ICalcRemote;

public class Main {
    public static void main(String[] args) {

        try {

            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
            //
            // props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
            // props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

            /*
             * THIS IS from Glassfish log of EJBDemo deployment 
             * 
             * 2017-02-25T20:41:47.100-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean,
             * java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote] 2017-02-25T20:41:47.100-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean:
             * [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]
             *
             *
             */
            Context ctx = new InitialContext();
            ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");
            System.out.println(calc.add(5, 7));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * (non-Java-doc)
     *
     * @see java.lang.Object#Object()
     */
    public Main() {
        super();
    }

}

我终于开始工作了!尽管阅读了许多与此问题相关的帖子,但这是一个相当大的过程。这是我的配置的详细信息。希望这对某些人有用。

OS: 赢 10 IDE:月食霓虹灯 应用程序服务器:Glassfish 4.1.1 JDK: 1.8.0_111

我需要提到这篇最终让我找到答案的文章:

http://mavicode.com/2014/08/a-standalone-client-for-ejbs-running-on-glassfish-4/

所以,谢谢和荣誉。

首先,创建 EJB 演示项目:

package com.ejb.test.pckg;

import java.io.Serializable;

public interface ICalculator extends Serializable {

    public int add(int a, int b);

}


package com.ejb.test.pckg;

import javax.ejb.Remote;

@Remote
public interface ICalcRemote extends ICalculator {

}

package com.ejb.test.pckg;

import javax.ejb.Stateless;

// @Stateless(mappedName = "chester")
@Stateless
public class CalcBean implements ICalcRemote {

    private static final long serialVersionUID = 5571798968598315142L;

    @Override
    public int add(int a, int b) {
        return a + b;
    }

}

将其部署在服务器上(运行 as > 运行 在服务器上)> 检查日志以查看 JDNI 信息。它应该看起来像这样:

2017-02-25T20:41:47.100-0400|Info: Portable JNDI names for EJB CalcBean: [java:global/EJBDemo/CalcBean, java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote] 2017-02-25T20:41:47.100-0400|Info: Glassfish-specific (Non-portable) JNDI names for EJB CalcBean: [com.ejb.test.pckg.ICalcRemote#com.ejb.test.pckg.ICalcRemote, com.ejb.test.pckg.ICalcRemote]

之后,创建应用程序客户端项目。 Main.java 将自动创建。这是我的主要内容:

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;

import com.ejb.test.pckg.ICalcRemote;

public class Main {
    public static void main(String[] args) {

        try {

            Properties props = new Properties();
            props.setProperty("java.naming.factory.initial", "com.sun.enterprise.naming.SerialInitContextFactory");
            props.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
            props.setProperty("java.naming.factory.state", "com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");

            Context ctx = new InitialContext();
            ICalcRemote calc = (ICalcRemote) ctx.lookup("java:global/EJBDemo/CalcBean!com.ejb.test.pckg.ICalcRemote");

            System.out.println(calc.add(5, 43));

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /*
     * (non-Java-doc)
     *
     * @see java.lang.Object#Object()
     */
    public Main() {
        super();
    }

}   

此时,我的项目是这样的:

关键部分来了。从 Glassfish lib 目录添加一个新的外部库。 !!!重要提示:不要只是复制并粘贴到路径! .jar 显然 uses/references other 类 在其他 GF 罐子中。所以只需将它作为外部 jar 而不是 copy/paste 添加到构建路径。

你现在应该准备好了。 运行 Main 作为 Java 应用程序。

这就是我得到的。