Sunteți pe pagina 1din 13

Module 5 EJB Basics of EJB Types of Beans Development of Session Beans Steps Creating & Implementing Interfaces Writing

riting Deployment descriptors Packaging and deploying bean - using the bean from a client Development of stateful session bean. Entity beans Features (Basics of developing and using entity beans) What is meant by Enterprise Java Beans (EJB) ? Enterprise JavaBeans (EJB) is a server side component architecture that enables and simplifies the process of building enterprise class distributed object applications in Java Component Architectures There are a number of tools that are available for developing components like IBMs VisualAge for Java, Symantecs Visual Caf and Borlands JBuilder The containers provide a runtime environment for the components to play in and also provides a set of common services that components will need The component vendors supplys a set of tools to aid in the deployment and maintenance of the components like customization for a particular environment JavaBeans and EJBs JavaBeans are development components and are not deployable JavaBeans is not deployable because JavaBeans is not a complete application JavaBeans do not need a runtime environment in which to live JavaBeans can have a container or not EJBs are deployable components EJBs are complete applications EJBs need a runtime environment to live EJBs need a container Applets, Servlets and EJBs Applets can be deployed in a Webpage and the browsers appletviewer provides a runtime container for the applet Servlets can be deployed in a Web Server and the web servers servlet engine provides a runtime container for the servlet Enterprise Java Beans can be deployed in a Application Server and the application server provides a runtime container for the Enterprise Java Beans The difference between applets, servlets and enterprise java beans is the domain of which each component type is intended to be a part Applets are portable java programs that can be downloaded and can execute in an untrusting environment so that an applet can be downloaded from a Web server into a Web browser deployed in a Webpage and the browsers applet Servlets are request/response oriented so that they take requests from client such as Web browser and issue a response back to that host Applets and Servlets are well suited to handle client side operations performing presentation related logic and lightweight business logic operations Enterprise Java Beans are server side components that are meant to

perform server side operations such as executing complex algorithms or performing high volume business transactions Server side components needs to run in a highly available, fault tolerant, transactional and multiuser secure environment An application server provides highend server side environment for the enterprise java beans and provides the runtime containment necessary to manage enterprise java beans Multi-tier Architecture Any well written software deployment has a logical partitioning into layers Each layer has a different responsibility in the overall deployment Within each layer there can be one or more components Each layer has a different responsibility in the overall deployment A layered system is a well designed system because each layer is responsible for a separate task

Presentation layer

Business logic layer

Data layer

The presentation layer contains components dealing with user interfaces and user interaction The presentation layer of a web based deployment could use java servlets, Java server pages and Java applets The presentation layer contains components that work together to solve business problems The components are written in a high level language like Java The data layer is used by the business logic layer to persist state permanently Central to the data layer is one or more database that house the stored data The advantage of partitioning an application into logical layers is to isolate each layer from the other so that it will be possible to plug in a different view By introducing the layer partitioning it will be possible to plug in a different set of business rules or to plug in a different database in the data layer with relatively minor effects on other layers The physical separation of the layers can be changed according to the needs There is two tier architecture, three tier architecture and even n tier architecture according to the use

In a two tier architecture either the business logic is combined with the presentation layer or the business logic is pushed into the data layer There are many advantages in pushing business logic into the data layer like saves many round trips, increases the speed of database operations, reduces total network traffic etc. Server Side Component Architecture Solutions As the multi tier server side deployments surfaced multitudes of application servers have begun to appear on the market The application servers provide a usable runtime environment in which components can execute and provide the needed services As a number of server side components appeared in the market the need for a standard architecture for server side components has arisen The standard architecture needs to craft a well formed interface between the application server which contains the components and the component themselves The most popular standards are Sun Microsystems Java 2 Platform Enterprise Edition (J2EE) Microsofts Distributed InterNet Application Architecture The Object Management Groups CORBA Standard The OMGs Common Object Request Broker Architecture (CORBA) is a specification and not a product The distinction between CORBA and J2EE is that CORBA has only the notion of an object and has no notion of deployable server side component that is automatically managed by an application server Java 2 Platform Enterprise Edition (J2EE) EJB is an integral part of Java 2 Platform Enterprise Edition Sun Microsystems first focused on building a robust Java Development Kit (JDK) which became the de facto reference implementation of the Java platform Sun Microsystems then recognized the power of Java on the server and began to develop several Enterprise APIs that provided enterprise level services for server side deployments These services served as great feedback to sun because they highlighted many problems with the Enterprise APIs and that lead to the development of Enterprise Java Beans J2EE takes Javas Enterprise APIs and bundles them together in a complete development platform for enterprise class server side deployments in Java J2EE addresses all the problems raised by Suns Enterprise APIs like poor synchrony between Enterprise APIs , no way to test application server compatibility, no reference implementations J2EE provides a test suite for vendors to test their products and if the product passes the test the product will be called J2EE compliant The technologies included with J2EE is shown in the following figure

