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
Create the GlassFish Environment
Open the environment wizard
Sign in to the platform dashboard and click Create environment.
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.

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

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);
}
}
}
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
Upload the EAR archive
Open Deployment Manager and upload the generated .ear file.

Deploy to GlassFish
Deploy the uploaded archive to the GlassFish environment created earlier.

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.

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.ejbpackage names shown in the source documentation.
