Title: Dia 1
1(No Transcript)
2Enterprise JavaBeansIntroduction
- Geert De Maere
- GDM AT cs.Nott.ac.UK
3Course overview
- Introduction
- EJBs
- Session beans
- Stateless
- Stateful
- Entity beans
- BMP Bean managed persistence
- CMP Container managed persistence
- Exercises (Ecommerce application)
- Best practices
4Todays overview
- J2EE overview
- Enterprise applications?
- Component models
- EJB components
- Developing EJBs
- Session beans
5EJB appetizer
Enterprise JavaBeans is a server-side component
architecture that simplifies the process of
building enterprise-class distributed component
applications in Java. By using EJB, you can write
scalable, reliable, and secure applications
without writing your own complex distributed
component framework. EJB is about rapid
application development for server side you can
quickly and easily construct server-side
components in Java by leveraging a prewritten
distributed infrastructure provided by the
industry. EJB is designed to support application
portability and reusability across any vendors
enterprise middleware services. From Mastering
Enterprise Javabeans
6Introduction to J2EE
7Platform overview
- Java 2 Platform, Micro Edition (J2ME)
- Java applications for micro-devices
- Java 2 Platform, Standard Edition (J2SE)
- applets
- standalone applications
- Java 2 Platform, Enterprise Edition (J2EE)
- enterprise applications
Specifications!!!
8J2EE platform
- J2EE contains
- Specifications
- Test suite
- Blueprints (describes function of the APIs)
- Reference implementation
- Provides middleware services
9J2EE APIs
- Enterprise JavaBeans (EJBs)
- Remote method invocation (RMI)
- Java Naming and Directory Interface (JNDI)
- Java Database Connectivity (JDBC)
- Java Transaction API (JTA)
- Java Messaging Service (JMS)
- Java Servlets
- Java Server Pages (JSPs)
- Java IDL
- JavaMail
- J2EE Connector Architecture (JCA)
- Java API for XML Parsing (JAXP)
- Java Authentication and Authorisation Service
(JAAS)
Generic APIs
10J2EE architecture
Browsers
Applications
Others
Java Packages
Swing
EnterpriseBeans
JavaServerPages
JDBC
Others
Java Virtual Machine
Windows, Solaris, Linux, others...
11Enterprise applications
12Enterprise applications
GUI
GUI
GUI
BusinessLogic
GUI Logic
Presentation Logic
GUI BusinessLogic Data access
Data access
Data access
BusinessLogic
BusinessLogic
Data access
Distributed
13Enterprise applications
Client
Client
Client
Server
Server
Back-end
14Enterprise applications, multi-tier
Web services
Application
Browser
Client tier
SOAP, UDDI, WSDL,
IIOP
HTTP
Application logic
Business tier
Presentation Logic
Business Logic
Database
Filesystem
XML Documents
Data tier
15Enterprise applications, multi-tier
- Tree logical layers
- Client tier
- Business tier
- Data tier
- More flexible design
- UI is independent from application logic
16Enterprise applications, demands
- Demands
- Resource pooling
- Transactions
- Back-end integration, persistence
- Security
- Reliable
- Scalable
- Load balancing
- Transparent fail-over
- Clustering
- Dynamic redeployment
- Caching
- Low complexity
- Fast development, Low TTM
J2EE allows you to buy middleware services
17Component models
18Component model
Container
Interface between component and container is
specifiedin component architecture!
- A component model defines a set of contracts
between the component developer and the system
that hosts the component (container). The
contract expresses how a component should be
developed and packaged - A component is an independent piece of software
that can be distributed and used in other
applications - A container is a managed environment
19Component model
Application server
Container
Client
Component
JRMP, IIOP, HTTP SSL, SOAP HTTP,
Container and application server provide low
level services (example CD)
20J2EE components, containers
- Components
- Client components (apps, applets, web clients)
- Web components (servlets, jsp)
- Business components (EJBs)
- Containers
- Application client container
- Applet container
- Web container
- Enterprise JavaBean container
21EJB components
22EJB History
J2EE
DNA
CORBA
EJB 1.0
EJB 1.1
Perfecting EJB 1.0
- Major improvement of Entity-beans
- Message driven beans
EJB 2.0
EJB 2.1
EJB 3.0
23Request interception
Application server
Vendor specific implementation
24Container services
- Resource management
- Instance pooling (similar to connection pooling)
- Activation
- Primary services
- Concurrency
- Transaction management
- Persistence
- Distributed objects
- Naming
- Security
Managed by the EJB container!
25Resource management, instance pooling
26Resource management, instance pooling
Appl. 1
Appl. 2
Appl. 3
Appl. 4
Appl. 5
Appl. 6
Appl. 7
Appl. 8
DB
27Resource management, activation
- Used for stateful session beans (conversational
state) - Mechanism
- Passivation serialize beans state in secondary
storage - Remove bean from memory
- ActivationRestoring the beans state
28Resource management, activation
EJB Container
Client
Passivation
FS
Bean
EJB Container
Client
Clear memory
FS
EJB Container
Client
Activation
FS
Bean
29Primary services, distributed objects
- Location transparency
- RMI-IIOP (Other protocols are possible JRMP,
SOAP) - Client view remote interface
30Primary services, concurrency
Client
EJB Container
Bean
Client
Bean instance does not allow concurrent access!
Only one client thread is executed at a
time! Beans are single threaded!
31Primary services, transaction
- Unit-of-work
- Types
- Flat transactions (example order book)
- Nested transactions (example book trip)
- EJB container supports declarative transaction
support!
32Primary services, data persistence
- Container synchronizes beans state with database
- BMP
- CMP
- Vendor specific implementation, based on callback
methods - Uses
- JDBC (generic API)
- EJB QL
- Mappings
- Relational
- Objects
- Legacy systems (mainly BMP)
33Primary services, naming
- Lookup service, locating distributed objects,
resources - Fundamentals
- Binding
- Lookup
- Java Naming and Directory Interface
JNDI(generic API) - Most platforms support Corba naming service
34Primary services, security
- Types
- Authentication
- Access control
- Secure communication
- Security roles are mapped to real-world
user-groups - Security identity is propagated
35Benefits of EJBs
- Simplified, and rapid development of distributed
applications - EJB container provides system-level service
- Supports multi-tiered development
- Widely used technology
- Portability
- Any operating system
- Any application server
- Reusability
36When to use EJBs
- The application must be scalable (clustering)
- Transaction management is necessary
- Different clients have to be supported (web, app)
37Component marketplace
- Buy components (pricing components)(www.component
source.com)(www.flashline.com) - Assemble application form prewritten components
- Less in-house experience needed
- Faster development
- Lower cost
38Application servers
39Application servers
- Additional Features
- E-commerce components
- Load-balancing
- Transparent fail-over
- Caching
Over 50 vendors!!!
40Application servers
41Application servers
- Commercial
- Orion
- BEA weblogic
- Pramati Sybase EAServer
- IBM WebSphere
- Open Source
- OpenEJB
- JOnAS
- JBoss
42Session beans
43Overview
Session Beans
Stateful
Stateless
No conversational state (pooled)
Conversational state (exclusive/client)
Deployment option
Managed by container
44Developing beans
- Write the java files
- Compile the java files
- Write/generate deployment descriptors
- Create vendor specific files
- Create jar-file
- Deploy jar-file
TOOLS available
45Parts of an EJB
- Remote (used for request interception)
- Remote interface
- Remote home interface
- Local (EJB 2.0)(used for request interception)
- Local interface
- Local home interface
- Enterprise bean class
- Deployment descriptor
- Vendor specific files
Interfaces
Applies to Session and Entity beans!!!
46Parts of an EJB
47EJB request interception
Application server
EJB Container
Client
EJB container middleware
2. Call middleware
1. Client method call
5. Return result
EJB Object Request interception
3. Call bean
4. Return result
Vendor specific implementation
48EJB request interception
Application server
49Parts of an EJB
Remotehome interface
Localhome interface
EJB Container
Client
Home Object (factory)
Enterprisebean class
Remote interface
EJB Object Request interception
Local interface
50EJB remote interface
- Used by the container for generating the EJB
Object - Defines business methods exposed to remote
clients - Must be written according to the EJB
specifications - Inherits from javax.ejb.EJBObject (which extends
java.rmi.Remote) - Properties
- Callable from remote JVM
- Methods must throw RemoteException
- Method parameters must be RMI-Parameters
- Primitive
- Serializable
- Remote objects
51EJB remote home interface
- Used by the container for generating the Home
Object (EJB object factory) - Defines lifecycle methods which can be accessed
from outside the container - Creating EJB objects
- Destroying EJB objects
- Finding EJB objects (Entity beans)
- Must be written according to the EJB
specifications - Inherits from javax.ejb.EJBHome
- Extends java.rmi.Remote
- Defines required methods
52EJB local interface (EJB 2.0)
- Used by the container for generating the LOCAL
Object - Defines business methods exposed to co-located
beans - Must be written according to the EJB
specifications - Inherits from javax.ejb.EJBLocalObject
- Properties
- Increased performance
- No RMI overhead
- Call Local object
- Request middleware services
- Execute request
- Parameters are passed by reference
- Method calls must be performed within the same
container
53EJB local home interface (EJB 2.0)
- Used by the container for generating the LOCAL
Home Object (EJB object factory) - Defines lifecycle methods exposed to co-located
beans - Creating Local EJB objects
- Destroying Local EJB objects
- Finding Local EJB objects
- Must be written according to the EJB
specifications(Inherits from javax.ejb.EJBLocalHo
me) - Improved performance!
54Bean class
- Implements
- Business methods (Remote/Local interface)
- Life-cycle methods (Remote/Local home interface)
- Callback methods (notify container, lifecycle)
- ejbActivate()
- ejbPassivate()
- ejbRemove()
- setSessionContext(javax.ejb.SessionContext
sessionContext) - Methods must match the signatures defined in
remote and local interfaces
55Bean class
//Bean class import javax.ejb. import
java.rmi. public class ExampleBean implements
SessionBean //callback method //life-cycle
methods //Business methods
56Stateless session bean lifecycle
- State transitions
- Create bean instance (1)
- Class.newInstance()
- setSessionContext()
- ejbCreate()
- Delete bean instance (2)
- ejbRemove()
- Garbage collection
- Business methods (3)
Does not exist
1.
2.
Pooled (method ready)
3.
57Stateful bean lifecycle
Does not exist
1.
ejbPassivate()
Pooled (method ready)
Passive
ejbActivate()
3.
58Deployment descriptor
- File format
- EJB 1.0 serializable classes
- EJB 1.1, 2.0 XML
- Allow us to define runtime properties attributes
- Similar to property files
- Used by container tools
- Important tags
- ltejb-namegt
- lthomegt
- ltremotegt
- ltejb-classgt
-
59Example, HelloWorld(Stateless Session Bean)
60Example, diagram
ltltinterfacegtgt Remote (java.rmi)
ltltinterfacegtgt Serializable (java.io)
J2EE platform
ltltinterfacegtgt EnterpriseBean (javax.ejb)
EJB distribution
ltltinterfacegtgt EJBObject (javax.ejb)
ltltinterfacegtgt EJBHome (javax.ejb)
ltltinterfacegtgt EJBLocalHome (javax.ejb)
ltltinterfacegtgt SessionBean (javax.ejb)
ltltinterfacegtgt HelloWorld
ltltinterfacegtgt HelloWorld- Home
ltltinterfacegtgt HelloWorld- LocalHome
HelloWorldBean
Write yourself
Remote EJBObject
RemoteEJBHome Obj
Local EJB Home obj
Generated by container tools
61Example, remote interface
package helloWorld public interface HelloWorld
extends javax.ejb.EJBObject public String
hello() throws java.rmi.RemoteException
62Example, local interface
package helloWorld import javax.ejb. public
interface HelloWorldLocal extends EJBLocalObject
public String hello()
63Example, remote home interface
package helloWorld import java.rmi. import
javax.ejb. public interface HelloWorldHome
extends EJBHome public HelloWorld create()
throws RemoteException, CreateException
64Example, local home interface
package helloWorld import javax.ejb. public
interface HelloWorldLocalHome extends
EJBLocalHome public HelloWorldLocal
create() throws CreateException
65Example, bean class
package helloWorld import javax.ejb. public
class HelloWorldBean implements SessionBean
private SessionContext ctx public
HelloWorldBean() public String hello()
return "hello world" public void
ejbCreate() throws CreateException public
void ejbActivate() public void
ejbPassivate() public void ejbRemove()
public void setSessionContext(SessionContext sc)
this.ctx sc
66Naming conventions
- Access methods (remote/local interface)
- setltPersistence-fieldgt
- getltPersistence-fieldgt
- Files
- Remote interface ltlogical-object-namegt
- Remote interface lt logical-object-namegt Home
- Local interface lt logical-object-namegt Local
- Local home interface lt logical-object-namegt
LocalHome - Bean class lt logical-object-namegt Bean
67Example, deployment descriptor
lt?xml version"1.0" encoding"UTF-8"?gt lt!DOCTYPE
ejb-jar PUBLIC '-//Sun Microsystems, Inc.//DTD
Enterprise JavaBeans 2.0//EN' 'http//java.sun.com
/dtd/ejb-jar_2_0.dtd'gt ltejb-jargt
ltdisplay-namegtHelloWorldEJBlt/display-namegt
ltenterprise-beansgt ltsessiongt
ltdisplay-namegtHelloWorldBeanlt/display-namegt
ltejb-namegtHelloWorldBeanlt/ejb-namegt
lthomegthelloWorld.HelloWorldHomelt/homegt
ltremotegthelloWorld.HelloWorldlt/remotegt
ltlocal-homegthelloWorld.HelloWorldLocalHomelt/local-
omegt ltlocalgthelloWorld.HelloWorldLocallt/loca
lgt ltejb-classgthelloWorld.HelloWorldBeanlt/ejb
-classgt ltsession-typegtStatelesslt/session-typ
egt lt/sessiongt lt/enterprise-beansgt lt/ejb-jargt
68Example, client
package helloWorld import javax.naming. publi
c class HelloWorldClient public static void
main(String args) try Context ctx
new InitialContext() Object obj
ctx.lookup("ejb/helloWorld")
helloWorld.HelloWorldHome home
(HelloWorldHome) javax.rmi.PortableRemoteObject.na
rrow(obj, helloWorld.HelloWorldHome.class)
HelloWorld helloWorld home.create()
System.out.println(helloWorld.hello())
helloWorld.remove() catch(Exception
e) System.out.println("Exception "
e.getMessage()) e.printStackTrace()
69EJB ecosystem
Supply tools
Deploy system
Assemble application
Tool Provider
Develop beans
System administrator
Application assembler
Deployer
Supply container
Bean provider
EJB container/server provider
70Exercise
71Methods
- getAvailableProducts()(returns a vector of
products) - selectProduct(int productCode)(marks a product
as selected) - getAllSelectedProducts()(returns a vector
containing the selected products) - calculatePrice()(returns the total price of all
selected products)
72Documentation
- Tutorial Sun (http//java.sun.com)
- Application server reviews
- http//www.flashline.com/components/appservermatri
x.jsp - http//www2.theserverside.com/reviews/index.jsp
- Books
- Enterprise JavaBeans (Oreilly, ISBN
0-596-00226-2) - Mastering EJBs (Wiley, ISBN 0-471-41711-4)
73Java Rules!!!