首页 > 解决方案 > 无法从客户端(Junit)访问远程 EJB。LookUp 失败

问题描述

我是 EJB 和 Geronimo 服务器的新手。我想在 Geronimo Server 上为 EJB 创建 Junit POC。但是在查找无状态会话 bean 时,它给了我这个错误。

请在下面找到异常

javax.naming.NamingException: Cannot lookup '/MyStatelessSessionBeanRemote'. [Root exception is java.rmi.RemoteException: Cannot read the response from the server. The class for an object being returned is not located in this system:; nested exception is:
java.lang.ClassNotFoundException: sampleear.RemoteBusinessInterface]
at org.apache.openejb.client.JNDIContext.lookup(JNDIContext.java:240)
at javax.naming.InitialContext.lookup(InitialContext.java:363)
at test.SampleTest.setUp(SampleTest.java:44)
at junit.framework.TestSuite.runTest(TestSuite.java:208)

我正在使用 Geronimo 服务器 2.1,并且我还在 geronimo 服务器中部署了 EAR 文件。EAR 文件已成功部署,显示在 Geronimo 服务器上。

请在下面找到日志

2019-07-01 14:39:46,221 INFO  [config] Configuring Service(id=Default BMP Container, type=Container, provider-id=Default BMP Container)
2019-07-01 14:39:46,222 INFO  [config] Configuring Service(id=Default CMP Container, type=Container, provider-id=Default CMP Container)
2019-07-01 14:39:46,222 INFO  [config] Configuring app: sampleear/sample-ejb/1.0/ejb
2019-07-01 14:39:46,269 INFO  [OpenEJB] Auto-deploying ejb MyStatelessSessionBean: EjbDeployment(deployment-id=sample-ejb/MyStatelessSessionBean)
2019-07-01 14:39:46,277 INFO  [config] Loaded Module: sampleear/sample-ejb/1.0/ejb
2019-07-01 14:39:46,555 INFO  [startup] Assembling app: /tmp/geronimo-deployer1011310413181662917.tmpdir/SampleEJB.jar
2019-07-01 14:39:46,557 ERROR [startup] Jndi(name=MyStatelessSessionBeanRemote) cannot be bound to Ejb(deployment-id=sample-ejb/MyStatelessSessionBean).  Name already taken by Ejb(deployment-id=SampleEJB.jar/MyStatelessSessionBean)
2019-07-01 14:39:46,557 INFO  [startup] Created Ejb(deployment-id=sample-ejb/MyStatelessSessionBean, ejb-name=MyStatelessSessionBean, container=Default Stateless Container)
2019-07-01 14:39:46,558 INFO  [startup] Deployed Application(path=/tmp/geronimo-deployer1011310413181662917.tmpdir/SampleEJB.jar)
2019-07-01 14:39:46,673 INFO  [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
2019-07-01 14:39:46,676 INFO  [XSRFHandler] Updated HTML content with XSRF JavaScript for requestURI=/console/portal//Applications/Deploy%20New

Junit - 客户

public class SampleTest extends TestCase {

  protected MyStatelessSessionBean remoteBusinessIntf;


  public void setUp() throws Exception {
    super.setUp();
    Properties prop = new Properties();
    prop.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
    prop.put("java.naming.provider.url", "ejbd://127.0.0.1:4201");
//    prop.put("openejb.authentication.realmName","geronimo-admin");
//    prop.put("java.naming.security.principal","system");
//    prop.put("java.naming.security.credentials","manager");
    Context context = new InitialContext(prop);
    remoteBusinessIntf = (MyStatelessSessionBean) context.lookup("MyStatelessSessionBeanRemote"); // Giving error on this.

}

public void testSaveBook() {
        System.out.println("ok");
        remoteBusinessIntf.sayHello("EJB");
    }

}

Stateless Session Bean

package sampleear;
import javax.ejb.Remote;
import javax.ejb.Stateless;

@Stateless
public class MyStatelessSessionBean implements RemoteBusinessInterface {
    public String sayHello(String name) {
        return getClass().getName() + " says hello to "+ name + ".";
    }
}

远程接口

package sampleear;

import javax.ejb.Remote;

@Remote
public interface RemoteBusinessInterface {
    public String sayHello(String name);
}

部署的 EJB 应用程序

 Component Name      State  Commands
 org.apache.geronimo.configs/mejb/2.1.4/car      running     Stop    Restart     Uninstall 
 org.apache.geronimo.plugins/agent/2.1.4/car     running     Stop    Restart     Uninstall 
 sampleear/sample-ejb/1.0/ejb    running     Stop    Restart     Uninstall 

Geronimo v2.1.4

未修改端口。使用默认端口。


Geronimo 港口详情

Startup completed in 31.119s seconds
  Listening on Ports:
    1050 0.0.0.0 CORBA Naming Service
    1099 0.0.0.0 RMI Naming
    1527 0.0.0.0 Derby Connector
    2001 0.0.0.0 OpenEJB ORB Adapter
    4201 0.0.0.0 OpenEJB Daemon
    6882 0.0.0.0 OpenEJB ORB Adapter
    8009 0.0.0.0 Tomcat Connector AJP AJP
    8080 0.0.0.0 Tomcat Connector HTTP BIO HTTP
    8443 0.0.0.0 Tomcat Connector HTTPS BIO HTTPS
    9999 0.0.0.0 JMX Remoting Connector
   61613 0.0.0.0 ActiveMQ Transport Connector
   61616 0.0.0.0 ActiveMQ Transport Connector

我再次创建了新的 Geronimo 服务器并部署了 EJB 应用程序。这次没有出现任何部署错误。请在下面找到日志。

2019-07-02 10:28:18,242 INFO  [config] Configuring Service(id=Default Stateful Container, type=Container, provider-id=Default Stateful Container)
2019-07-02 10:28:18,242 INFO  [config] Configuring Service(id=Default BMP Container, type=Container, provider-id=Default BMP Container)
2019-07-02 10:28:18,242 INFO  [config] Configuring Service(id=Default CMP Container, type=Container, provider-id=Default CMP Container)
2019-07-02 10:28:18,243 INFO  [config] Configuring app: sampleear/sample-ejb/1.0/ejb
2019-07-02 10:28:18,275 INFO  [OpenEJB] Auto-deploying ejb MyStatelessSessionBean: EjbDeployment(deployment-id=sample-ejb/MyStatelessSessionBean)
2019-07-02 10:28:18,281 INFO  [config] Loaded Module: sampleear/sample-ejb/1.0/ejb
2019-07-02 10:28:18,555 INFO  [startup] Assembling app: /tmp/geronimo-deployer7186189966556040174.tmpdir/SampleEJB.jar
2019-07-02 10:28:18,558 INFO  [startup] Jndi(name=MyStatelessSessionBeanRemote) --> Ejb(deployment-id=sample-ejb/MyStatelessSessionBean)
2019-07-02 10:28:18,558 INFO  [startup] Created Ejb(deployment-id=sample-ejb/MyStatelessSessionBean, ejb-name=MyStatelessSessionBean, container=Default Stateless Container)
2019-07-02 10:28:18,558 INFO  [startup] Deployed Application(path=/tmp/geronimo-deployer7186189966556040174.tmpdir/SampleEJB.jar)
2019-07-02 10:28:18,669 INFO  [SupportedModesServiceImpl] Portlet mode 'edit' not found for portletId: '/plugin.Deployment!227983155|0'
2019-07-02 10:28:18,673 INFO  [XSRFHandler] Updated HTML content with XSRF JavaScript for requestURI=/console/portal//Applications/Deploy%20New

标签: javajunitejbgeronimo

解决方案


推荐阅读