Enterprise Java Beans - PowerPoint PPT Presentation


PPT – Enterprise Java Beans PowerPoint presentation | free to view - id: 1d4d6e-ZDc1Z


The Adobe Flash plugin is needed to view this content

Get the plugin now

View by Category
About This Presentation

Enterprise Java Beans


Business methods. Code a home interface. Finder methods. Home ... Bean's business methods. Code the local home interface. Life cycle. Finder methods. 11/8/09 ... – PowerPoint PPT presentation

Number of Views:78
Avg rating:3.0/5.0
Slides: 101
Provided by: raCrem


Write a Comment
User Comments (0)
Transcript and Presenter's Notes

Title: Enterprise Java Beans

(No Transcript)
Enterprise Java Beans
  • Introduction
  • Application Server
  • Java 2 Enterprise Edition
  • What is an Enterprise Bean ?
  • EJB Properties
  • EJB Overview
  • Deployment Phase
  • Type of beans
  • Client access with interfaces
  • Remote access
  • Local Access

Enterprise Java Beans
  • Contents of an Enterprise Bean
  • EJB Example
  • EJB vs MTS
  • A few EJB implementations
  • Whats new in EJB 2.0
  • Bibliography

  • Enterprise Java Beans ( EJB ) is
  • a middleware component model for Java and CORBA
  • a specification for creating server-side,
    scalable, transactional, multi-user and secure
    enterprise-level applications
  • Presented by Sun in the 1999, they are easier
    than other technologies as RMI or Corba

  • This is the three level structure for Application

Applicaton Server
  • Presentation
  • HTML Application
  • Java Application
  • Business Logic
  • Data Access

  • HTML
  • Generated server-side HTML
  • Runs on any Web browser
  • Less client-side power
  • Java
  • Required Java virtual Machine
  • More client side power
  • Runned on a page
  • Launched from a browser or a standalone

Business Logic
  • Implements the logic of the application defining
    all the function that may be used from a client
  • Change Business Rules Easily
  • Re-use components
  • Make complex applications manageable

Data Access
  • Utility to access external datas such as Database
    or other Web component

J2EE Application Server
  • Java 2 Enterprise Edition standardizes interfaces
    for Application Server components

