Pin It

How to create, deploy and run Session Bean EJB module in 2.x in Application Server using a stand alone Java client ??.

A simple steps in creating, deploying and running Stateless/Stateful Session Bean in EJB 2.1 in application server (say Glassfish Server 3.x) using a stand alone Java client


The steps are as follows :

Step 1 - Create a Java Project in Eclipse(Juno). Add a Jar to its build-path by the name called as "javax.ejb.jar", you can either download the jar from the internet or download Glassfish server and to its /glassfish/modules folder there is jar available. After placing the jar add a package with your name. I am using package as "com.hubberspot.ejb"

Step 2 - Create a new Interface in the package created above by the name say "HelloRemote". This interface will act as a component interface, where all the business methods goes. This interface goes go client which it uses to invoke on the Session Bean. This interface extends EJBObject interface and also has the business methods throwing RemoteException. See the code below :

----------- Component Interface --------------
package com.hubberspot.ejb;

import java.rmi.RemoteException;
import javax.ejb.EJBObject;

public interface HelloRemote extends EJBObject {
	
	public void sayHello() throws RemoteException;

}




Step 3 - Create a another new Interface in the package created above by the name say "HelloHome". This interface will act as a home interface. This interface is used by the clients in getting a reference to the remote interface. After getting the remote interface client can call the business methods declared on that remote interface. This interface extends EJBHome interface which has one mandatory method by the name create which returns HelloRemote reference and throws a RemoteException. See the code below :

----------- Home Interface --------------
package com.hubberspot.ejb;

import java.rmi.RemoteException;
import javax.ejb.EJBHome;


public interface HelloHome extends EJBHome {
	
	public HelloRemote create() throws RemoteException;

}




Step 4 - Create a Bean class in the package created above which implements javax.ejb.SessionBean. This bean class overrides the unimplemented methods of SessionBean interface which itself inherits from RemoteInterface. This bean class along with unimplemented methods of SessionBean interface also provides a mandatory method called as ejbCreate() with parameters same as that of the create method in the home interface created above by the name HelloHome. I have created a business method by name sayHello(), this method will contain the business logic which will be invoked by the client. See the code below :


-------------- Session Bean ---------------------

package com.hubberspot.ejb;

import java.rmi.RemoteException;
import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;


public class HelloBean implements SessionBean {

	@Override
	public void ejbActivate() throws EJBException, RemoteException {
	}

	@Override
	public void ejbPassivate() throws EJBException, RemoteException {
	}

	@Override
	public void ejbRemove() throws EJBException, RemoteException {
	}

	@Override
	public void setSessionContext(SessionContext arg0) throws EJBException,
	RemoteException {
	}

	// used for creating a reference to RemoteInterface
	public void ejbCreate () throws CreateException{
		System.out.println("ejb - create");
	}

	// Our business method which client will call
	public void sayHello() throws RemoteException, CreateException {
		System.out.println("Hello World by EJB 2.x ...");
	}

}




Step 5 - Create a folder by the name META-INF in the 'src' folder of Java Project created above in step 1. After creation of META-INF folder, we place two xml files into this folder. 1. ejb-jar.xml and 2. glassfish-ejb-jar.xml.


Step 6 - Create a xml file by name : "ejb-jar.xml" and place it in META-INF folder created in step 5. Here ejb-jar.xml file also called as Deployment Descriptor is the configuration file which is mandatory to be deployed in server before deploying any Enterprise Java Beans. This configuration file contains description of (which is an) home interface, remote interface and bean class in the application. This xml file is self-explanatory. It has :
1. 'enterprise-beans' tag, which contains meta data or information about each and every EJB.
2. 'ejb-name' tag, which informs server which is EJB name for this application.
3. 'home' tag, which informs server which is Home Interface for this application.
4. 'remote' tag, which informs server which is Remote Interface or Component Interface for this application.
5. 'ejb-class' tag, which informs server which is EJB class for this application.
6. 'session-type' tag, which informs server which is session type for this application.
7.'transaction-type' tag, which informs server which is transaction type for this application.
See the deployment descriptor file below which we need for this example -


---------------- Deployment Descriptor -----------------

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, 
Inc.//DTD Enterprise JavaBeans 2.0//EN" 
"http://java.sun.com/dtd/ejb-jar_2_0.dtd" >


<ejb-jar>
	<display-name>Session Bean Example</display-name>
	<enterprise-beans>
		<session>
			<description>Simple Session Bean Example</description>
			<ejb-name>HelloBean</ejb-name>
			<home>com.hubberspot.ejb.HelloHome</home>
			<remote>com.hubberspot.ejb.HelloRemote</remote>
			<ejb-class>com.hubberspot.ejb.HelloBean</ejb-class>
			<session-type>Stateless</session-type>
			<transaction-type>Bean</transaction-type>
		</session>
	</enterprise-beans>