Java Data Base Connectivity

Java Remote Method Invocation

Java Naming and Directory Interface

Enterprise Java Beans

Java Transaction API and Java Transaction Service Java Mail

Java 2 Enterprise Edition

Java Server Pages

Servlet

Java IDL

Connectors

Java MessagingS ervice

EXtended Markup Language

Java Remote Method Invocation (JRMI) RMI is a mechanism for invoking methods remotely on other machine JRMI is seamless so that a user will not know that he is using RMI when programming a distributed application JRMI abstracts networking issues from the user such as marshalling parameters, handling machine byte order which are all necessary for network communications JRMI also contains other functions like automatic activation of remote objects and a distributed garbage collector to clean up unused remote objects Java Naming and Directory Interface (JNDI) EJB relies on JNDI for looking up distributed components across the network JNDI controls the directory service that stores information about where resources reside and other information such as user name and passwords In EJB when client code requests access to a component, directory services are used to locate and retrieve a component to service that client There are many types of directory services and is a big problem in switching to different directory services and JNDI solves the problem Java Database Connectivity (JDBC) JDBC is a standard Java extension for data accessRMI is a mechanism for invoking methods remotely on other machine Using JDBC programmers can represent database connections, issue SQL statements, process database results in a relatively portable way JDBC is similar to Open Database Connectivity standard and the two are interoperable through JDBC-ODBC bridge JDBC contains built in support for database connection pooling Java Transaction API (JTA) and Java Transaction Service (JTS) A transaction is a unit of work that makes a set of guarantee about its execution

Any code executing within the scope of a transaction is guaranteed either to all be executed or not to execute at all Transactions allow for multiple users to be modifying the same data yet each to be isolated from one another JTA is a high level transaction interface that is used to control transactions JTS is a set of low level transaction interfaces that is used behind the scenes and the client code do not directly interact with JTS JTS makes it possible for multiple vendors to collaborate when performing in a distributed heterogeneous environment Enterprise Java Beans strictly depends on JTA but it does not depend on JTS Java Messaging Service (JMS) A messaging service allows for distributed objects to communicate in an asynchronous reliable manner Processes can respond to messages at their leisure and they could potentially be offline when a message is initially sent Java Messaging Service can be used as a portable messaging service Java Servlets Servlets are networked components that can be used to extend the functionality of a Web server Servlets are request response oriented in that they take from client host and issue a response back to that host Servlets differ from EJB components is that the server side component features that EJB offers is not readily available to servlets Servlets are suitable to handle simple request response needs and they do not require sophisticated management by an application server Java Server Pages (JSP) Java Server Pages are very similar to servlets and JSP scripts are compiled into servlets JSP scripts are not pure Java code but more centered around look and feel issues JSP can be easily written and maintained by non Java programmers JSP do not require a Java compiler Java IDL Java IDL is an implementation of the CORBA specification in Java and it allows for connectivity and interoperability with heterogeneous objects CORBA is a cross platform architecture CORBA is language independent and it does not matter what the programming language is as long as CORBA supports the language in which the program is developed Java IDL is one specific implementation of CORBA among many such implementations JavaMail JavaMail API allows applications to use E-mail capabilities JavaMail defines a set of interfaces to which the application code is written and those interfaces shield the code from specific protocols or mail service implementations used

The Internet mail code becomes portable across platforms as well as across mail protocols JavaMail depends on the JavaBeans Activation Framework (JAF) to encapsulate message data and to handle interactions with that data Connectors A connector is a vendor specific bridge that links an existing system to the Java 2 Platform Enterprise Edition The existing system could be ERP systems, mainframe systems such as IBMs CICS or existing legacy databases Connectors are useful because they will take care of navigations to existing systems, handling transaction and security concerns The connectors are different from the other J2EE APIs because connectors are very vendor specific The Six Parties EJB partitions the responsibility of an EJB development to six different parties and they are The Bean Provider The Container Provider The Server Provider The application assembler The deployer The System Administrator The Bean Provider The Bean Provider is the party who supplies enterprise beans components having methods for performing application logic The Bean Provider is a vendor of components The Bean should be designed correctly to be reusable considering the different applications, domains and users of the Bean Developing a reusable bean will require many iterations depending on the feedback from the customers using the Bean Bean reusability fall into three different levels Reuse as given The application assembler uses the bean as it is to build an application The Bean functionality cannot be tailored to fit the application Reuse by customization The application assembler configures the bean by modifying the bean properties to fit the specific needs of the application Reuse by extension (subclass) The application assembler creates custom application specific bean by subclassing the prebuilt acquired beans Reuse by extension is more powerful but difficult to achieve and is only provided by very few providers The more reusability a bean provides the more useful a bean is Enterprise beans can range in size and scope like smaller grained enterprise beans which are very concrete with limited scope and duties and larger grained enterprise beans

