Simple EJB 3 Application using JBoss(5.1) and Eclipse

Project Description :

We are going to create a simple EJB 3 stateless session bean project and a remote Java application client which will call/invoke the bean.

For testing this example we will write a remote Java Application Client.For simplicity, the session bean and the client to access the session bean are created in the same project.

Tools Used to develop this Application :

  1. JDK 6 (Java SE 6)
  2. EJB 3.0 (stateless session bean)
  3. Eclipse Indigo
  4. JBoss Application Server (AS) 5.1.0

Project Description :

We are going to create a simple EJB 3 stateless session bean project and a remote Java application client which will call/invoke the bean.

For testing this example we will write a remote Java Application Client.For simplicity, the session bean and the client to access the session bean are created in the same project.

Tools Used to develop this Application :

  1. JDK 6 (Java SE 6)
  2. EJB 3.0 (stateless session bean)
  3. Eclipse Indigo
  4. JBoss Application Server (AS) 5.1.0




Note:- To Setup the development environment for this Project click here

Create a new EJB Project :

Open Eclipse IDE and create a new EJB project which can be done by clicking on, File menu -> New -> EJB Project

ejb project in eclipse

Name the Project as SimpleEjb3Project.Make sure that Target Runtime as JBoss 5.1 Runtime EJB Module version as 3.0 .

ejb project in eclipse

Now Click Next -> Next -> and Finish .You will see an EJB project in the Project Explorer view as shown below.

ejb project in eclipse

Creating Session Bean and Bean Interface :

Create a new package by Right click on ejbModule -> New -> Package and name this package to com.jwt.ejb.business and click on Finish.

ejb project in eclipse

Right click on this package and Create a new Interface and name it to Hello and click Finish

ejb project in eclipse

ejb project in eclipse

Interface can be either @Remote or @Local or without any annotation. In this example we have used @Remote .

Add following code into this Interface.

Hello.java
package com.jwt.ejb.business;

import javax.ejb.Remote;

@Remote
public interface Hello {

	public String sayHello();
}

Create a new package by Right click on ejbModule -> New -> Package and name this package to com.jwt.ejb.businesslogic and click on Finish.

ejb project in eclipse

Create a new Class HelloBean in this package which should implement Hello interface and add the following code.

This is a Bean class. Bean type can either be @Stateful or @Stateless . In this example we have used @Stateless .

Hello.java
package com.jwt.ejb.businesslogic;

import javax.ejb.Stateless;

import com.jwt.ejb.business.Hello;

@Stateless
public class HelloBean implements Hello {

	public HelloBean() {

	}

	public String sayHello() {

		return "Hello Boss Welcome to EJB";
	}

}


Creating Client :

The next step is to write a remote Java client application (with main()) for accessing and invoking the EJBs deployed on the server.

Client uses JNDI to lookup for a proxy of your bean and invokes method on that proxy.

Creating JNDI InitialContext :

All naming service operations are performed on implementation of javax.naming.Context interface.The starting point of interacting with the naming service is to obtain a Context by providing the properties specific to the server implementation being used. In our case it is, JBoss Application Server.

To create a javax.naming.InitialContext , we need to initialize it with properties from the environment. JNDI verifies each property's values from the following two sources,

  • jndi.properties resource files found on the classpath.
  • Using parametrized constructor of InitialContext which takes properties of supplied environment

We are going to use constructor for initializing the InitialContext.

For JBoss AS 5 we need to set following properties

Context.INITIAL_CONTEXT_FACTORY = org.jnp.interfaces.NamingContextFactory<br>
Context.URL_PKG_PREFIXES = org.jboss.naming:org.jnp.interfaces<br>
Context.PROVIDER_URL = jnp://localhost:1099

Steps to create Client Class :

  • Right click on ejbModule -> New -> Class
  • Enter the class name as Client .
  • Enter the package name as com.jwt.ejb.test and click on Finish.

ejb project in eclipse

Client.java
package com.jwt.ejb.test;

import java.util.Properties;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.jwt.ejb.business.Hello;

public class Client {
	public static void main(String[] args) {
		Hello bean = doLookup();
		System.out.println(bean.sayHello()); // 3. Call business logic
	}

	private static Hello doLookup() {
		Context context = null;
		Hello bean = null;
		try {
			// 1. Obtaining Context
			context = getInitialContext();
			// 2. Lookup and cast
			bean = (Hello) context.lookup(LOOKUP_STRING);
		} catch (NamingException e) {
			e.printStackTrace();
		}
		return bean;
	}

	private static final String LOOKUP_STRING = "HelloBean/remote";
	/*
	 * location of JBoss JNDI Service provider the client will use. It should be
	 * URL string.
	 */
	private static final String PROVIDER_URL = "jnp://localhost:1099";

	/*
	 * specifying the list of package prefixes to use when loading in URL
	 * context factories. colon separated
	 */
	private static final String JNP_INTERFACES = "org.jboss.naming:org.jnp.interfaces";

	/*
	 * Factory that creates initial context objects. fully qualified class name.
	 */
	private static final String INITIAL_CONTEXT_FACTORY = "org.jnp.interfaces.NamingContextFactory";

	private static Context initialContext;

	public static Context getInitialContext() throws NamingException {
		if (initialContext == null) {
			// Properties extends HashTable
			Properties prop = new Properties();
			prop.put(Context.INITIAL_CONTEXT_FACTORY, INITIAL_CONTEXT_FACTORY);
			prop.put(Context.URL_PKG_PREFIXES, JNP_INTERFACES);
			prop.put(Context.PROVIDER_URL, PROVIDER_URL);
			initialContext = new InitialContext(prop);
		}
		return initialContext;
	}

}

Directory Structure of Project :

ejb project in eclipse

Deploying the project :

Now we need to deploy the our EJB "SimpleEjb3Project" on server.Follow the steps mentioned bellow to deploy this project on server.

Strat the server

Right click on "JBoss 5.1 Runtime Server" from Servers view and click on Start.

ejb project in eclipse

Deploy the application :

Right click on "JBoss 5.1 Runtime Server" available in Servers view -> Add and Remove. -> Select the SimpleEjb3Project JAR file from the left pane and click Add-> and then Finish.

ejb project in eclipse

ejb project in eclipse

If the project is deployed properly with global JNDI mapping then you will see the following message in the console.

ejb project in eclipse


Testing the Client Application :

Open Client class Client.java in your eclipse editor and press Ctrl + F11 to run the client.

Output :

Hello Boss Welcome to EJB



comments powered by Disqus