38
Enterprise Java Beans Sparleanu Radu Bucur Ioan Gabriel

Enterprise Java Beans - cs.ubbcluj.roamarcus/cbdmw06/Lectures/EJB Presentation.pdfThe Motivation for EJB • Possible problems in case of a distributed system with multiple clients

Embed Size (px)

Citation preview

Enterprise Java Beans

Sparleanu RaduBucur Ioan Gabriel

What is EJB

• EJB is a server-side component architecture that simplifies the process of building enterprise-class distributed component applications in Java

• EJB is about rapid application development for the server side

• By using EJB, you can write scalable, reliable, and secure applications without writing your own complex distributed component framework.

The Motivation for EJB• Possible problems in case of a distributed system with multiple clients connecting to

multiple servers and databases over a network1. Remote method invocations2. Load balancing3. Transparent fail-over4. Back-end integration5. Transactions6. Clustering7. Dynamic redeployment8. Clean shutdown9. Logging and auditing10. Systems Management11. Threading12. Message-oriented middleware13. Object life cycle14. Resource pooling15. Security16. Caching

The application server

• Was born to let you buy these middleware services, rather than build them yourself

• Provides you with common middleware services

• Allows you to focus on your application and not worry about the middleware you need for a robust server-side deployment

• Divide and conquer

Introducing Enterprise JavaBeans

• EJB standard is a component architecture for deployable server-side components in Java

• EJB is an agreement between components and application servers that enable any component to run in any application server

• EJB components (called enterprise beans) are deployable, and can be imported and loaded into an application server, which hosts those components

Values of EJB

• It is agreed upon by the industry• Portability is easier. The EJB specification

is published and available freely to all• Rapid application development. Your

application can be constructed faster because you get middleware from the application server

EJB as a Business Solution• EJB is specifically used to help solve business

problems. EJB components might perform any of the following tasks

1. Perform business logic2. Access a database - JDBC3. Access another system – Java Connector

Architecture (JCA)• EJB components are not GUI components;

rather, enterprise beans sit behind the GUIs and do all the hard work

GUIs that can connect to enterprise beans

• Thick clients - execute on a user’s desktop. They could connect via the network with EJB components that live on a server (Java applications or applets)

• Dynamically generated web pages - Both servlets and JSPs live within a Web server and can connect to EJB components, generating pages differently based upon the values returned from the EJB layer

• XML-based Web Service wrappers - Some business applications require no user interface at all. They exist to interconnect with other business partners’ applications that may provide their own user interface

The EJB Ecosystem• EJB encourages collaboration of more than six different

parties. Each of these parties is an expert in its own field and is responsible for a key part of a successful EJB deployment

1. The Bean Provider - supplies business components, or enterprise beans. Enterprise beans are not complete applications, but rather are deployable components that can be assembled into complete solutions

2. The application assembler - is the overall application architect, is responsible for understanding how various components fit together and writes the applications that combine components

The EJB Ecosystem II3. The EJB Deployer - deploy beans within servers

and customize the beans for a specific environment

4. The System Administrator – oversees the stability of the operational solution

5. The Container and Server Provider - supplies an EJB container (the application server). This is the runtime environment in which beans live

6. The Tool Vendors - To facilitate the component development process, there should be a standardized way to build, manage, and maintain components

The EJB Ecosystem III

EJB Fundamentals• EJB technology is based on two other

technologies: Java RMI-IIOP and JNDI• An enterprise bean is a server-side software

component that can be deployed in a distributed multitier environment

• The EJB specification requires that your beans expose a few required methods; these required methods allow the EJB container to manage beans uniformly, regardless of which container your bean is running in

Types of Beans

• Session beans - model business processes. They are like verbs because they are actions

• Entity beans - model business data. They are like nouns because they are data objects—that is, Java objects that cache database information.

• Message-driven beans - are similar to session beans in that they are actions. The difference is that you can call message-driven beans only by sending messages to those beans

Clients interacting with an EJB component system

Implicit middleware• Write your distributed object to contain only business

logic• Declare the middleware services that your distributed

object needs in a separate descriptor file• Run a command-line tool provided for you by the

middleware vendor. This tool takes your descriptor file as input and generates an object - the request interceptor

• The request interceptor intercepts requests from the client, performs the middleware that your distributed object needs (such as transactions, security, and persistence), and then delegates the call to the distributed object

Implicit middleware II

What Constitutes an Enterprise Bean?

1. The Enterprise Bean Class = Java class that conforms to a well-defined interface and obeys certain rules

• implements tagging interface : javax.ejb.EnterpriseBeanjavax.ejb.SessionBeanjavax.ejb.EntityBeanjavax.ejb.MessageDrivenBean

What Constitutes an Enterprise Bean? II

