glassfish 4 EJB 3独立客户端jndi查找

时间:2014-05-01 06:35:23

标签: client jndi glassfish-4 ejb-3.2

我在调用远程ejb时遇到问题。我已经成功部署了远程EJB:

public interface IHelloWordlHome extends EJBHome {
    mybeans.IHelloWordl create() throws RemoteException, javax.ejb.CreateException;
}

public interface IHelloWordl extends javax.ejb.EJBObject {
    public String hello(String name) throws RemoteException;
}

@javax.ejb.Stateless(name = "HelloWordlEJB")
public class HelloWordlBean implements Serializable {
    public HelloWordlBean() {
    }

    public String hello(String name) {
        return "asd" + name;
    }

    public void ejbCreate() throws CreateException {
    }
}

ejb-jar.xml中:

<enterprise-beans>
    <session>
        <ejb-name>HelloWordlEJB</ejb-name>
        <home>mybeans.IHelloWordlHome</home>
        <remote>mybeans.IHelloWordl</remote>
        <ejb-class>mybeans.HelloWordlBean</ejb-class>
        <session-type>Stateless</session-type>
        <transaction-type>Container</transaction-type>
    </session>
</enterprise-beans>

现在我正在尝试运行独立客户端。这意味着它完全不同的应用程序现在在与服务器(localhost)相同的机器上运行,但稍后它将在不同的机器上运行。由于glassfish描述(没有链接atm)说我使用了没有参数的InitialContext,在服务器日志中我找到了我的bean的JNDI名称(&#34; java:global / ear_ear_exploded / ejb / HelloWordlEJB!mybeans.HelloWordlBean&#34;并试图查找它。我使用gl-client.jar lib,我在classpath上有它。请注意,我没有复制.jar,我在glassfish安装文件夹中使用.jar(我知道这可能是问题因为它链接其他.jars)我复制了(ctrl + c&amp; ctrl + v)bean接口(IHelloWordl) )从服务器到客户端。

客户代码:

public static void main(String[] args) throws NamingException, RemoteException {
        IHelloWordl foo = (IHelloWordl) new InitialContext().lookup("java:global/ear_ear_exploded/ejb/HelloWordlEJB!mybeans.HelloWordlBean");
        foo.hello("Martin");
}

这是我运行的IDE:

P:\Java\jdk1.8.0\bin\java -Didea.launcher.port=7534 "-Didea.launcher.bin.path=P:\IntelliJ IDEA 13.1.1\bin" -Dfile.encoding=UTF-8 -classpath "P:\Java\jdk1.8.0\jre\lib\charsets.jar;P:\Java\jdk1.8.0\jre\lib\deploy.jar;P:\Java\jdk1.8.0\jre\lib\javaws.jar;P:\Java\jdk1.8.0\jre\lib\jce.jar;P:\Java\jdk1.8.0\jre\lib\jfr.jar;P:\Java\jdk1.8.0\jre\lib\jfxswt.jar;P:\Java\jdk1.8.0\jre\lib\jsse.jar;P:\Java\jdk1.8.0\jre\lib\management-agent.jar;P:\Java\jdk1.8.0\jre\lib\plugin.jar;P:\Java\jdk1.8.0\jre\lib\resources.jar;P:\Java\jdk1.8.0\jre\lib\rt.jar;P:\Java\jdk1.8.0\jre\lib\ext\access-bridge.jar;P:\Java\jdk1.8.0\jre\lib\ext\cldrdata.jar;P:\Java\jdk1.8.0\jre\lib\ext\dnsns.jar;P:\Java\jdk1.8.0\jre\lib\ext\jaccess.jar;P:\Java\jdk1.8.0\jre\lib\ext\jfxrt.jar;P:\Java\jdk1.8.0\jre\lib\ext\localedata.jar;P:\Java\jdk1.8.0\jre\lib\ext\nashorn.jar;P:\Java\jdk1.8.0\jre\lib\ext\sunec.jar;P:\Java\jdk1.8.0\jre\lib\ext\sunjce_provider.jar;P:\Java\jdk1.8.0\jre\lib\ext\sunmscapi.jar;P:\Java\jdk1.8.0\jre\lib\ext\sunpkcs11.jar;P:\Java\jdk1.8.0\jre\lib\ext\zipfs.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\out\production\project-ejbclient;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.annotation.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.ejb.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.jms.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.transaction.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.persistence.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.servlet.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.resource.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.servlet.jsp.jar;D:\projects\self\dt-reservation-system-for-doctors\project-ejbclient\lib\javax.servlet.jsp.jstl.jar;P:\glassfish4\glassfish\lib\gf-client.jar;P:\glassfish4\glassfish\lib\appserv-rt.jar;P:\IntelliJ IDEA 13.1.1\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMain mybeans.Main

当我运行客户端时,我遇到了异常情况,我无法理解并且没有在线帮助:

Exception in thread "main" javax.naming.CommunicationException: Communication exception for SerialContext[myEnv={java.naming.factory.initial=com.sun.enterprise.naming.impl.SerialInitContextFactory, java.naming.factory.url.pkgs=com.sun.enterprise.naming, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl} [Root exception is java.rmi.MarshalException: CORBA BAD_PARAM 1398079494 Maybe; nested exception is: 
    java.io.NotSerializableException: ----------BEGIN server-side stack trace----------
org.omg.CORBA.BAD_PARAM: WARNING: 00100006: Class mybeans.__EJB31_Generated__HelloWordlBean__Intf____Bean__ is not Serializable  vmcid: SUN  minor code: 6 completed: Maybe
    at com.sun.proxy.$Proxy153.notSerializable(Unknown Source)
    at com.sun.corba.ee.impl.misc.ORBUtility.throwNotSerializableForCorba(ORBUtility.java:783)
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.writeAny(Util.java:360)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$10.write(DynamicMethodMarshallerImpl.java:306)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.writeResult(DynamicMethodMarshallerImpl.java:488)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:177)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)

----------END server-side stack trace----------]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:513)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:438)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at mybeans.Main.main(Main.java:10)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
Caused by: java.rmi.MarshalException: CORBA BAD_PARAM 1398079494 Maybe; nested exception is: 
    java.io.NotSerializableException: ----------BEGIN server-side stack trace----------
