1 / 114

Enterprise Java Beans

Enterprise Java Beans. Architectural Overview. What is EJB. Server side components that are Developed in Java. Solve business/enterprise requirements Can be designed for remote access Designed to receive the standard services from the J2EE application servers

lramos
Download Presentation

Enterprise Java Beans

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. Enterprise Java Beans Architectural Overview

  2. What is EJB Server side components that are Developed in Java. Solve business/enterprise requirements Can be designed for remote access Designed to receive the standard services from the J2EE application servers Follow the EJB specification published onhttp://java.sun.com/products/ejb/docs.html

  3. What is EJB The difference between java beans and enterprise java beans is as follows: Java beans are client side. (don’t have in built RMI facility) Java beans have 2 types. Visible (like activex controls) and non visible. While EJB’s are always server side (hosted on application server and consume services from it). EJB specification gives built in RMI facility to EJB’s because of which they can be called remotely according to the requirement.

  4. What is EJB An EJB is a collection of Java classes, following defined rules and providing specific call-back methods, and an XML file, combined into one single unit. An Enterprise Bean is a server-side software component that can be deployed in a distributed multi-tier environment.

  5. The Contents of an Enterprise Bean To develop an enterprise bean, you must provide the following files: Deployment descriptor: An XML file that specifies information about the bean such as its persistence type and transaction attributes. The deploy tool utility creates the deployment descriptor when you step through the New Enterprise Bean wizard. Enterprise bean class: Implements the methods defined in the interfaces. Interfaces: Helper classes: Other classes needed by the enterprise bean class, such as exception and utility classes.

  6. Packaging The EJB You package the files in the preceding list into an EJB JAR file, the module that stores the enterprise bean. An EJB JAR file is portable and may be used for different applications. To assemble a J2EE application, you package one or more modules—such as EJB JAR files—into an EAR file, the archive file that holds the application. When you deploy the EAR file that contains the bean’s EJB JAR file, you also deploy the enterprise bean onto the J2EE server.

  7. Types of Enterprise Beans Session EJB’s contain the business logic which is complex and specific to the domain. For ex: Calculating interest Entity beans always represent a business entity like account/customer. Because these entities need a permanent existence, every entity bean will be corresponding to one or more tables in database as well. For ex: Account bean will have an account table mapped to it. Every instance of account bean will map to one record of account table and the updation in the bean updates the table as well

  8. Session Bean Vs Entity Bean Entity Bean SessionBean Represents a business entity object that exists in persistent storage Performs a task for a client Persistent. Not persistent.

  9. Session Bean Session should usually not access the database. Except for the following situation The application is relatively simple The data does not represent a business entity (e.g. rate) For example, if the Calculate interest method needs interest rate from the database, then it is ok to access the database from session bean because rate is not a complete business entity.

  10. Entity bean Entity Bean should usually access the database except in the following situation The data represents a business entity You want to hide the relational model from the session bean Normally database access should happen through entity bean. Because of this the session bean contain pure business logic and call the entity beans for database access. So they remain more reusable even for the similar logic with different database structure.

  11. Message Beans Message-Driven Beans (EJB 2.0) Asynchronously Only a bean class – no interfaces Acts as a listener for the Java Message Service API, processing messages asynchronously

  12. Contracts in terms of EJB’s EJBS are to be developed and deployed according to the EJB specification published on Sun’s site. The rules mentioned in this specification are called as contracts Types of contracts are negotiable contracts non-negotiable contracts

  13. Types of Contracts Some rules are mandatory for the creating a working EJB and some rules are given as a guideline, which can be followed for a better maintainable EJB. The thumb rules are called as non-negotiable contracts and the other ones are negotiable contracts. Example : non-negotiable contract is having a home interface for the EJB &negotiable contract is that Session EJB’s should not make JDBC calls and Entity beans should not contain business logic.

  14. Minimum Program Requirements Remote Interface - Specifies the beans business methods Home Interface - Defines the beans life cycle methods Bean class - Implements the beans business methods Client class - Provides a pointer into the database It is must for a EJB to have a remote interface and a home interface along with the bean class. The client class is used to test the EJB.

  15. A Beans’ Interface Remote Interface Business methods to do the beans work Implemented by the “shadowdy” Bean Object Remote Home Interface Defines the beans life cycle methods Implemented by EJB Home Local Interface (EJB 2.0) Business methods used by other beans in the same container Local Home Interface (EJB 2.0) Life-cycle methods used by other beans in the same container

  16. Home Interface Life cycle management is a service that one must take from application server for the EJB components. That’s why the programmer will never implement any method related to life cycle management of the EJB’s. Programmer uses home Interface to convey the names of life cycle related methods and their signatures to the EJB container and let the ejb container implement the same. Business methods cannot be part of and this home interface. Lifecycle related methods include create PostCreate Activate Passivate Remove Etc.

  17. Remote Interface Contains declarations of business methods like calculateInterest() etc. This interface will be indirectly implemented by the bean by giving implementations of the methods declared in this interface. It cannot contain life cycle related methods

  18. Remote Interfaces

  19. Is it must to have Home interface YES. Having Home interface with appropriate declarations of life cycle methods is a non- negotiable contract of EJBs. Home interface exposes the create method to the client. The create method creates/locates actual EJB instance. Unless you call create method you cannot get handle to EJB instance. And for the purpose of calling create method we need handle to Home interface first of all.

  20. The Bean Class Implement the business methods Do not implement Remote (or local) and (local) Home Interfaces Beans exist in the middle of client software and data sources Clients never interact directly with the bean class, uses methods of the Remote and Home Interface, interacting with automatically generated stubs

  21. EJB Subtypes Entity Beans Represents a business object in a persistent storage mechanism Can be shared by multiple clients Two types of persistence: Container-managed Bean-managed

  22. EJB Subtypes Container-managed persistence They are the simplest to develop The bean’s code contain no database access calls Bean-managed persistence Explicitly write persistence logic More flexibility in how state is managed between the bean instance and the database Used when deployment tools are inadequate

  23. Entity Bean Types CMP Entity Beans- Container Managed Persistence A type of Entity beans that receive persistence service from the EJB container. Because of this service CMP beans need not use JDBC statements but the container will manage the persistence. CMP entity beans do not have handwritten JDBC statements

  24. Entity Bean Types.. BMP Entity Beans: Bean Managed Persistence A type of entity bean where the developer of the entity beans makes JDBC calls to persist the data in the entity bean BMP entity beans are also chosen to represent those tables which are not standard in their structures or which d o not have standard relationships with other tables. BMP entity beans have handwritten JDBC statements

  25. EJB Subtypes Session Beans Useful for describing interactions Does not represent shared data in the database, but can access shared data Two types: Stateless Stateful

  26. Session Beans Types Stateless Session Beans- a type of EJB that doesn’t need session management service State full Session Beans- a type of EJB that needs session management service from the EJB container In Stateless session beans , create method should not take any parameter. This is non-negotiable contract In stateful bean, create takes parameters because the members of the instance of this bean are specific to one client.

  27. Differences in types of Session Beans

  28. Differences in types of Session Beans..

  29. Stateless Session Beans Supports multiple clients Relatively easy to develop and very efficient Require few server resources Stateless session beans are appropriate if: The bean's state has no data for a specific client A generic task is performed in a single method invocation The bean fetches a set of read-only data

  30. Stateful Session Beans Dedicated to one client for the life of the bean instance Instance variables represent the state of a unique client-bean session Stateful session beans are appropriate if: The bean needs to hold information about the client across method invocations The bean mediates between the client and the other components of the application

  31. EJB Subtypes When to use Message Driven Beans To receive messages asynchronously When consuming JMS messages Message-Driven Beans Has only a bean class Can consume and process messages concurrently Acts as a JMS message listener Deliver messages to a virtual channel Currently process only JMS messages

  32. Message Beans In several respects, a message-driven bean resembles a stateless session bean. A message-driven bean’s instances retain no data or conversational state for a specific client. All instances of a message-driven bean are equivalent, allowing the EJB container to assign a message to any message-driven bean instance. The container can pool these instances to allow streams of messages to be processed concurrently. A single message-driven bean can process messages from multiple clients.

  33. Message Beans The instance variables of the message-driven bean instance can contain some state across the handling of client messages—for example, a JMS API connection, an open database connection, or an object reference to an enterprise beanobject. When a message arrives, the container calls the message-driven bean’s onMessage method to process the message. The onMessage method normally casts the message to one of the five JMS message types and handles it in accordance with the application’s business logic.

  34. Message Beans The onMessage method may call helper methods, or it may invoke a session or entity bean to process the information in the message or to store it in a database. A message may be delivered to a message-driven bean within a transaction context, so that all operations within the onMessage method are part of a single transaction. If message processing is rolled back, the message will be redelivered.

  35. Bean Class Bean Class contains implementations of the methods declared in remote interface Call back methods like ejbCreate() which will be called by the container whenever the create is executed internally by the container, you can do initialization type work in these methods Must implement interface SessionBean /EntityBean for indicating the type of the Bean

  36. 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()");) } Example of Bean Class

  37. public void ejbPassivate() { System.out.println("ejbPassivate()"); } public void setSessionContext(SessionContext ctx) { System.out.println("setSessionContext()"); } public String sayHello(String name) { System.out.println("hello()"); return ("Hello"+name+"!"); } }

  38. Entity bean components

  39. Entity Bean Components Remote, Home interface are always required on client side In case of entity beans, a primary key which is sent from client to server to find out the correct record has to be an object of some class. It cannot be a basic data type . If it object of Integer class , this class is obviously available to the Client because it’s a part of JDK. But in case of composite primary keys , they are represented using a user defined class. E.g. AccountPK In such case,class AccountPK is instantiated by client and hence needs to be present on client side as well

  40. Sequence Diagram for Session Bean : EJBHome : EJBObject : Container : Bean : Client Instance 1: new 2: setSessionContext() 3: ejbCreate() 4: create() 5: new 6: assign bean instance 7: business method 8: business method Container creates the instance pool for the session bean even before the call is made for the bean. The pool size is configurable during deployment.

  41. Sequence Diagram for Entity Bean : Client : EJB Home : EJB Object : Container : Bean : Database Instance 1: class.newInstance() 2: setEntityContext() 3: create() 4: ejbCreate() 5: insert record 6: class.newInstance() 7: assign bean instance 8: ejbPostCreate() 9: business method 10: business method 11: remove 12: ejbRemove() 13: delete record 14: unsetEntityContext()

  42. Stateless Session Bean- Remote Interface import javax.ejb.*; import java.rmi.*; public interface SimpleInterest extends EJBObject { public double SI(int p,int r,int t) throws Remote Exception; }

More Related