Enterprise Java Beans

Share Embed Donate


Short Description

EBJ Basics...

Description

Enterprise Java Beans (EJB)

By Janaranjani K

Outline 

Introduction of EJBs.



EJB as a Business Tier Component.



EJB fundamentals.







Enterprise Beans.



Foundation of EJB.



What constitutes Enterprise Bean?

Session beans. 

Components of a Session beans.



The Session EJB Lifecycle (includes stateful and stateless).

An example of stateless session bean (which includes both remote and local interface).

Introduction to EJBs

Introduction to EJBs 

EJB is a standard for building server side component in java. It defines a contract between the component and application server  that enables the component to run in any application server.



EJB components (called (called as enterprise beans) beans ) are 

deployable



imported



loaded

into an application server, which host all these components.

Continued… 

Common Service includes: 

Transaction Management



Resource Management 

Connection pooling



Security



Networking



Persistant Management



Concurrency



Location transparency



Load Balancing etc.

EJB as a Business Tier Component 

Enterprise beans are intended for server side component development not for client side (which lies in presentation tier  and components like thick clients, web service clients etc are used for its development). So it lies in Business Tier.



EJB component can perform the following tasks:  



Perform business logic. Access to the database. 

E.g., Submitting an order for books.



Enterprise bean can achieve database access using JDBC.

Access another system. 

E.g., Calling some legacy system written in COBOL.

EJB Fundamentals

Enterprise Beans 

An enterprise bean is a server side software component that can be deployed in a distributed environment.



It compose of one or more java objects.



The client to an enterprise bean can be one of the following: 

 A servlet 



 An applet 



 An enterprise bean (this results in chain of beans).

Continued… 

Types of Beans: 

Session beans. 

Stateful session beans.



Stateless session beans.



Entity beans.



Message Driven beans.

Diagrammatic Representation Enterprise java beans

Session Beans

Stateful

Stateless

MDBs

Entity beans

CMP

BMP

Session Beans 

Session beans model business process.



They are perform actions. Like, 

Adding numbers



Accessing database



Unlike entity beans, they are non-persistant.



Types: 

Stateful session beans.



Stateless session beans.

Stateful Session Beans 

A stateful session bean maintains a conversational state



The state is relevant only for a single client  



Cannot be seen by other clients

The state is not persistent  

Expires after a certain timeout



It has performance hit due to resource pooling.



Canonical example: Shopping cart

Stateless Session Beans  







Conceptually, the same as stateful session beans. No need to save the conversational state, so called Stateless.  Can have fields, but they are not unique to any client Since the container knows the bean has no state, it can:  Use a single bean instance (While each client thinks it has its own copy)  Destroy/re-instantiate on the fly  Redirect requests to different instances (load balancing) Client does not care on which server the bean is stored Example: Currency conversion bean

Entity Beans  





Entity beans model Business Data. Object-oriented view of entities stored in persistent storage  Normally, each instance represents a row in a relational DB table Persistence code can be written manually (bean-managed persistence, BMP ) or automatically (container-managed persistence, CMP ) A single bean instance (on the server) can be accessed by multiple clients  Unlike stateful session beans.

Message Driven Beans 

New in EJB 2.0 standard



Built on top of JMS to send messages m essages to clients, which in turn would be received by MDB.



It is invoked by the container upon arrival of message at the destination that is serviced by the message-driven bean.



Have no client visibility,

Foundation of EJB

Distributed Architecture  





EJB components are based on Distributed Objects A distributed object is an object that is callable from a remote system. The client can be in-process, out-of-process or a client located elsewhere on the network. It does the invocation with the help of the followings:  Stubs (client side proxy object):  



It masks the network communication from the t he client. It knows how to call over the network using the sockets, massaging parameters as necessary into their network representation.

Skeleton (server side proxy object):   



It masks the network communication from the t he distributed object. It understands how to receive calls on a network. Also knows how to massage parameters from the network representation to their java representations. r epresentations. It then delegates the call to the appropriate implementation object.

What constitutes Enterprise Bean?

Continued… 

Enterprise Bean class:  It contains the implementation of your business logic.  It can be session or entity or MDB.



Component Interface:  Remote Interface:  It is the java interface that enumerates the business methods of your bean class.  Client code always go through the remote or local interface and never interacts with the bean directly.  It obeys the rules for java RMI-IIOP.

Continued… 





Local Interface: 

It’s a high performing version of remote version.



Used when you are calling the bean that presents in the same process.



Unlike remote interface, it will not undergo stubs, skeleton, marshaling/demarshaling of parameters.

EJB object: 

It is container generated implementations of the remote interface.



All client invoications go through this.It delegates the call to bean and implements the remote interface

Local home object: 

High performing version of home object.



Implements the local home interface.

Continued… 

Home interface:  Java interface, acts as a EJB factory.  Provides a handle of bean class to client



Local Home interface:  High version of Home interface.



Home object:  Container generated implementation of home interface.  Obeys RMI-IIOP rules.



Local home object:  High performing version of home object.  Implements the local home interface.

Continued… 

Deployment Descriptor: 



Vendor specific files: 



An XML file, specifies the middleware requirements of your  bean to the container.

Enables you to take the advantage of vendor specific features

EJB-jar file: 

A complete zip of the above said files.



Given to the application server.



Application server unpacks the jar file and loads the bean into the container.

Session Beans

Components of a Session Beans 

To define session bean X, you must create three classes:  The remote (local) component interface, called  X (or  Local)  X (or  X   X Local) 

Extends EJBObject (or EJBLocalObject) 



The home interface, called X  called  X Home Home (or  X   X LocalHome) LocalHome) 



Extends EJBHome (or EJBLocalHome)

The bean class itself, called X  called  X Bean Bean    



