Remote Access to EJB on GlassFish

Remote Access to EJB on GlassFish

Enterprise Java Beans support local and remote client views. A local client view is suitable when the client and bean run in the same JVM, while a remote client view is required when the client connects from another JVM or from a distributed environment.

Local and Remote EJB Views

Local client viewUse when the client and EJB run inside the same JVM. Calls are made directly without remote method invocation.
Remote client viewUse when the client runs in another JVM or must access the bean through a distributed environment.
Parameters by valueRemote interfaces are suitable when parameters are transferred by value between the client application and the bean.

Create the GlassFish Environment

1

Open the environment wizard

Sign in to the platform dashboard and click Create environment.

2

Select GlassFish and enable Public IP

Select GlassFish as the application server, set the required Cloudlet limits, enable a Public IP, enter the environment name, and click Create.

Create a GlassFish environment with Public IP
Create a GlassFish environment with the required resource limits and Public IP.
3

Copy the GlassFish Public IP

After the environment is created, open the additional information for the GlassFish node and copy its Public IP address.

GlassFish Public IP address
Use the GlassFish Public IP in the remote EJB client configuration.

Create the EJB Application

Create a new project directory for the EJB module and the remote client files.

Create the Stateless Session Bean

package com;

import javax.ejb.Stateless;

@Stateless
public class EJBTest implements EJBTestRemote {

    @Override
    public String getName(String name) {
        return "name is: " + name;
    }
}

Create the Remote EJB Interface

package com;

import javax.ejb.Remote;

@Remote
public interface EJBTestRemote {
    public String getName(String name);
}

Build the EJB module and package it as an .ear archive.

Create the Remote Client

The client creates an initial naming context, connects to the GlassFish server through its Public IP, looks up the remote interface, and invokes the getName() method.

package ejbclient;

import com.EJBTestRemote;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class Main {

    private static InitialContext ic;

    public void loadProperties(String h, String p) {
        try {
            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", h);
            props.setProperty("org.omg.CORBA.ORBInitialPort", p);

            ic = new InitialContext(props);
        } catch (NamingException ex) {
            Logger.getLogger(Main.class.getName())
                  .log(Level.SEVERE, null, ex);
        }
    }

    public static void main(String[] args) {
        try {
            new Main().loadProperties(
                "{GlassFish_Public_IP}",
                "23700"
            );

            EJBTestRemote bean =
                (EJBTestRemote) ic.lookup("com.EJBTestRemote");

            System.out.println(bean.getName("Jelastic"));
        } catch (NamingException ex) {
            Logger.getLogger(Main.class.getName())
                  .log(Level.SEVERE, null, ex);
        }
    }
}
i

GlassFish cluster port mapping

The documented client uses port 23700. The additional leading digits are used because the platform works with gfcluster.

Deploy and Test the Application

1

Upload the EAR archive

Open Deployment Manager and upload the generated .ear file.

Upload the EJB EAR archive
Upload the EAR package through Deployment Manager.
2

Deploy to GlassFish

Deploy the uploaded archive to the GlassFish environment created earlier.

Deploy the EAR archive to GlassFish
Select the GlassFish environment as the deployment target.
3

Run the client and verify the result

Run the remote client application. It should connect through the GlassFish Public IP, invoke the remote EJB method, and print the returned value.

Remote EJB access result
The client receives the value returned by the remote EJB method.

Expected Result

The remote Java client connects to GlassFish through its Public IP, resolves the remote EJB interface through JNDI, calls the session bean method, and receives the server response.

Important Notes

  • Use a local interface when the client and bean run inside the same JVM.
  • Use a remote interface when the client connects from another JVM.
  • Replace {GlassFish_Public_IP} with the real Public IP of the GlassFish node.
  • Ensure that the required EJB client libraries are available in the remote client project.
  • Open the required network port only to trusted sources.
  • The example uses the older javax.ejb package names shown in the source documentation.

Common Issues and Solutions

NamingException occursCheck the Public IP, port, JNDI factory classes, remote interface name, and deployed application status.
Client cannot connectVerify the GlassFish node status, Public IP, port access, firewall rules, and network connectivity.
EJB lookup failsConfirm the remote interface package and JNDI lookup name used by the deployed application.
Method call returns an errorReview the GlassFish server logs and confirm that the client and server use compatible interface classes.
Application deploys but client libraries are missingAdd the required GlassFish client libraries to the remote client classpath.