• 2. The EJB Object - Enterprise beans are not full-fledged remote objects. The invocation to a bean instance is intercepted by the EJB container and then delegated to the bean instance

• Implicit distributed transaction management• Implicit security• Implicit resource management and component life cycle• Implicit persistence• Implicit remote accessibility• Implicit support• Implicit component location transparency• Implicit monitoring

The Remote Interface

• EJB objects must clone every business method that your bean classes expose

• javax.ejb.EJBObject - the EJB container implements its methods when it autogenerates the EJB objects for you

• Every method of javax.ejb.EJBObjectthrows a java.rmi.RemoteException

What Constitutes an Enterprise Bean? III

3. The Home Object - To acquire a reference to an EJB object, your client code asks for an EJB object from an EJB object factory. This factory is responsible for instantiating (and destroying) EJB objects. The EJB specification calls such a factory a home object

4. The Home Interface - defines methods for creating, destroying, and finding EJB objects. The container’s home object implements your home interface

What Constitutes an Enterprise Bean? IV

5. Deployment Descriptors - To inform the container about your middleware needs, you as a bean provider must declare your components’ middleware service requirements in a deployment descriptor fileIn EJB 2.0, a deployment descriptor is an XML file

6. Vendor-Specific Files - Since all EJB server vendors are different, they each have some proprietary value-added features

7. Ejb-Jar File - is a compressed file that contains all the needed files, and it follows the .ZIP compression format

EJB-jar structure

Writing a simple Bean• Write the .java files that compose the bean• Write the deployment descriptor• Compile the .java files from step 1 into .class

files• Create an EJB-jar file containing the deployment

descriptor and the .class files• Deploy the EJB-jar file into the container• Configure the EJB server to host the EJB-jar• Start the EJB container and verify that it loaded

the EJB-jar file

Hello World Object Model

Home Interfaces and Objects

The Client Side

• Types of clients:– Java RMI-IIOP based clients – use JNDI to

look for objects over the network and they use Java Transaction API (JTA) to control transactions;

– CORBA clients – use CORBA Naming Service (COS Naming) to look up objects over the network, and they use the CORBA’s Object Transaction Service (OTS) to control transactions.

How to Call a Bean

• Look up a home object• Use the home object to create an EJB

object• Call business methods on the EJB object• Remove the EJB object

Acquiring a reference to a home object

Types of beans

• Session beans– Stateless session beans;– Stateful session beans.

• Entity Beans– Bean-managed persistent entity beans;– Container-managed persistent entity beans.

• Message-driven beans

Session Beans

• Session beans are business process objects

• They implement business logic, business rules, algorithms

• They are reusable components that contain logic for business processes

• Session beans are nonpersistent(opposed to entity beans)

Session Beans Subtypes

• All enterprise beans hold conversations with clients at some level

• A conversation is an interaction between a client and a bean, and it is composed of method calls between the client and the bean

1. Stateful session beans– Retain state on behalf of an individual client– E-commerce web site (shopping cart)

2. Stateless session beans– Do not hold multimethod conversations with their

clients– Credit card verification component

Activation / Passivation

• To limit the number of stateful session beans instances in memory, the container can swap out a stateful bean, saving its conversational state to a hard-disk or other storage (passivation)

• When the original client invokes a method, the passivated conversational state is swapped in to a bean (activation).

Entity Beans

• They are persistent objects that can be stored in permanent storage

• Entity beans are persistent data components• They can persist themselves in many ways

(serialization, object-relational mapping or object database persistence)

• The primary key class makes every entity bean different

• Entity beans instances are a view into a database

Ways to Persist Entity Beans

• A bean managed persistent entity bean is an entity bean that must be persisted by hand (the component developer must write code to translate the in-memory fields into an underlying data store).

• Container-managed persistence is the alternative. In this case, the bean does not contain persistence logic and you inform the container how you would like to be persisted by using the container’s tools.

Message-Driven Beans

• Special beans that can be accessed via messaging (only from EJB 2.0)

• Messaging is an alternative to RMI. A middleman sits between the client and the server, and receives messages from message producers and broadcasts those messages tomessage consumers.

• The Java Message Service (JMS) is a messaging standard. It has two parts:– An API to write code to send and receive messages;– A Service Provider Interface (SPI) where you plug in

JMS drivers.

Client View of a JMS System

What is a Message-Driven Bean?

• A message-driven bean consumes messages that are sent by any valid JMS client

• It does not have a home interface, local home interface, remote interface or local interface

• They have a single business method (onMessage())

• A message-driven bean does not return a value and it is stateless

Important topics

• How to call beans from other beans• How to use environment properties• How to use the EJB security model• Advanced concepts:

– Transactions– BMP and CMP relationships– Persistence best practices– EJB Design Strategies