org.omg.CORBA.BAD_PARAM: WARNING: 00100006: Class mybeans.__EJB31_Generated__HelloWordlBean__Intf____Bean__ is not Serializable  vmcid: SUN  minor code: 6 completed: Maybe
    at com.sun.proxy.$Proxy153.notSerializable(Unknown Source)
    at com.sun.corba.ee.impl.misc.ORBUtility.throwNotSerializableForCorba(ORBUtility.java:783)
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.writeAny(Util.java:360)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$10.write(DynamicMethodMarshallerImpl.java:306)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.writeResult(DynamicMethodMarshallerImpl.java:488)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:177)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)

----------END server-side stack trace----------
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:300)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:211)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.invoke(StubInvocationHandlerImpl.java:150)
    at com.sun.corba.ee.impl.presentation.rmi.codegen.CodegenStubBase.invoke(CodegenStubBase.java:226)
    at com.sun.enterprise.naming.impl._SerialContextProvider_DynamicStub.lookup(com/sun/enterprise/naming/impl/_SerialContextProvider_DynamicStub.java)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:478)
    ... 8 more
Caused by: java.io.NotSerializableException: ----------BEGIN server-side stack trace----------
org.omg.CORBA.BAD_PARAM: WARNING: 00100006: Class mybeans.__EJB31_Generated__HelloWordlBean__Intf____Bean__ is not Serializable  vmcid: SUN  minor code: 6 completed: Maybe
    at com.sun.proxy.$Proxy153.notSerializable(Unknown Source)
    at com.sun.corba.ee.impl.misc.ORBUtility.throwNotSerializableForCorba(ORBUtility.java:783)
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.writeAny(Util.java:360)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$10.write(DynamicMethodMarshallerImpl.java:306)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.writeResult(DynamicMethodMarshallerImpl.java:488)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:177)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)

