Before it dies, using EJB2 with GlassFish and JBoss Application Server!

It is true that EJB2 is a very old protocol. But still, some people use it and will use it according to their requirements.

I am writing this blog to make your lives easy if you ever come across the need of setting up EJB2 with GlassFish or JBoss.

There are several articles about this, but there is no article which provides you the easiest way to do this.

First of all, let me tell you that you will need to use an IDE for this. I recommend using NetBeans IDE because it is very easy to setup the servers.

So you will first have to download and install NetBeans IDE first.

Setting up GlassFish in NetBeans

Download GlassFish and extract the zip to a place you want to place it.

Go to Tools → Servers → Add Server and choose GlassFish Server in NetBeans IDE as in Figure 1.

Place your GlassFish extraction location and create a GlassFish Server.

Figure 1: Adding a GlassFish Server

Setting up JBoss in NetBeans

Download AS 6.XXX Version of JBoss first. You need to extract the zip and place it somewhere you want to keep it.

Go to Tools → Servers → Add Server and choose JBoss Application Server in NetBeans IDE as in Figure 2.

Figure 2: Adding a JBoss Application Server

Place your JBoss Zip extraction location and create a JBoss server.

When you are providing Instance Properties, use 8080 as the HTTP port and 1099 as the JMX Port as shown in Figure 3.

Figure 3: Configuring JBoss Server

Configuring EJB2 Client Application

Now let’s configure our EJB2 Client application. For this, you need to create a new Java application.

Choose Java → Java Application from New Project Wizard.

Create a new package com.acme.library in the source packages. Create a new Java Interface LibrarySessionBeanRemote with the annotation @Remote

package com.acme.library;

import java.util.List;

import javax.ejb.Remote;

@Remote

public interface LibrarySessionBeanRemote {

String addBook(String bookName);

List getBooks();

}

Now your EJB2 Client is ready!

Configuring EJB2 Modules

First, you need to create an EJB module from New Project Wizard.

Choose JavaEE → EJB Module as in Figure 4.

Figure 4: Creating an EJB Module

Choose GlassFish Server/ JBoss Application Server you have configured before from the list of servers available and finish creating the EJB Module as in Figure 5 and Figure 6.

Figure 5: Creating an EJB Module on GlassFish Server
Figure 6: Creating an EJB Module on JBoss Application Server

Add a package com.acme.library to the source packages directory.

Create a new Session Bean called LibrarySessionBean. Choose the created EJB2 Client application as the Remote interface by ticking the Remote Create Interface part as in Figure 7.

Figure 7: Creating an EJB Module with Remote Interface

Following is the implementation of the LibrarySessionBean. This includes what you need to do with the client interface you have created.

package com.acme.library;



import java.util.ArrayList;

import java.util.List;

import javax.ejb.Stateless;



@Stateless

public class LibrarySessionBean implements LibrarySessionBeanRemote {

List<String> bookShelf;

public LibrarySessionBean(){

bookShelf = new ArrayList();

}

@Override

public String addBook(String bookName) {

bookShelf.add(bookName);

System.out.println("successfull");

return "done";

}

@Override

public List<String> getBooks() {

return bookShelf;

}

}

You need to add client library jar to your Libraries Directory.

For JBoss Application Server, add filejbossall-client.jar to the LibrariesDirectory.

Testing the EJB2 Modules

Let’s test the EJB2 modules that we created for GlassFish and JBoss Application Servers.

You will need to implement two Main Java classes for this testing purpose in your EJB2 modules with following implementations.

Let’s test adding a book to our library.

Test implementation for GlassFish Server

package com.acme.library;



import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;



public class Main {

LibrarySessionBeanRemote librarySessionBean = lookupLibrarySessionBeanRemote();

private String ejbClassPath;

private String ejbInterfaceClassPath;

private String methodName;

private List<Object> paramValues = new ArrayList<>();

private Map<String, Object> paramMatch = new HashMap<>();

public static void main(String[] args) throws NamingException, IllegalArgumentException, InvocationTargetException {

Main clazz = new Main();

LibrarySessionBeanRemote bean = clazz.lookupLibrarySessionBeanRemote();

bean.addBook("Woman");

System.out.println(bean.getBooks());

}

private LibrarySessionBeanRemote lookupLibrarySessionBeanRemote() {

try {

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");

properties.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");

// glassfish default port value will be 3700,

properties.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

Context c = new InitialContext(properties);

return (LibrarySessionBeanRemote) c.lookup("java:global/EJBModule2/LibrarySessionBean!org.library.com.LibrarySessionBeanRemote");

} catch (NamingException ne) {

Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);

throw new RuntimeException(ne);

}

}

}

Test implementation for JBoss Application Server

package com.acme.library;

import java.lang.reflect.InvocationTargetException;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import java.util.Properties;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.naming.Context;

import javax.naming.InitialContext;

import javax.naming.NamingException;

import javax.rmi.PortableRemoteObject;

public class Main {

LibrarySessionBeanRemote librarySessionBean = lookupLibrarySessionBeanRemote();

private String ejbClassPath;

private String ejbInterfaceClassPath;

private String methodName;

private List<Object> paramValues = new ArrayList();

private Map<String, Object> paramMatch = new HashMap();

public static void main(String[] args) throws NamingException, IllegalArgumentException, InvocationTargetException {

Main clazz = new Main();

LibrarySessionBeanRemote bean = clazz.lookupLibrarySessionBeanRemote();

bean.addBook("Woman");

System.out.println(bean.getBooks());

}

private LibrarySessionBeanRemote lookupLibrarySessionBeanRemote() {

try {

Properties properties = new Properties();

properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");

properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");

properties.put(Context.PROVIDER_URL, "jnp://localhost:1099");

Context c = new InitialContext(properties);

LibrarySessionBeanRemote objec = (LibrarySessionBeanRemote) c.lookup("LibrarySessionBean/remote");

LibrarySessionBeanRemote bean = (LibrarySessionBeanRemote) PortableRemoteObject.narrow(objec, LibrarySessionBeanRemote.class);

return objec;

} catch (NamingException ne) {

Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);

throw new RuntimeException(ne);

}

}

}

Now you can either start GlassFish or JBoss Application Server from Services→ Servers and run the appropriate Main Method of the respective bean.

It’s done now!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s