我应该如何命名EJB以正确查找它?

时间:2014-12-28 23:09:12

标签: java java-ee glassfish ejb jndi

我有一个简单的问题,我有一个在Glassfish 4中运行的无状态EJB bean。我有一个客户端,我想查找这个ejb,而我根本无法做出正确的名称。我应该如何正确地命名这些?

我刚收到javax.naming.NamingException,但我不知道如何正确行事。 我遵循java:global/[ear-name]/[jar-name]/[ejb-name]![fully-qualified-interface-name]惯例。

这是客户:

...
public class Main {

    public static void main(String[] args) {
        Calculator calculator;
        Context ctx = null;

        try {
            Properties environment = new Properties();
            environment.setProperty("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
            environment.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

            // Find the EJB with a JNDI lookup
            ctx = new InitialContext(environment);
            calculator = (Calculator)ctx.lookup(
                "java:global/calculator-application/calculator-ejb/calcBean!eak.Calculator"
            );
        } catch(NamingException ex) {
            ex.printStackTrace();
            return;
        }           
        ...
    }
}

以下是我的EJB组件的注释:

@Stateless(name="calcBean", mappedName="calc")
@Remote(Calculator.class)
public class CalculatorBean implements Calculator {
...

我运行了我的Glassfish服务器中的jndi名称:

C:\javaee\glassfish4\glassfish\bin>asadmin.bat list-jndi-entries
UserTransaction: com.sun.enterprise.transaction.startup.TransactionLifecycleServ
ice$2
ejb: com.sun.enterprise.naming.impl.TransientContext
java:global: com.sun.enterprise.naming.impl.TransientContext
calc__3_x_Internal_RemoteBusinessHome__: javax.naming.Reference
calc: javax.naming.Reference
jdbc: com.sun.enterprise.naming.impl.TransientContext
concurrent: com.sun.enterprise.naming.impl.TransientContext
com.sun.enterprise.container.common.spi.util.InjectionManager: com.sun.enterpris
e.container.common.impl.util.InjectionManagerImpl
jms: com.sun.enterprise.naming.impl.TransientContext
calc#eak.Calculator: javax.naming.Reference
Command list-jndi-entries executed successfully.

1 个答案:

答案 0 :(得分:2)

你的配置看起来对我有效。我测试了你的例子,它工作得很好。请确保gf-client.jar中的glassfish4/glassfish/lib位于您的类路径中。还请确认您的应用程序,模块和bean名称是您在Glassfish控制台中看到的。在我的例子中,JNDI查找字符串java:global/ear-1.0-SNAPSHOT/my-ejb-jar-1.0-SNAPSHOT/calcBean!Echo运行良好。

enter image description here