简体   繁体   中英

How to connect to remote Ejb that deployed on glassfish from java Se?

After two days googling and testing allot, I posting here. I saw similar questions but their solutions don't feet my needs.
I wrote a simple Ejb with remote interface and want to call it from java Se program.
I deployed my ejb as Eclipse ejb module. This is a part of my code.

@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);
    }

}

And This is my Java se program

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

I also have a copy of Greeter Interface in client app

In client Side, I'm using glassfish 5 client

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

I'm using java EE 8 and glassfish 5.0.1 and Ejb 3.2 and Eclipse ide 2019 and This is my full stack trace.

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

Any help would be appreciated. Thanks in advance.

First, create an enterprise application project on eclipse File>new>Enterprise Application Project and enter a name. for Example, EarEjbProject. Leave default settings and hit finish. Then, create an Ejb project for Example, EjbProjectModule. Enter your project name and check add project to an Ear option and select your ear project that you built before. create youre remote Ejb interface.

package com.hamid.test;

import javax.ejb.Remote;

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

And Then, create your remote Bean implementation.

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;
    }

}

Create an Application Module project File>new>Application Module Project. create a package as same as your remote interface on EjbServerModule project andcoppy that interface from server project. Copy the as-install/lib/gf-client.jar file to the client machine and include it in the classpath on the client side. The gf-client.jar file references GlassFish Server JAR files in its MANIFEST.MF file. If there is no GlassFish Server installation on the client machine, you must also copy the as-install/modules directory to the client machine and maintain its directory structure relative to the as-install/lib/gf-client.jar file. Or you can use the package-appclient script. "as-instal" means your glassfish directory. now create your main class.

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();
        }
    }
}

You can download this example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM