使用端口号3700 glassfish Server上的JNDI从客户端访问EJB 3.1

时间:2012-11-18 17:16:58

标签: java java-ee ejb-3.1 openejb

我正在使用netbeans IDE 7.1.2,我创建了一个EJB 3.1无状态Bean,并实现了远程和本地接口,并将它成功部署到glassfish服务器3.2.1 ...这里是代码

__ _ __ _ __ _ __ _ __ _ __ _ ___ CaseInfo.java _ 的__ _ __ _ __ _ __ _ __ _ __

enter code here

package maxx;

import javax.ejb.Stateless;

@Stateless(name="CaseInfo",mappedName="CaseInfoBean")
public class CaseInfo implements CaseInfoRemote, CaseInfoLocal {

@Override
public String MyCaseMessageLocal() {
    return "Hello this is my Local message.";
}

@Override
public String MyCaseMessageRemote() {
    return "Hello this is my Remote message.Please Note that this message is comming from the remote";
}

}

__ _ __ _ __ _ __ _ __ _ __ _ CaseInfoLocal.java _ __ _ __ _ __ _ __ _ __ _ ___ < / p>

package maxx;

import javax.ejb.Local;

@Local
public interface CaseInfoLocal {

String MyCaseMessageLocal();

}

__ _ __ _ __ _ __ _ __ _ __ CaseInfoRemote.java _ __ _ 的__ _ __ _ __ _ __ _ __

package maxx;

import javax.ejb.Remote;

@Remote
public interface CaseInfoRemote {

String MyCaseMessageRemote();

}


现在我已成功从本地访问它并在我创建之后 新项目&gt; Java EE&gt;应用程序客户端项目,这是一个桌面应用程序,我已成功访问该bean,通过将bean模块的jar文件添加到其库作为客户端Stub这里是一些代码

__ _ __ _ __ _ __ _ __ _ __ _ ___ Main.java _ 的__ _ __ _ __ _ __ _ __ _ __ _ __

package applicationclient;

import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import maxx.CaseInfoRemote;

public class Main {


public static void main(String arg[]) {

    try {
Properties prop = new Properties();
prop.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                          "com.sun.enterprise.naming.SerialInitContextFactory");
prop.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
prop.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); 

        InitialContext ic = new InitialContext(prop);
        Object ref = ic.lookup("CaseInfoBean");
        CaseInfoRemote home = (CaseInfoRemote)PortableRemoteObject.narrow(ref,
                CaseInfoRemote.class);
        System.out.println(home.MyCaseMessageRemote());

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

但是当我使用这个客户端访问代码时,请转到New Project&gt; java&gt; java Application并在此处编写相同的代码,以便显示该问题

__ _ __ _ __ _ __ _ __ _ __ _ ____ ERROR _ __ < EM> _ __ _ __ _ __ _ __ _ __ _ __ _ _

run:
javax.naming.NoInitialContextException: Cannot instantiate class: com.sun.enterprise.naming.SerialInitContextFactory [Root exception is  java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory]
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:674)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
at javax.naming.InitialContext.init(InitialContext.java:242)
at javax.naming.InitialContext.<init>(InitialContext.java:216)
at testcasebean.TestCaseBean.main(TestCaseBean.java:30)
Caused by: java.lang.ClassNotFoundException: com.sun.enterprise.naming.SerialInitContextFactory
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:63)
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:671)
    ... 4 more
BUILD SUCCESSFUL (total time: 0 seconds)

如果我正在移除属性代码行,那么就是给出了这种类型的错误

运行: javax.naming.NoInitialContextException:需要在环境或系统属性中指定类名,或者在applet参数中或在应用程序资源文件中指定类名:

java.naming.factory.initial
    at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:662)
    at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:307)
    at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:344)
    at javax.naming.InitialContext.lookup(InitialContext.java:411)
    at testcasebean.TestCaseBean.main(TestCaseBean.java:27)
BUILD SUCCESSFUL (total time: 0 seconds)

请告诉我如何通过使用简单的Java桌面应用程序来访问这个bean,因为使用Java EE应用程序客户端没有问题但是相同的代码会出现这些错误

1 个答案:

答案 0 :(得分:1)

您可能没有按照说明如何从独立 Java客户端访问部署在GlassFish服务器上的远程EJB。这并非完全无足轻重。

这应该回答你的问题。一般来说,我怀疑你缺少的是类路径中的gf-client.jar文件。按照说明告诉我们:

http://glassfish.java.net/javaee5/ejb/EJB_FAQ.html#StandaloneRemoteEJB