Tuesday, December 23, 2008

What's new in EJB3

Before discussing the features, which ejb3 is offering, i just want to explain the basic terms which are there in ejb previous versions, so that it helps my readers to understand the features of ejb3 quite easily. If u already aware all these basic concepts then you can directly move to Ejb3 features.
Following are the basic terms:
1.what is ejb
2.what is application server
3.what is web server
4.what is ejb container
5.what is remote interface
6.what is home interface
7.what r the types of ejb

What is an ejb:
ejb is server side architecture which allows us to develop server side components, which are portable and reusable and can be deployed into application server easily. There is a difference between javabean and enterprisebeans. Javabeans are development components and used to bulid an entire application or integrate different ejbcomponents as an application. Unlike ejb, java beans them self are not deployable.
Enterprise beans are used for developing large and distributed applications. In the past, most companies build there middle ware service, these days, companies that build their own middle ware risk setting themselves up for failure. High-end middle ware is hideously complicated to build and maintain.


APPLICATION SERVER:
The application server was born to let u provide these middleware services such as resource pooling, transaction management and security authorization etc to enterprise beans.
An application server is an application program that accepts connections in order to service requests, by sending back response
An application server may be a part of three-tier architecture model. A 3 -tier architecture includes client, middle Tier and Eis(enterprise information system) tier. It may thus consists of presentation view as the GUI interface,middle tier as collection of business logic application and eis tier consists of database management systems to support the applications.

What is web server:
web server is is a program which provides web services to clients. Web server contains a web container, it provides runtime environment to the servlets and jsp's.
Web server serve less clients as compared to application server(load balancing). Application server contains a both web container as well as ejb container. Application server provides middle ware services to EJB.

What is ejb container:
ejb container is program which is there in application server. Application server contains both web container as well as ejb container. ejb container creates a runtime environment to the bean components.

Types of ejb:

Ejb defines 3 kinds of enterprise beans:
1.session beans
2.Entity beans
3.Message driven Beans

session beans:
These are model business processors. They are like verbs because they are actions. The action could be any thing such as adding numbers, accessing database.
two types of session beans are there

a. stateless session beans: These beans are loss client state once it send response to the client. Treat client as new client during next request.
ex:adding 2 numbers


b. stateful session beans: These beans are maintain client states during multiple requests of clients.
ex: shopping-cart

Entity beans:
Entity beans are model business data. They are like nouns, because they are data objects that is java objects cache database information. Each entity bean represents one record in database.
two types of entity beans are there
a.bean managed entity bean
b.container managed entity bean
In bean managed entity bean the persistence management will be controlled by the bean(programmer). In a container managed entity bean the persistence behavior will be controlled by the container.

Message driven beans:
message driven beans are similar to session beans in that their actions, difference is that you can call message driven beans only by sending messages to those beans.
Now iam going to write small stateless session bean program and I explain each and every statement int it.
ex: this session bean is simple which performs multiplication operation of two numbers.
The following are the things which are needed to write meaningful session bean
1.A remote interface
2.A home interface
3.class which extends Session Bean
4.And configurations of ejb-jar.xml and jboss.xml
5.client program

Remote Interface:
A remote interface contains all the function prototypes of business methods which wes are implemented in session bean class, remote interface must extend EJBobject. Container implements the remote interface, and produce a object called EJBobject. EJBobject acts as an interface between client and bean instances.

package com.interfaces;
import java.rmi.RemoteException;
import javax.ejb.*;
interface myremoteineterface extend EJBobject
{
int add(int a ,int b)throws rmi.RemoteException;
}
Home interfaces:
A home interface defines the methods that allow a remote client create, find, remove EJBobject, as well as business methods that are not specific to a bean's instance.The remote home interface is defined by the enterprise provide and implemented by the enterprise bean container.

package com.interfaces;
import java.rmi.RemoteException;
import javax.ejb.*;
public interface myhomeinterface extends EJBHome
{
// it creates remote object
public com.interfaces myremoteinterface create() throws javax.ejb.createException, java.rmi.RemoteException;
}

session bean class:
Session bean class must implement the SessionBean interface and must implements all the corresponding functions into the class, here we are just writing simple bean so need to write code to all the functions. There I provide all null implantations to all functions.

package com.ejb;
import javax.ejb.*;
public class addbean implements SessionBean
{
private SessionContext ctx;
public void ejbcreate()throws javax.ejb.CreateException{}
public void ejbActivate()throws EJBException,RemoteEXception{}
public void ejbPassivate()throws EJBException,RemoteEXception{}

public void remove()throws EJBException,RemoteEXception{}
public void setSessionContext(SessionContext newctx)
{
ctx=newctx;
}
public int add(int x,int y)
{
int sum=x+y;
return sum;
}
}

Depeloyment Descriptor:
We need generate a deployment descriptor, which describes our bean's middle ware requirements to the container. DD'S ARE one of the key features of EJB because they allow you to declarative specify attributes on your beans, rather than programming this functionality into the bean itself.

EJB-jar File
So far we have written all the necessary files for our component, we need to package all the files together i s n an Ejb-jar file. If you are using development environment, it generates Ejb-jar.xml for you or we can generate manually as follows : jar cf firstbean.jar *
The asterisk indicates the files to include in the jar- the bean class, home interface, local home interface(optional), remote interface, local interface(optional), deployment descriptor and vendor-specific files(depending on your container)
The following is the folder structure within the Ejb-jar file
META-INF/MANIFEST.MF
META-INF/ejb-jar.xml
ejb/addbean.class
interfaces/myremoteinterface.class
interfaces/myhomeinterfacecs.class
The MANIFEST.MF file is a listing of the files within the Ejb-jar files. It is auto generated by the jar utility.
Jboss.xml:
It is an vendor specific code.It is an xml file, we must provide the entries to the ejb-name, jndi-name, method-attributes, which must be place with in the tag.

client program:
Client program can be simple java program or servlet/jsp. It performs the following tasks.
1.locate JNDI server.
2.lookup in the JNDI registry for home object.
3.client get the remote object by using home object which in turn calls create method to obtain remote object.
4.with remote object you can call the method of bean class.it calls the add() method.
package packs.client;
import javax.naming.*;
import java.util.Properties;
public class TestClient
{
public static void main(String args[])
{
Properties props=System.getProperties();
Context c=new InitialContext(props);
//this entry we provide it in jboss.xml
myhomeinterface rhi=(myhomeinterface)PortableRemoteObject.narrow(c.lookup("myhomeinterface1"),myhomeinterface.class);
myremoteinterface mhi=rhi.create();
System.out.println("SUM OF TWO NUMBERS 10 AND 30 IS"+mhi.add(10,30));
}
}

No comments: