Saturday, July 27, 2013

Creating a Simple EJB SessionBean Using NetBeans & GlassFish Server


Hi , Today we are going to see how to create a simple EJB Stateless Session Bean using NetBeans and then we are going to deploy it in GlassFish Server and finally we will test it.

We are not going to discuss what is EJB what is Session Bean and how we have to use it etc.

So we require the following :

1.NetBeans ID
2. GlassFish Server.

Generally NetBeans comes along with GlassFish Server .While downloading NetBeans you can select this Option so that NetBeans Installer will download along with GlassFish Server.

Install the NetBeans with GlassFish Server.Once it is done we are ready to Go.

In this tutorial i am using the NetBeans 7.3 Version along with GlassFish Server 3.1.2 Version.

Now open NetBeans ID.

Step 1:

- Select : File->New Project ->Java EE - > EJB Module and click the "Next" Button.

- Give Project a Suitable Name Here i am giving "MyApp" click the "Next" Button.

- Select the Server as "GlassFish" with the version You have installed . Here i am selecting "GlassFish Server 3.1.2"

-  Select the J2EE Version what ever you have installed. Currently it is J2EE 6. and click the "Finish" Button.

You will have the Project Directory Structure something like this:















Step 2:

- Create a New Package called test under the Source Package Folder.

-  Create a new interface called "Home".under test package and add the following code.

 - This is a Remote Interface this can access from anywhere,once it is deployed in the server.

package test;
import javax.ejb.Remote;

@Remote
public interface Home {
public String sayHello(String name);
   }

Step 3:

Create a SessionBean class which implements this Interface .

RightClick on test and create New --> JavaClass--> HomeImpl

package test;

import javax.ejb.Stateless;
@Stateless
public class HomeImpl implements Home{

@Override
public String sayHello(String name) {
return "Welcome to EJB"+" "+name;
}

}

Step 4:

Create a GlassFish Descriptor File by

Righ Click on the Project "MyApp" and then choose New -->Other-->GlassFish-->GlassFish Descriptor and then click the "Next" button and click the "Finish " Button.

 glassfish-ejb-jar.xml file will be created under the Configuration Files Folder.

Set the JNDI Name for the HomeImpl Bean by editing the file or by clicking the TAB "EJB" and giving jndi name as "abc". 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-ejb-jar PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 EJB 3.1//EN" "http://glassfish.org/dtds/glassfish-ejb-jar_3_1-1.dtd">
<glassfish-ejb-jar>
<enterprise-beans>
<ejb>
<ejb-name>HomeImpl</ejb-name>
<jndi-name>abc</jndi-name>
<pass-by-reference>true</pass-by-reference>
</ejb>
</enterprise-beans>
</glassfish-ejb-jar>

Save the XML File.

Note : There can be only one glassfish-ejb-jar.xml file per EJB Module.

Step 5:

- Right click on the Project Node(MyApp)and select clean and Build.

- After clean and build again right click on the project node and select "Deploy".

For Both this Actions "BUILD SUCCESSFUL" Message will be displayed in the console output.Then every thing is fine and our EJB Session Bean  HomeImpl with JNDI Name "abc" is successfully Deployed.

Step 6:

RightClick on the Project Node (MyApp) and create new Class called "Main"

package test;

import java.util.Properties;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;


public class Main {

  public static void main(String arg[])throws Exception
{
Properties p=new Properties();
InitialContext c=new InitialContext();
Object l=c.lookup("abc");
Home r=(Home)l;
System.out.println(r.sayHello("Ayaz"));
}

}

Run this class to check our EJB is deployed Sucessfully . It will give the following result in the output console.
run:
Welcome to EJB Ayaz
BUILD SUCCESSFUL (total time: 14 seconds)


So far we have created a Stateless EJB Session Bean and deployed it in the Server and checked it through the same EJB Module , this will work since we are accessing from EJB Container locally from our EJB Module .

To access this bean remotely from another Java Component such as Web Application or Java Swing Application or Java Webservice we require the Jar files created from the "HomeImpl" Session Bean just now we created  & Server specific jars to load the Bean.

In my next Post on EJB we will see how to do this...


         Next >>                                          Index

--------------------------------------------------------------------------------------------------------



Technology Blogs
blogs

6 comments:

  1. Easy to understand,,

    ReplyDelete
  2. really nice and easy to implement the bean Thank you

    ReplyDelete
  3. i can't able deploy the bean in server...
    i get this error msg....
    "
    deploy?DEFAULT=/home/jec/NetBeansProjects/ejb_simulation/build/classes&name=myapp&force=true failed on GlassFish Server 4.0
    Error occurred during deployment: Exception while deploying the app [myapp] : Referencing error: this bundle has no bean of name: Homeimpl. Please see server.log for more details.
    /home/jec/NetBeansProjects/ejb_simulation/nbproject/build-impl.xml:919: The module has not been deployed.
    See the server log for details.
    BUILD FAILED (total time: 0 seconds)

    ReplyDelete
  4. I get the answer after a long tragedy...............!

    ReplyDelete