----------END server-side stack trace----------
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.mapSystemException(Util.java:292)
    ... 13 more
Caused by: org.omg.CORBA.BAD_PARAM: ----------BEGIN server-side stack trace----------
org.omg.CORBA.BAD_PARAM: WARNING: 00100006: Class mybeans.__EJB31_Generated__HelloWordlBean__Intf____Bean__ is not Serializable  vmcid: SUN  minor code: 6 completed: Maybe
    at com.sun.proxy.$Proxy153.notSerializable(Unknown Source)
    at com.sun.corba.ee.impl.misc.ORBUtility.throwNotSerializableForCorba(ORBUtility.java:783)
    at com.sun.corba.ee.impl.javax.rmi.CORBA.Util.writeAny(Util.java:360)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl$10.write(DynamicMethodMarshallerImpl.java:306)
    at com.sun.corba.ee.impl.presentation.rmi.DynamicMethodMarshallerImpl.writeResult(DynamicMethodMarshallerImpl.java:488)
    at com.sun.corba.ee.impl.presentation.rmi.ReflectiveTie._invoke(ReflectiveTie.java:177)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatchToServant(ServerRequestDispatcherImpl.java:528)
    at com.sun.corba.ee.impl.protocol.ServerRequestDispatcherImpl.dispatch(ServerRequestDispatcherImpl.java:199)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequestRequest(MessageMediatorImpl.java:1549)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:1425)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleInput(MessageMediatorImpl.java:930)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.RequestMessage_1_2.callback(RequestMessage_1_2.java:213)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.handleRequest(MessageMediatorImpl.java:694)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.dispatch(MessageMediatorImpl.java:496)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.doWork(MessageMediatorImpl.java:2222)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.performWork(ThreadPoolImpl.java:497)
    at com.sun.corba.ee.impl.threadpool.ThreadPoolImpl$WorkerThread.run(ThreadPoolImpl.java:540)

----------END server-side stack trace----------  vmcid: SUN  minor code: 6 completed: Maybe
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:408)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.MessageBase.getSystemException(MessageBase.java:813)
    at com.sun.corba.ee.impl.protocol.giopmsgheaders.ReplyMessage_1_2.getSystemException(ReplyMessage_1_2.java:131)
    at com.sun.corba.ee.impl.protocol.MessageMediatorImpl.getSystemExceptionReply(MessageMediatorImpl.java:594)
    at com.sun.corba.ee.impl.protocol.ClientRequestDispatcherImpl.processResponse(ClientRequestDispatcherImpl.java:519)
    at com.sun.corba.ee.impl.protocol.ClientRequestDispatcherImpl.marshalingComplete(ClientRequestDispatcherImpl.java:393)
    at com.sun.corba.ee.impl.protocol.ClientDelegateImpl.invoke(ClientDelegateImpl.java:272)
    at com.sun.corba.ee.impl.presentation.rmi.StubInvocationHandlerImpl.privateInvoke(StubInvocationHandlerImpl.java:198)
    ... 12 more

我被解雇了:/任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我不确定你从哪里得到这个,但看起来你混淆了什么。在 EJB 3 中,您不必扩展EJBHomeEJBObject。您不需要HomeInterface

你应该这样做:

import javax.ejb.Remote;

@Remote
public interface HelloWorldRemote {
    public String hello(String name);
}

@javax.ejb.Stateless(name = "HelloWorldEJB")
public class HelloWorldBean implements HelloWorldRemote {

    public String hello(String name) {
        return "asd" + name;
    }
}

PS:HelloWorldHelloWordl)中有拼写错误。

您不需要ejb-jar.xml中的任何声明。

客户端代码看起来应该类似于:

InitialContext con = new InitialContext();
HelloWorldBean foo = (HelloWorldBean) con.lookup("java:global/ear_ear_exploded/HelloWorldEJB");

另见:

答案 1 :(得分:0)

您的类必须是可序列化的,这意味着您的EJB需要实现Serializable