(All extended types are from javax.ejb )

Extends SessionBean Should implement java.io.Serializable (for stateful beans) Implements business methods from the component interface Implements lifecycle methods (ejbXXX)

The class names are (strong) conventions

Stateful Session Bean’s Lifecycle

Lifecycle Methods 

Lifecycle methods are defined in the X  the X Bean Bean class



They are named ejb XXX 





ejbCreate, ejbActivate, etc. plus setSessionContext



No other method name should begin with ‘ejb’

Some lifecycle methods also appear in the X  the X Home Home interface 

For session beans, only the create methods appear in the home interface



In the home interface, the ‘ejb’ prefix is not used 



i.e., method should be named ‘create’

Lifecycle methods are invoked by the container when container  when changing state during the bean’s life

ejbCreate Methods in Stateful Session Beans 

A stateful session bean can contain several ejbCreate methods



The methods can have zero or more parameters



They can be called as ejbCreate(...) or ejbCreate XXX (...), (...), for more descriptive names 

e.g., ejbCreateByName(String name)



Must be public void



Should throw javax.ejb.CreateException if creation fails for whatever reason (invalid arguments, etc.)

Continued… 

Must be reflected in the X  the X Home Home interface (local or remote) by a method with identical parameters, that is called ‘create ‘create’’ or  ‘createXXX’ createXXX’ 

Returns the bean component interface type ( X   X )



For remote homes, throws java.rmi.RemoteException



e.g., public ...

X

createByName(String name) throws

Activation and Passivation 

 



During the bean’s lifecycle, the container may decide to  passivate it Normally done to free up memory and other resources Bean is notified immediately before passivation by lifecycle method ejbPassivate() In this method, the bean should:  Release any resources it might be holding 







Sockets, Open files, database connections, etc.

Nullify fields that should not be serialized (cache, ( cache, etc.)

If the bean is referenced by client while passivated, the container  activates it. Bean is notified by ejbActivate() immediately after deserialization  Its chance to restore all field values, resources, etc.

The Lifecycle of Stateless Session EJBs have no state, so activation and passivation are Session EJBs 

meaningless





The container simply destroys instances when low on memory, and creates new ones as needed



Still, ejbActivate and ejbPassivate must be implemented in X  in X Bean Bean (as empty methods)

The resulting lifecycle diagram is somewhat simplified:

ejbCreate Methods in Stateless Session Beans 

For stateless session beans, there can be only one ejbCreate method, accepts zero parameters



Reflected normally in the home interface as create.



Must be public void



Should throw javax.ejb.CreateException if creation fails for  whatever reason (invalid arguments, etc).



Must be reflected in the X  the  X Home Home interface (local or remote) by a method with identical parameters, that is called ‘ create’. create’. 

Returns the bean component interface type ( X  ( X )



For remote homes, throws java.rmi.RemoteException

Sample Session Bean Deployment Descriptors    





   

…. HelloWorldLocalBean examples.webservices.basic.localHome_Object.HelloWo rldHome examples.webservices.basic.localHome_Object.HelloW orldRemote examples.webservices.basic.localHome_Object.HelloWorl dLocalBean Stateless(stateful) Stateless(stateful) Container ….

An example of stateless session bean.

Session Bean Class



package examples.webservices.basic.localHome_Object;



import javax.ejb.SessionBean; import javax.ejb.CreateException; import javax.ejb.SessionContext;

 

 

public class HelloWorldLocalBean implements SessionBean{ private SessionContext ctx;

    

public void ejbCreate() {} public void ejbActivate() {} public void ejbPassivate() {} public void ejbRemove() {}

   

public void setSessionContext(SessionContext ctx) { this.ctx = ctx; } public String sayHelloMethod(int num, String s) {

 







System.out.println("sayHello in webservices.basic.javaclass webservice has "+ "been invoked with arguments " + s + " and " + num+" the context is : "+ctx); String returnValue = "HelloWorldLocalBean....:This message brought to you by the "+ "letter "+s+" and the number "+num+" the context is : "+ctx; return returnValue;



}

 

}

Home Interface 

package examples.webservices.basic.localHome_O examples.webservices.basic.localHome_Object; bject;



import javax.ejb.EJBHome; import javax.ejb.CreateExcepti javax.ejb.CreateException; on; import java.rmi.RemoteExcepti java.rmi.RemoteException; on;

 

 



public interface HelloWorldHome extends EJBHome{ HelloWorldRemote create() throws CreateException, RemoteException ; }

Remote Interface 

package examples.webservices.basic.localHome_Object;



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





public interface HelloWorldRemote extends EJBObject {



public String sayHelloMethod(int num, String s) throws RemoteException;

 

}

Local Home Interface 

package examples.webservices.basic.localHome_Object;



import javax.ejb.EJBLocalHome; import javax.ejb.CreateException;



  

public interface HelloWorldLocalHome extends EJBLocalHome{ HelloWorldLocal create() throws CreateException; }

Local Interface 

package examples.webservices.basic.localHome_Object;



import javax.ejb.EJBLocalObject;



public interface HelloWorldLocal extends EJBLocalObject { public String sayHelloMethod(int num, String s);

  

}

Deployment Descriptor  

    



HelloWorldLocalBean examples.webservices.basic.localHome_Object.HelloWo rldHome examples.webservices.basic.localHome_Object.HelloW orldRemote

Continued… 





    

examples.webservices.basic.localHome_Object.HelloWorl dLocalHome examples.webservices.basic.localHome_Object.HelloWor  ldLocal examples.webservices.basic.localHome_Object.HelloWorl dLocalBean Stateless Container

Q&A

Thank you all

View more...

Comments

Copyright ©2017 KUPDF Inc.
SUPPORT KUPDF