如何从Java Se连接到在Glassfish上部署的远程Ejb?

问题描述 投票:2回答:1

[经过两天的谷歌搜索和测试分配后,我在此处发布。我看到了类似的问题,但是他们的解决方案无法满足我的需求。我写了一个带有远程接口的简单Ejb​​,想从Java Se程序中调用它。我将ejb部署为Eclipse ejb模块。这是我的代码的一部分。

@Remote
public interface Greeter extends Serializable {
public void greet(String name) throws NamingException;
}

@Stateless
public class GreeterBean implements Greeter {
    @Override
    public void greet(String name) throws NamingException {
        System.out.println("hello"+name);
    }

}

这是我的Java se程序

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");

try {
    InitialContext  initialContext = new InitialContext(props);
    Greeter greeter = (Greeter) initialContext.lookup("java:global/EjbServer/GreeterBean");
    greeter.greet("hamid");
...

我在客户端应用程序中也有Greeter Interface的副本

在客户端,我正在使用glassfish 5客户端

<dependency>
 <groupId>org.glassfish.main.appclient</groupId>
 <artifactId>gf-client</artifactId>
 <version>5.1.0</version>
</dependency>   

我正在使用Java EE 8和glassfish 5.0.1,Ejb 3.2和Eclipse IDE 2019这是我的完整堆栈跟踪。

javax.naming.NamingException: Lookup failed for 'java:global/EjbServer/GreeterBean' in SerialContext[myEnv={org.omg.CORBA.ORBInitialPort=3700, java.naming.factory.initial=com.sun.enterprise.naming.SerialInitContextFactory, org.omg.CORBA.ORBInitialHost=localhost, java.naming.factory.state=com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl, java.naming.factory.url.pkgs=com.sun.enterprise.naming} [Root exception is javax.naming.NamingException: ejb ref resolution error for remote business interfacetest.Greeter [Root exception is java.lang.ClassNotFoundException: test.Greeter]]
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:467)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:414)
    at javax.naming.InitialContext.lookup(InitialContext.java:417)
    at com.client.ejb.App.main(App.java:45)
Caused by: javax.naming.NamingException: ejb ref resolution error for remote business interfacetest.Greeter [Root exception is java.lang.ClassNotFoundException: test.Greeter]
    at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:409)
    at com.sun.ejb.containers.RemoteBusinessObjectFactory.getObjectInstance(RemoteBusinessObjectFactory.java:51)
    at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
    at com.sun.enterprise.naming.impl.SerialContext.getObjectInstance(SerialContext.java:503)
    at com.sun.enterprise.naming.impl.SerialContext.lookup(SerialContext.java:463)
    ... 3 more
Caused by: java.lang.ClassNotFoundException: test.Greeter
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at com.sun.ejb.EJBUtils.getBusinessIntfClassLoader(EJBUtils.java:663)
    at com.sun.ejb.EJBUtils.loadGeneratedRemoteBusinessClasses(EJBUtils.java:439)
    at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:389)

... 7 more

任何帮助将不胜感激。预先感谢。

java jakarta-ee glassfish ejb
1个回答
1
投票

首先,在eclipse File上创建企业应用程序项目>新建> Enterprise Application Project并输入名称。例如EarEjbProject。保留默认设置,然后单击完成。然后,为示例EjbProjectModule创建一个Ejb项目。输入项目名称,然后选中将项目添加到Ear选项,然后选择之前构建的ear项目。创建您的远程Ejb界面。

package com.hamid.test;

import javax.ejb.Remote;

@Remote
public interface Greeting {
public String greet(String name);
}

然后,创建您的远程Bean实现。

package com.hamid.test;

import javax.ejb.EJB;
import javax.ejb.Remote;
import javax.ejb.Stateless;
@Remote
@Stateless
public class GreetingBean implements Greeting  {
    @Override
    public String greet(String name) {
        String greet ="hello"+name;
        System.out.println(greet);
        return greet;
    }

}

创建应用程序模块项目File> new> Application Module Project。在EjbServerModule项目上创建与您的远程接口相同的程序包,并从服务器项目中复制该接口。将as-install / lib / gf-client.jar文件复制到客户端计算机,并将其包含在客户端的类路径中。gf-client.jar文件在其MANIFEST.MF文件中引用了GlassFish Server JAR文件。如果客户端计算机上没有安装GlassFish Server,则还必须将as-install / modules目录复制到客户端计算机,并相对于as-install / lib / gf-client.jar文件维护其目录结构。或者,您可以使用package-appclient脚本。“按原样”是指您的glassfish目录。现在创建您的主类。

package com.ejb.client.example;
import java.util.List;
import java.util.Properties;

import javax.ejb.EJB;
import javax.naming.InitialContext;

import com.hamid.test.Greeting;

public class Main {
    public static void main(String[] args) {
        System.out.println("test");
        try {
            String host="localhost";// if you run your client and server sample on same machine
            String port ="3700";//default
// to obtain port use asadmin get "configs.config.server-config.iiop-service.iiop-listener.orb-listener-1.*" 
            Properties prop = new Properties();
            prop.put("org.omg.CORBA.ORBInitialHost",host);
            prop.put("org.omg.CORBA.ORBInitialPort",port);
            InitialContext context =new InitialContext(prop);
            Greeting greeting =(Greeting) context.doLookup("java:global/EarEjbProject/EjbServerModule/GreetingBean");
            String text=greeting.greet("hamid");
            System.out.println(text);
            System.out.println("exit");
            context.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}

您可以下载this example

© www.soinside.com 2019 - 2024. All rights reserved.