A bean can be a client of another bean eg. Credit card transaction

Client Bean for credit card scanner

Server Bean for processing transactions

Verifier Bean

The Container Provider The EJB container provides a playground for the Enterprise Java Beans to run There can be many Beans running with in a container Bean containers are responsible for managing the beans running within them The Container interact with the beans by calling a few required methods that the bean must expose The Containers can also provide access to legacy systems The Server Provider An application server provides services for the application such as transaction services, security services etc. The services are needed for the application to be scalable, robust and secure for multiple concurrent users The EJB server provides a runtime environment for one or more containers EJB servers manage low level system resources allocating resources to containers as they are needed The relationship between EJB servers and EJB containers

EJB Server EJB Container 1 EJB Container 2

Enterprise Java Bean1

Enterprise Java Bean3

Enterprise Java Bean2

Enterprise Java Bean4

EJB Server and container responsibilities The Servers and containers provide both implicit services and explicit services The implicit services include

o Implicit distributed transaction management o Implicit security o Implicit resource management and component life cycle o Implicit persistence o Implicit remote accessibility o Implicit multiclient support o Implicit component location transparency The explicit services include Naming and directory service allowing components to be located across the network A set of deployment tools that allows the EJB deployer to deploy components into the EJB server and customize the components as needed Intelligent load balancing Server clustering Connectors to legacy systems like SAP The Application Assembler The application assembler assembles the reusable deployable server side components to solve a business problem The application assembler can be a staff or an outside consultant who understands the complete application system and know how the various components fit together The tasks that an application assembler should perform are 1. Write the code that calls on components purchased from vendors 2. Provide a workflow solution between a number of disparate components mapping between them 3. Supply an user interface if needed ( using JSP or servlets ) 4. Write new enterprise beans to solve domain specific problems eg. Business entity that is specific for a particular business The application assembler should be involved in the design and implementation of logic residing in several tiers in multi tier architecture The EJB Deployer The application assembler may not be familiar with the specific operational environment that the application must run in The EJB deployers are aware of specific operational environment and know how to deploy beans within servers and how to customize the beans for a specific environment The EJB deployer has knowledge of a customers existing naming and directory services and understands how to customize enterprise Beans for that scenario Another function of EJB deployer is mapping security settings The Business stores the list of their employees and their security levels in some directory service structure and the EJB deployer must adapt the access level of the beans to fit the particular environment The EJB deployers must be aware of the differences between the various beans, servers and containers available in the market

The EJB deployer can be also a staff or an outside consultant The System Administrator The system administrator is responsible for the upkeep and monitoring of the deployed system and make use of runtime monitoring and management tools that the EJB server and containers provide Good EJB deployments might page the system administrator if a serious error occurs that require immediate attention Some EJB Products have hooks into professional monitoring products for helping system administrator Enterprise Java Beans An Enterprise Java Beans is a server side software component that can be deployed in a distributed multi tier environment The client of a enterprise java beans can be a servlet, an applet or even another enterprise java bean The client request to a bean can result in a whole chain of beans being called resulting in subdividing a complex task into subtasks handled by simple beans There are two different kinds of enterprise java beans the session beans and the entity beans Session Beans Session beans are business process objects that implements business logic, business rules and workflow Session beans are reusable components that contain logic for business processes A session bean can perform price quoting, order entry, video compression, banking transactions, stock trades, complex calculations etc. Session beans are called session beans because they live for about as long as the session of the client code that is calling the session bean exists Session beans are usable by one client at a time and they are not shared between clients The EJB container is responsible for managing the lifetime of beans The container destroys session beans at the appropriate time There are two subtypes of session beans the stateful session beans and stateless session bean Stateful Session Beans Some business processes can be performed in a single method request other business processes can last across multiple method requests and transactions A stateful session bean is a bean that is designed to service business processes that span multiple method requests or transactions The stateful session beans retain state on behalf of an individual client so in a stateful session beans state is changed during a

method invocation the same state will be available for the client in the following invocation Stateless Session Beans A stateless session bean is a bean that is designed to service business processes that have a single request A single request business process does not require state to be maintained across method invocations An example for stateless session bean is video compression where the client could pass in a buffer of uncompressed data as well as a compression factor and the bean would return a compressed buffer and the bean does not retain any state Another example is a credit card verification component Entity Beans An Entity bean is a component that represents persistent data Entity beans represent real data objects such as customers, products employees, bank accounts, orders, stock portfolios etc. Entity bean do not contain business process logic rather they model data Session beans use entity beans to represent the data session bean uses The Entity bean is an object oriented in memory view of a database It is possible to read an entire set of data out of a database at once into an in memory entity bean component and then manipulate this entity bean by calling methods on it An entity bean implements the data access logic layer in a multi tier Architecture. EJB Container Permanent Storage