</ejb-jar>



Step 7 - Create a xml file by name : "glassfish-ejb-jar.xml" and place it in META-INF folder created in step 5. Here glassfish-ejb-jar.xml file also called as Server Specific Configuration file which varies from server to server. I am using Glassfish Server in this example for the deployment and running of this module. This configuration file contains description of (which is an) JNDI-Names of the Enterprise Bean in the applications. This name is used by the client code to get the reference of Home Interface of the session bean. This xml file is self-explanatory. See below :



--------------- glassfish-ejb-jar.xml-----------------


<?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>HelloBean</ejb-name>
			<jndi-name>jndi/HelloBean</jndi-name>
		</ejb>
	</enterprise-beans>


</glassfish-ejb-jar>




Step 8 - Place ejb-jar.xml and glassfish-ejb-jar.xml in the META-INF folder created in the step 5


Step 9 - Create a jar for final EJB deployment. In order to deploy our Session Bean created above we need to place all the files such as HelloBean.class , HelloRemote.class, HelloHome.class , META-INF folder with ejb-jar.xml and glassfish-ejb-jar.xml. This jar can be created by two ways :
1. Using Eclipse Export option - Right-Click on the Java Project --> export -->jar --> Click Next --> place the .classes files and META-INF folder along with two xml files --> Click next to export after giving the name of Jar say (Application_name.jar)
2. Using the command argument - Traverse to workspace project of the eclipse on the command prompt. Run the command under the Java project folder as: jar cvf myjar.jar META-INF -C bin com/hubberspot/ejb







Step 10 - After completion of step 9 we will have a jar, which we will be deploying on the Glassfish server. This jar will contain all the .classes files in the com.hubberspot.ejb package and two xml files in the META-INF folder. In order to deploy the jar on the server we will open Glassfish server admin console. The default-url of the console would be 'http://localhost:4848/'. Under applications tab we will click on deploy button, it will ask for the jar file to deploy. We will traverse where our jar file is created and than after uploading jar file we will click ok. The jar files get deployed on the server. After deploying the jar just start the Glassfish server





























Step 11 - Create yet another jar for the client (this jar will go to client so that it can invoke business methods ). This jar will contain only the two interfaces such as HelloHome.class and HelloRemote.class. You can create the jar with only two such interfaces by using either option mentioned in step 9.

Step 12 - Create a Java Project here it will act as stand alone Java project. Create a package in this project say by any name (com.hubberspot.ejb) and create a Test class which will test our HelloBean's sayHello() method deployed on the server.The TestClass.java is shown below and code is explained in comments. After creating TestClass.java just run the main method and check the console of Glassfish server for the output.
Note: before running the main method compiler will throw compile-time error. You need to first put the client jar created on step 11 and you just place all the jar files coming in the glassfish/modules folder of the glassfish server



------------ TestClass.java at the Java Client Project ---------------

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.hubberspot.ejb;

import java.rmi.RemoteException;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

/**
 *
 * @author Administrator
 */
public class TestClass {

	public static void main(String[] args) {

		try {

			// Creating a Property object
			Properties props = new Properties();

			// Providing the props the Initial Context factory Class
			props.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.enterprise.naming.SerialInitContextFactory");
			// Setting the host by giving url here its localhost
			props.setProperty("org.omg.CORBA.ORBInitialHost", "localhost");			
			// Glassfish's default jndi registry server port
			props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");

			// Creating an Initial Context object by passing 
			// properties created above. It will help us 
			// locating the Home Interface through JNDI
			InitialContext ctx = new InitialContext(props);

			// Having a lookup for the jndi name specified in 
			// the glassfish-ejb-jar.xml file
			Object obj = ctx.lookup("jndi/HelloBean");

			// Converting Corba object to portable object below
			// and getting the Home interface reference
			HelloHome beanhome = 
					(HelloHome) javax.rmi.PortableRemoteObject.narrow(obj, HelloHome.class); 
			// Calling the create method of home interface which 
			// will return back the reference of remote interface
			HelloRemote bean = (HelloRemote)beanhome.create();

			// calling the business method of remote interface
			bean.sayHello();
		}
		catch (RemoteException e) {

			e.printStackTrace();
		}
		catch (NamingException e) {

			e.printStackTrace();
		}

	}


}




Output of the program at Glassfish console :



Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot