Você está na página 1de 5

Creating a Simple EJB SessionBean Using NetBeans & GlassFish

Server

So we require the following :

1.NetBeans ID
2. GlassFish Server.

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.
Java Naming and Directory Interface
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.

Calling / Accessing an EJB Stateless Bean Remotely


We are going to access the same bean which we deployed in the previous part namely
"HomeImpl"(Seesion Bean) with the JNDI Name "abc".

To access an EJB Bean remotely we need to follow these steps :

Step 1:

Add all the Require Jar files in the libraries.Right click on the Libraries and select add Jar
and go to the GlassFish folder and select all the jars inside the "Modules" folder.

Ex :
E:\Install-files\Net\glassfish\modules

Add the Jar file "MyApp" our EJB Project jar which we have created previously, if not create
it using the "clean and build" option by right clicking on the Project Node and select "Clean
and Build".The jar file will be created and available inside the dist folder of the Project .

Step 2 :

Once all the Jars is added, we can write our client code to access the Bean.
Code:

import java.util.Properties;
import javax.naming.InitialContext;
import test.Home;
import test.MyBeanRemote;

public class Main


{

public static void main(String[] args)throws Exception {

/*
Require EJB2.0 Version
System.setProperty("java.naming.factory.initial",
"com.sun.enterprise.naming.SerialInitContextFactory");
System.setProperty("java.naming.factory.url.pkgs", "com.sun.enterprise.naming");
System.setProperty("java.naming.factory.state",
"com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
System.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");
System.setProperty("org.omg.CORBA.ORBInitialPort", "3700"); */
InitialContext ic = new InitialContext( );
Object l=ic.lookup("abc");
Home d=(Home)l;
System.out.println(d.sayHello("Jack Sparrow..."));

Output :
Welcome to EJB Jack Sparrow...

Note : All the GlassFish jars should be added in libraries and the Bean should be deployed
in the GlassFish Server otherwise an exception will occur.

This will work fine under both the GlassFish Server and client in the same System , when we
tried to access the EJB Client from other System we have to specify the IPAddress & Port of
the machine in which server is running in the System.Property values.

Você também pode gostar