What is an Enterprise Bean ?
  • Is a server side component written in Java
  • Industry standard distribuited component model
  • Incorporates the business logic of an application
    ( the code that implements the purpose of the

EJB Properties
  • Bean writers need not write
  • Remote access Protocols
  • Transactional Behaviour
  • Threads
  • Security
  • State Management
  • Object life cycle
  • Resource pooling
  • Persistence

EJB Overview
Deployment Phase
Deployment Phase
When to use Enterprise bean
  • The application must be scalable.It will run on
    different machine and their location will remain
    transparent to the client
  • Transaction requirement
  • The application will have lot of different type
    of clients

Type of beans
  • Session Bean
  • Entity Bean
  • Message Driven Bean

Session Bean
  • Represents a single client inside the server
  • The client calls the session bean to invoke
    methods of an application on the server
  • Perform works for its client, hiding the
    complexity of interaction with other objects in
    the server
  • Is not shared
  • Is not persistent
  • When the client stops the session,the bean can be
    assigned to another client from the server

Session Bean
  • Stateful session bean
  • Stateless session bean

Stateful Session Bean
  • Contains the state of a single client session
  • Information on the client
  • On method called
  • Return values
  • This state is called conversational state and is
    not retained when the session ends, also if the
    client not removes the bean

Stateless Session Bean
  • Not maintain a conversational state for a
    particular client
  • Contains values only for the duration of the
    single invocation
  • Except during method invocation, all instances of
    stateless session bean are equivalent

Entity Bean
  • Represents a business object in a persistent
    storage mechanism such as a relational database
  • Usually is a table in the database and each
    instance of that entity bean is a row in that
  • Properties
  • Persistent
  • Allow shared access
  • Have primary key
  • Have relationship with other entity beans.

Entity Bean persistent
  • Bean managed persistence
  • Container managed persistence

Bean managed persistence
  • Who write the beans code must access the
    database and save his own data

Container managed persistence
  • The container save the data
  • There is no code in the bean for access the
  • The container handles all database access
    required for the bean
  • Links between beans are created using a structure
    called abstract schema

Entity beans shared access
  • Entity beans can be used by different clients
  • Its important that they work whithin
  • The EJB container provides transaction management
  • The transactions attribute are specified in the
    beans deployment description

Entity beans primary key
  • Each entity bean has a unique object identifier
    like a key in a database table

Entity beans relationship
  • Container managed persistent
  • The container performs all the operation to
    create relationship
  • Bean managed persistent
  • The code to perform relations must be written in
    the bean

Message Driven bean
  • Allows applications to process messages
  • The messages may be sent by
  • An application client
  • Another enterprise bean
  • A Web component

Message Driven bean
  • Retain no data or conversational state for a
    specific client
  • All instances 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
  • Can process messages from multiple clients

Message Driven bean
  • A client cant access directly to a message
    driven bean
  • When a message arrive, the container gives it to
    a message driven bean
  • The bean process the message

Client access with interfaces
  • A client may access a session or an entity bean
    only through the methods defined in the bean's
  • They define the client's view of a bean
  • Types of access
  • Remote access
  • Local access

Remote access
  • A remote client of an enterprise bean has the
    following traits
  • It may run on a different machine and a different
    Java virtual machine than the enterprise bean it
    accesses (It is not required to run on a
    different JVM )
  • It can be a Web component
  • It can be another enterprise bean

Remote access
  • To create an enterprise bean with remote access,
    you must
  • Code a remote interface
  • Business methods
  • Code a home interface
  • Finder methods
  • Home methods

Remote access example
Local access
  • A local client has these characteristics
  • It must run in the same JVM as the enterprise
    bean it accesses
  • It may be a Web component or another enterprise
  • To the local client, the location of the
    enterprise bean it accesses is not transparent
  • It is often an entity bean that has a
    container-managed relationship with another
    entity bean

Local access
  • To create an enterprise bean with local access,
    you must
  • Code the local interface
  • Bean's business methods
  • Code the local home interface
  • Life cycle
  • Finder methods

Local interfaces
  • If an entity bean is the target of a container
    managed relationship it MUST have local

Contents of an Enterprise Bean
  • Deployment descriptor
  • Persistence type
  • Transaction attribute
  • Enterprise bean class
  • Interfaces
  • Helper classes
  • Exception
  • Utility classes

EJB Example
  • The OnLine Bank
  • We will take a not completed system to give an
    idea to how choose if a component is an entity,
    session or message driven bean.

EJB Example
EJB Example
  • The example has three component
  • Services what the client can do in the system
    such as see the foreign currency , listed shares
    or make operations on his hown account.
  • Accounts a database containing the accounts of
    all the clients of the bank with information
    about credit,debit,access etc..
  • Security is a subsystem that receives all the
    alarm caused from wrong access and performs
    action about the situation
  • ( calls police and stops operation of that
    client keeping information about him )

EJB Example
  • In this example is easy to create an EJB
  • Client will have a web page at client side to
    insert values and connect the system.This will be
    done using JSP ( Java Servlet Pages )
  • Services will be a Statefull Session Bean and it
    will be different for each client connecting the
    system mantaining data about the client
  • Accounts will be formed by an Entity Bean for
    each account in the system with a code-account as
    primary key.
  • Security will be a Message driven bean and will
    be called only from container if some operation
    are abnormal for result or the autentification
    for the same client fails too much times.

  • Microsoft Transaction Server is based on the
    Component Object Model (COM) which is the
    middleware component model for Windows NT
  • MTS can be defined as a component-based
    programming model

EJB vs MTS Analogies
  • Implement business logic for Application Server
  • Have a Server and a container ( for MTS called
    MTS Executive )
  • Similar architecture in both models
  • A client invokes wrapped method

EJB vs MTS Difference
  • EJB
  • Component instance are pooled
  • Dont support heterogeneous transactions
  • Portability accross multiple platforms using Java
  • MTS
  • Component is not created until the call from a
    client reaches the container
  • Support heterogeneous transactions
  • Portability only on Windows NT

EJB vs MTS Difference
  • EJB
  • Invoked by clients using RMI
  • Has both persistent and non-persistent components
  • MTS
  • Invoked by clients using DCOM or through local
    COM calls
  • Components are not persistent, even though they
    may contain information

A few EJB implementations
  • WebLogic
  • Bluestone
  • Novera
  • Persistence
  • Oracle AS
  • Oracle8i

Whats new in EJB 2.0
  • Released On April 26, 2001
  • Integration with JavaTM Message Service (JMS) --
    Asynchronous Capabilities Streamline Systems
  • Send asynchronous messages via the JMS API
  • Container-Managed Persistence (CMP) --
    Simplifying and Expediting Application
  • Used to isolate the application developer from
    the physical database schema
  • Introduces for the first time a portable query
    language, based on the abstract schema

Whats new in EJB 2.0
  • Local Interfaces -- Streamlining Calls Between
    Local Beans
  • The local interface may be defined for a bean
    during development, to allow streamlined calls to
    the bean if a caller is in the same container
  • Inter-Server Interoperability -- Enabling
    Heterogeneous Environments
  • Takes the benefit of cross-server application
  • Able to deploy the EJB technology-based
    application across a heterogeneous environment
    mixing application servers from different vendors

  • The J2EE Tutorial
  • http//java.sun.com/j2ee/tutorial/1_3-fcs/doc/EJBC
  • Meeting about Application servers
  • http//www.phxjug.org/meetings/silverstream/sld001
  • Developing Enterprise components
  • http//spectral.mscs.mu.edu/EJB20001113/index.html
  • A detailed Comparison of EJB MTS models
  • http//members.tripod.com/gsraj/misc/ejbmts/ejbmts

  • What's new in the Enterprise JavaBeansTM 2.0
  • http//java.sun.com/products/ejb/2.0.html
  • Introduction to Enterprise JavaBeans
  • http//cosmos.inesc.pt/a01/javacourses/ejb/
  • Programming WebLogic Enterprise JavaBeans
  • http//e-docs.bea.com/wls/docs61/ejb/index.html

The MTS architecture
  • Is made up of
  • The MTS Executive (mtxex.dll)
  • The Factory Wrappers and Context Wrappers for
    each component
  • The MTS Server Component
  • MTS clients
  • Auxiliary systems like
  • COM runtime services,
  • Service Control Manager (SCM)
  • The Microsoft Distributed Transaction Coordinator
  • The Microsoft Message Queue (MSMQ)
  • The COM-Transaction Integrator (COM-TI)

The MTS architecture
The EJB architecture
  • Consists of
  • An EJB server
  • EJB containers that run within the server
  • Home objects
  • Remote EJBObjects
  • Enterprise Beans
  • EJB clients
  • Auxiliary systems like
  • Java Naming and Directory Interface (JNDI)
  • Java Transaction Service (JTS)
  • Security services

The EJB architecture
Stateful session beans life cycle
  • The client invoke the create method
  • The EJB container
  • Instantiates the bean
  • Invokes the setSessionContext
  • Invokes ejbCreate
  • The bean is ready

Stateful session beans life cycle
  • While in the ready state
  • EJB container may passivate the bean moving it
    from memory to secondary storage
  • A client may invoke a business method
  • EJB container may activate a bean,moving it back
    to the ready stage, and then calls the bean's
    ejbActivate method
  • A client may invoke the remove method and the
    container calls the bean's ejbRemove method

Stateful session beans life cycle
Stateless session beans life cycle
  • The client invoke the create method
  • The EJB container
  • Instantiates the bean
  • Invokes the setSessionContext
  • Invokes ejbCreate
  • The bean is ready

Stateless session beans life cycle
  • While in the ready state
  • A client may invoke a business method
  • A client may invoke the remove method and the
    container calls the bean's ejbRemove method
  • Its never passivate

Stateless session beans life cycle
Entity beans life cycle
  • The EJB container
  • Creates the instance
  • Calls the setEntityContext
  • The entity bean moves to a pool of available

Entity beans life cycle
  • While in the pool
  • Instance is not associated with any particular
    object identity
  • All instances in the pool are identical
  • EJB container may assign an identity to an
    instance when moving it to the ready stage
    invoking the ejbActivate method
  • A client may invoke the create method
  • EJB container calls ejbCreate and ejbPostCreate
  • EJB container may remove the instance invoking

Entity beans life cycle
  • While in the ready state
  • A client may invoke entity bean's business
  • A client may invoke the remove method
  • EJB container calls the ejbRemove method
  • EJB container may invoke the ejbPassivate method

Entity beans life cycle
Message driven beans life cycle
  • EJB container creates a pool of message-driven
    bean instances
  • For each instance, the EJB container instantiates
    the bean
  • It calls the setMessageDrivenContext
  • It calls the instance's ejbCreate
  • Like a stateless session bean,its never
    passivated, It has only two states
  • Nonexistent
  • Ready to receive messages.

Message driven beans life cycle
  • While in the ready state
  • EJB container may call onMessage
  • EJB container may call the ejbRemove

Message driven beans life cycle
Abstract schema
  • Part of an entity bean's deployment descriptor
  • Defines the bean's persistent fields and
    relationships.The term abstract distinguishes
    this schema from the physical schema of the
    underlying data store
  • You specify the name of an abstract schema in the
    deployment descriptor

Example of Abstract schema
Abstract schema
  • Persistent fields
  • Are stored in the underlying data store
  • Constitute the state of the bean. At runtime, the
    EJB container automatically synchronizes this
    state with the database
  • During deployment, the container
  • Maps the entity bean to a database table
  • Maps the persistent fields to the table's columns

Abstract schema
  • Relationship fields
  • Its like a foreign key in a database table.It
    identifies a related bean
  • Like a persistent field, a relationship field is
    virtual and is defined in the enterprise bean
    class with access methods
  • Unlike a persistent field, a relationship field
    does not represent the bean's state

Abstract schema
  • Multiplicity in Container-Managed Relationships
  • One-to-one Each entity bean instance is related
    to a single instance of another entity bean
  • One-to-many An entity bean instance may be
    related to multiple instances of the other entity
  • Many-to-one Multiple instances of an entity bean
    may be related to a single instance of the other
    entity bean
  • Many-to-many The entity bean instances may be
    related to multiple instances of each other

Abstract schema
  • Direction in Container-Managed Relationships
  • Bidirectional relationship each entity bean has
    a relationship field that refers to the other
    bean. Through the relationship field, an entity
    bean's code can access its related object
  • Unidirectional relationship only one entity bean
    has a relationship field that refers to the other

EJB 3.0
  • This section based on
  • Mastering EJB 3.0 4th Ed

EJB 3.0
  • Key issues for distributed systems
  • Remote Method Invocation
  • Load Balancing
  • Transparent failover
  • Back-end integration to legacy systems
  • Transactions
  • Clustering (state replication across servers)
  • Dynamic redeployment (upgrade while running)

EJB 3.0
  • More key issues
  • Clean shutdown
  • Logging auditing
  • Systems management
  • Threading
  • Message-oriented middleware
  • Component lifecycle
  • Resource pooling

EJB 3.0
  • Yet more issues
  • Security
  • Caching
  • Etc., etc.

EJB 3.0
  • All of these can be thought of as middleware
  • Developer of distributed app shouldnt have to
    worry about them
  • Should concentrate on business logic
  • Much too expensive to build from scratch
  • Distributed app should be portable across
    middleware service providers

EJB 3.0
  • EJB spec defines standard interfaces between
    distributed components their containers

EJB 3.0
  • Advantages
  • Industry standard
  • Vendors design containers to EJB specs
  • Portability possible across containers
  • RAD capable because services provided by

EJB 3.0
  • Whats an EJB?
  • Server-side component encapsulating application
  • Business logic
  • DB access
  • Integrate with other legacy systems
  • Serve multiple clients
  • Browser
  • Desktop apps
  • Other EJBs
  • Web app components (e.g., Servlets)
  • Web Service clients

EJB 3.0
  • How do they work?
  • EJB can be located anywhere, usually on another
  • Use RMI-IIOP for remote invocation of methods (P.
  • Location transparency
  • Client thinks remot EJB is local
  • Run inside EJB container
  • Application servers

EJB 3.0
  • Who makes containers?
  • Most of the usual enterprise suspects
  • BEA, IBM, Red Hat (JBOSS), Sun, Oracle
  • Closely related to web services
  • P. 29

EJB 3.0
  • EJB 2.1 was NASTY!!
  • HelloWorld EJB 2.1 (pp. 40-49)
  • Many disappointed enterprise developers
  • Vastly simplified in EJB 3.0
  • HelloWorld EJB 3.0 (pp. 83-85)
  • Many once burned twice shy developers
  • Suns dilemma
  • If we simplify it, will they resume coding EJBs?

EJB 3.0
  • EJB best practices
  • For appropriate use only
  • Remoting is required
  • Distributed transactions required
  • Component security required
  • Persistence required
  • Legacy system integration
  • Scalability required

EJB 3.0
  • If your application components dont need to be
    distributed across servers, avoid using
    distributed components like EJBs

EJB 3.0
  • This section based on
  • J2EE Development Without EJB

EJB 3.0
  • Written by a once burned twice shy EJB
  • Observations apply to EJB 2.1 (and earlier)
  • EJB 3.0 spec in early stages when book written

EJB 3.0
  • Basic thesis is EJBs.
  • Rarely necessary for web-based applications
  • Commonly overused
  • Symptom of over-engineering
  • Can obtain most of needed EJB services via other
    programming techniques using lightweight
    containers (e.g., Spring)
  • Java EE Java SE
  • Aspect-oriented programming
  • Dynamic proxies

EJB 3.0
  • Hes not alone
  • Many other enterprise architects and developers
    have critized EJBs since early 2000s
  • Martin Fowlers Law of Distributed Objects
  • Dont do it!!

EJB 3.0
  • Some criticisms
  • Too complex
  • Specifications getting more complex
  • EJB 3.0 tries to address this
  • Unit testing hard to do
  • Have to isolate app from container
  • If something broken, is it container related or
    app related?

EJB 3.0
  • Other technologies (AOP) get the job done more
  • Entity beans are a flop
  • EJB 3.0 doesnt upgrade entity beans
  • Left to wither on the Java EE vine
  • Persistence API tries to address this

EJB 3.0
  • Metadata (a la .NET) are better than descriptor
  • Annotations designed to address this
  • Only truly unique service provided by EJB is
    remote access to a component
  • Java EE provides other services already
  • JNDI, JTA, JCA, resource pooling, etc.

EJB 3.0
  • EJBs remain best choice for apps that genuinely
    need object distribution or use IIOP protocol
  • EJBs also pretty good for apps based around
    asynchronous messaging
  • Message-drive beans pretty simple

EJB 3.0
  • Financial services middleware likely good place
    for EJB-driven apps
  • Argues that EJBs are declining technology will
    be legacy within 3 years
  • Doesnt think EJB 3.0 will make much of a

EJB 3.0
  • Advocates use of lightweight containers like
  • Oh btw, author is co-founder of Spring framework!
  • Bias is always an issue!

EJB 3.0
  • Whats an enterprise developer to do?
  • Distributed systems a holy grail of system
    architecture deployment since early 90s
  • CORBA initiative began in 91
  • Web Services are current frontier
  • IT profession still learning when how to do
    distributed systems

EJB 3.0
  • References
  • Master Enterprise JavaBeans 3.0, 4th Ed,
    Sriganesh et. Al., 2006
  • J2EE Development Without EJB, Johnson, 2004
About PowerShow.com