1 / 16

EJB Framework

EJB Framework. EJB Framework. As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major elements of the EJB framework include EJB server, EJB container, EJB components, and EJB client.

brook
Download Presentation

EJB Framework

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. EJB Framework

  2. EJB Framework • As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major elements of the EJB framework include EJB server, EJB container, EJB components, and EJB client. • EJB or J2EE server - EJB server stores the container and offers system-level services. • EJB container - EJB container provides runtime services such as memory allocation and database connectivity to a bean. It also manages the life cycle of the beans. It acts as an interface between the EJB components and the outside world. • EJB components – EJB architecture defines three types of EJB components, session beans, entity beans, and message-driven beans. Every EJB component is associated with two interfaces, a home interface and a component interface.

  3. EJB Types • Session Beans • Represents business processing performed in an application • Entity Beans • Persistence data in an application is represented by Entity Beans • Message Driven Beans • Represents business processing to be carried out in asynchronous way

  4. EJB Components • EJB Application consists of following main components • Home interface - EJBHome is the home interface that provides methods for creating, removing, and locating instances. An EJB developer defines the home interface, and the EJBHome object is generated by the tools provided by the container vendor. An instance of the class that implements the home interface is called a Home object. • Component or Remote interface - EJBObject is the component interface that provides the business methods present in EJB. An EJB developer defines the remote interface, and EJBobject is generated by the tools provided by the container vendor. An instance of a class that implements the component interface is called EJBObject. • Enterprise Bean - Bean class provides the implementation of business methods declared in Home or Component interface. Container instantiates the bean (life-cycle) and makes it available to server client’s requests. • EJB client – A client for an EJB could be a java program, or another enterprise bean or can be a web module also. A client cannot call methods on a bean directly. It has to invoke the method on the EJBObject. The component interface exposes the business methods of the bean and the EJBObject is an instance of the class implementing the component interface.

  5. Session Beans • Stateless Session Beans • does not maintain any conversation state of a client • E.g. : Whether monitoring system • Stateful Session Beans • conversation state of a client is maintained for the duration of the session • E.g. : Banking Application

  6. An Entity Entity Bean Instance Entity Beans • Bean Managed Persistence • Container Managed Persistence

  7. Contd… • An entity bean is a type of enterprise bean that represents data saved in persistent stores. • A persistent store can be a database table or a flat file. • An entity bean instance represents a particular row or data in a particular table or multiple tables. For example, if employee is an entity in a problem specification, a specific employee represents an instance of an entity bean. The information about an employee can spread across multiple tables too. The tables could provide information on personal details, professional details, project details. • Thus the instances of entity bean may engage themselves in some or other kind of relationships. This is similar to the foreign key-primary key relationships in the RDBMS. • Entity beans can be of two types, bean-managed persistence or BMP and container-managed persistence or CMP. A BMP manages its own state in a data store where as a CMP assigns saving its state to the EJB container.

  8. Coding Example package mypack.Hello; import javax.ejb.*; public class HelloBean implements SessionBean { public void ejbCreate() { System.out.println("ejbCreate()"); } public void ejbRemove() { System.out.println("ejbRemove()"); } public void ejbActivate() { System.out.println("ejbActivate()"); } public void ejbPassivate() { System.out.println("ejbPassivate()"); } public void setSessionContext(SessionContextctx) { System.out.println("setSessionContext()"); } public String SayHello() { return "Hello, World!"; } } Bean Name Bean type Call Back Methods Business Method

  9. Bean Class • In this code, we have only one business method called “SayHello”. • This method displays a message “Hello World”. When developing enterprise beans, we follow a naming convention that states that the name of the bean class must contain an application-specific name followed by the word “Bean”. • Here, the bean is called “HelloBean”. • Notice the statement “HelloBean implements SessionBean”. This merely indicates that we are creating a session bean and doesn’t provide any information on the type of session bean. • This information is configured externally. Also, notice that in addition to the business method, the bean also contains several other methods called container call-back methods. These methods are defined in the session bean interface.

  10. Coding Example • Component Interface package mypack.Hello; import javax.ejb.*; import java.rmi.RemoteException; import java.rmi.Remote; public interface Hello extends EJBObject{ public String SayHello() throws RemoteException; }

  11. Component Interface • This interface extends from a base interface called EJBObject. The methods in the component interface expose the business methods inside a bean. This base interface inherits from java.rmi.Remote. Hence, the component interface, Hello, also inherits from java.rmi.Remote. Therefore, methods in the component interface are remote methods and so throw RemoteException. The container creates a class that implements the methods in the component interface and its stub. It should also implement the methods as defined in EJBObject. • While defining a component interface, you need to follow some rules. • Component interface should extend from EJBObject • All business methods declared in component interface should throw RemoteException • All the arguments passed and return values of these business methods must be RMI-IIOP compatible

  12. Coding Example • Home Interface package mypack.Hello; import javax.ejb.*; import java.rmi.RemoteException; public interface HelloHome extends EJBHome { Hello create() throws RemoteException, CreateException; }

  13. Home Interface • The home interface is the entry point for the client to gain access to the EJBObject that represents the actual EJB. In this sample code, we have a home interface called HelloHome. The code defines a single method named create(). This method returns a reference of type component interface i.e. EJBObject. This method is executed by an instance of a class automatically generated by the container from the home interface of your application. The object is called EJBHome object. The EJBHome object serves as a factory to generate EJBObjects for the requesting clients. There is just one instance of type EJBHome created by a container corresponding to an EJB. This single home object instance is accessed by all clients of that EJB. • The home interface is also responsible for the lifecycle management of beans. When a client requests for a bean, the EJBHome object instantiates the bean. When the client request is serviced and wants to remove the bean, the EJBHome object removes the bean from the container.

  14. EJB Client package mypack.Hello; import javax.ejb.*; import javax.naming.*; import java.rmi.*; import java.util.Properties; public class HelloClient { public static void main(String[] args) { try { /* Get System properties for JNDI initialization */ Context ictx=new InitialContext(); Object objref=ictx.lookup(“java:comp/env/ejb/HelloTeller”); HelloHome home= (HelloHome) PortableRemoteObject.narrow(objref,HelloHome.class); Hello helloref = home.create(); System.out.println(helloref.SayHello()); helloref.remove(); } catch (Exception e) {e.printStackTrace();} } }

  15. The code is a simple Java application called HelloClient. • A new instance of InitialContext has been created to search (lookup) for a Home object. • Next line of code looks for the location of the home object. • Notice that the initial context begins “java:comp/env/”, which is the initial context for JNDI. • In the code, ejb is the subcontext under the initial context, where all the ejb references are stored and HelloTeller is the nick name of bean. The object that we look for is stored in a variable called objref, which is of the datatype Object. • Object is a super class of any class in Java. Any object or application written in Java is a child of a super parent called Java. Next, the generic datatype is narrowed to a more specific datatype by using an API in Java called reflection. • This API takes two parameters, source and destination, which are object and hellohome respectively. • This is then typecased to hellohome. Then, the create method in the home object is executed and the value is returned/stored in a component object, helloref. helloref is of the datatype hello, which in turn is the datatype of the component interface. • The return value is printed on the console. Finally, the object helloref is eliminated using the remove method.

  16. Client’s View (1 of 2) • The client views the container that contains the home object and remote object. • Client view consist of the following • Container • Home Object • Remote Object

More Related