Remote Access to Enterprise Java Beans on GlassFish Server

Enterprise Java Beans (EJB) specification includes remote client and local client views. In some cases, your Java application may need to have session and entity beans with local home and component interfaces and in other cases with remote ones.

Let’s clarify which interface to choose and when to choose it.

A local client view is good for cases when you are ensured that other EJBs and clients will only request your bean within a single JVM. Such access will be given by direct method calls, instead of RMI (remote method invocation). It also makes sense to use local clients for beans associated with each other.

Your EJB needs a remote client view when you are going to use this bean in distributed environments, i.e. when your client is located on the other Java Virtual Machine. RMI will handle all the methods’ calls from the remote home and/or component interface. We also recommend to use remote access client view for passed by value parameters, between the bean and the client application.

Of course, the final decision depends on your application’s qualities.

Today we’ll focus on how an EJB remote client works with Java Bean deployed to Jelastic ZEROCodeChange PaaS.  This tutorial has been prepared for those readers who have some knowledge of Enterprise Java Beans technology.

Create environment

1. As usual we start with creating an environment for our application. Log into your Jelastic account and click Create environment.

2. Select GlassFish server and specify the resource amount your Java EE application needs. Then switch on Public IP for the server for further connection from client application to EJB.

Create GlassFish Server Environment

Wait a few minutes to see your environment, available from the dashboard.

3. To find your Public IP, expand your environment list and click on the additional button for GlassFish.

GlassFish Server Public IP

Create application

1. Firstly, create the directory where you plan to place your Enterprise Java Beans and client application files.

2. Create your Session Bean, which will be invoked by a remote client application to access an application that is deployed on the server. Session Bean performs work for the client by executing business tasks inside the server.

package com;

import javax.ejb.Stateless;

@Stateless
public class EJBTest implements EJBTestRemote {

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

3. After that you have to create the EJB interface, it’s required for the beans which are accessed by remote clients. Here’s the example:

package com;

import javax.ejb.Remote;

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

4. Compile your EJB module and package it into the .ear file for further deployment.
5. Create remote client application to access your bean. Here’s an example, which connects to EJB remotely via GlassFish Public IP and calls method getName(), which returns data to the client:

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;
    //private String host="",port="";

    public void loadProperties(String h, String p) {
        try {
            Properties props = new Properties();

            System.out.println("h: " + h + " p: " + p);

            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 a[]) {

        try {

            new Main().loadProperties("{GlassFish_Public_IP}", "23700");
            EJBTestRemote etr = (EJBTestRemote) ic.lookup("com.EJBTestRemote");
            System.out.println(etr.getName("Jelastic"));

        } catch (NamingException ex) {
        }
    }
}

Note: Jelastic works with gfcluster only, so all of the port numbers include an additional number 2 at the beginning.

Our test EAR package can be downloaded here.

Application deployment

1. Go back to the Jelastic dashboard and upload your .ear to the Deployment manager.

Upload Enterprise Java Beans Application

2. Deploy the archive to the environment you’ve created earlier.

Deploy Enterprise Java Beans Application

3. Once your application is successfully deployed, run your client application to check the results.

Remote Access to Enterprise Java Beans

We hope the above article will provide useful guidelines for you. Of course these are not all of the nuances of remote interfaces usage, they are just the basics. If you need more details just let me know I’ll be happy to share my experience with you.

Leave a Reply