Client code such as applet

Entity Bean

Entity bean survive critical failures such as application server crashing because entity beans are representation of data in a permanent database Entity beans have a life cycle much longer than Session beans even years depending on how long data sits in the database Multiple clients can use Entity beans simultaneously One client can isolate from another by using transactions Transactions guarantee multiple clients to perform persistent operations independently of one another Entity beans can be used with legacy database where data is already inside The data that entity beans model could exist in a database before a company decided to employ Enterprise Java Beans

Database records can be read in and interpreted as objects in almost any language and EJB takes advantage of this and allows for the transformation of a databases data into Java object There are two subtypes of entity beans the bean managed persistent entity beans and container managed persistent entity beans Bean Managed Persistent Entity Beans Entity beans are persistent components because their state is saved to a secondary storage such as a relational database A bean managed persistent entity bean is an entity bean that must be persisted by the component developer The component developer must write code to translate the in memory fields into an underlying data store such as a relational database The component developer must handle the persistent operations including saving, loading and finding data within the entity bean Container Managed Persistent Entity Beans In Container managed persistent entity bean the container provides an automatic persistence for the entity beans The container performs every function including saving loading and finding component data The developer just describe what should be persisted to the container and it will persist for the developer The container gives theoretical database independence allowing the developer to switch one data store for another The EJB containers perform a wide variety of mapping functionality Motivation for Multiple Bean Types Why EJB offers various kinds of beans Sun is not the only company involved in constructing the Enterprise Java Beans standard Many companies have been involved each with customers that have different kinds of distributed systems and to accommodate the needs of different enterprise applications sun decided for different kinds of beans By offering various kinds of beans EJB provides increased functionality Session Beans versus Entity Beans How to decide what to put in a session bean and what to put in an entity bean The key difference between the two are that session bean represent business processes and business logic and entity bean holds permanent business entities such as businesses data and data related logic Consider an example where a customer wants to order 15 workstations The pricer component is represented by a session bean, after the price is calculated the pricer is free to service another client

When the customer places the order an order entity bean is used to put up the order The order is fulfilled by another session bean that contains the logic to do so Session beans model a current business process which can be tuned with different algorithms and approaches Entity bean defines the core business data such as purchase orders, customers and bank accounts that do not change very much over time In practice entity beans achieve a higher level of reuse Session beans never embody permanent data but merely provide access to data Session beans are not persistable they represent business processe Entity beans embody data and are persistable EJB Container Responsibilities The most important responsibility of an EJB container is to provide an environment in which enterprise beans can run EJB containers house the enterprise beans and make them available for clients to invoke remotely The container is responsible for connecting clients to beans, performing transaction coordination, providing persistence, managing a beans life cycle etc. EJB containers are a huge challenge to write because they are so complex and perform so many tasks The features an EJB container provides are Resource Management and Bean Life Cycle Management State Management Transactions Security Persistence Remote Accessibility and Location Transparency Glue Code and Bean Installation Tools Specialized Container Features Resource Management and Bean Life Cycle Management The container is responsible for providing all resource management services The resources include threads, socket connections, database connections and more The EJB container is also responsible for controlling the life cycle of the deployed enterprise bean components As client request arrive the EJB container dynamically instantiates, destroys and reuses beans as appropriate If a client request a certain type of bean that does not exist in memory the EJB container may instantiate a new in-memory instance in behalf of the client If a new bean has to be instantiated and the system is low in memory

then the container can destroy some beans that are not being used anymore and this is called instance pooling The EJB container is responsible for coordinating the entire effort of resource management as well as managing the deployed beans life cycle State Management A bean can be a stateless bean or a stateful bean A stateless bean can be reassigned to other clients dynamically by the EJB container because there is no state lost from the primary client The reuse of beans results in incredible resource gains If a client times out the EJB container destroys the bean or reuse it In a stateful bean the EJB container should do the state management to service multiple clients Entity beans are inherently stateful because they represent data Consider the case when a new client request a component and the container has reached the limit The container can take a component that hasnt been used for a while and write the state out to disk and the bean can be reassigned to a different client When the original client makes a request the original clients bean state can be read back in from disk in a different in-memory bean object and use it again Transactions A transaction is a series of operations that appear to execute as one large atomic operation Transactions allow multiple users to share the same data and they guarantee that any set of data they update will be completely and wholly written Transactions ensure that database data is kept consistent The EJB container handles the underlying transaction operations coordinating efforts behind the scenes between transaction participants

S-ar putea să vă placă și