Creating your first WebSphere EJB (Enterprise Java Bean) app - Backend JAR

Home > Search
  by

An EJB (Enterprise Java Bean) is a model that allows you to create separation between the frontend of an app, such as the JSP pages and servlets used to produce the app, and the backend, such as the database connection and business logic working behind the scenes. As an example, let's say you have an identical class being used by numerous applications that connects to a database. Instead of each application containing the database connection class, a backend EJB can be created, which contains the database connection class, and then individual frontend applications can use the database connection class by calling the backend EJB. 

 


Create a new EJB Project

In Eclipse, select File New > EJB Project. Give your project a name, such as "EJB Backend", select WebSphere or Liberty as the server runtime, and select Finish. I also like to remove the check mark from "add to EAR", just to reduce the cluter when learning something now. The EJB Backend folder should be added to the left panel of Eclipse.

 

Create Packages

Expand EJB Backend, right click on the ejbModule folder, and select New > Package. Create the follow two packages:

  • com.ejb.businesslogic
  • com.ejb.remoteinterface

The new packages should now be displayed under the ejbModule folder.

 

Create Interface

Right click on the com.ejb.remoteinterface package, and select New > Interface. Give the interface a name, such as "Greeting", and select Finish. The new interface should now be displayed under the com.ejb.remoteinterface package.

 

Add the following markup to the Greeting.java interface.

package com.ejb.remoteinterface;

import javax.ejb.Remote;

@Remote
public interface Greeting {
	public String sayHello();
}

 

Create Class

Right click on the com.ejb.businesslogic package, and select New > Class. Give the class a name, such as "Hello", and select Finish. The new class should now be displayed under the com.ejb.businesslogic package.

 

Add the following markup to the Hello.java class.

package com.ejb.businesslogic;

import javax.ejb.Stateless;
import com.ejb.remoteinterface.Greeting;

@Stateless
public class Hello implements Greeting {

	public Hello() {
		
	}

	public String sayHello() {
		return "Hello World";
	}
}

 


Deploy EJB to WebSphere

Right click on your EJB project and select Export. This will create a JAR file. You can then deploy the JAR file to your WebSphere application server. Once deployed, you will then want to add the Stub to the JAR, by running this script.

~]# was_home/bin/createEJBStubs.sh was_home/profiles/your_profile/installedApps/your_ear/yourEJB.jar

 

In this example, the following Stub would be added to the JAR.

com/ejb/remoteinterface/_Greeting_Stub.classPK

 

You will also want create a JNDI name that will be used by the clients to call the class in the EJB. In WebSphere, go to Applications > All applications > your EJB > Bind EJB Business, and give the EJB an JNDI name (HelloJNDI in this example). Restart the JVM.

 

The application server logs should provide you with the binding details, which the clients will need. For example, these are the logs of the EJB deployed to WebSphere.

[mm/dd/yy hh:mm:ss] 000006a WASNameSpaceB I CNTR0167I: 
The server is binding the com.ejb.view.HelloWorldRemote interface of the HelloWorld enterprise bean in the EJBTest.jar module of the EJBTest_jar application. 
The binding location is: HelloJNDI

[mm/dd/yy hh:mm:ss] 000006a WASNameSpaceB I CNTR0167I: 
The server is binding the com.ejb.view.HelloWorldRemote interface of the HelloWorld enterprise bean in the EJBTest.jar module of the EJBTest_jar application. 
The binding location is: java:global/EJBTest/HelloWorld!com.ejb.view.HelloWorldRemote

 

You can now follow this tutorial to configure a client application to use the Hello class from the EJB.

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.




Please enter in the box below so that we can be sure you are a human.




Comments