Download Processing Instructions

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
The EJB Approach (DeitelDeitelSantry, Ch. 14; jGuru Tutorial)
Java servlets and JSP are one approach for implementing business and
presentation logic multi-tier applications
Enterprise JavaBeans (EJBs) defines architecture for a
 transactional
 distributed object
system based on
 components, i.e. predefined software elements that can be
assembled into an application; (this is typically done in a graphical
programming and design environment) .
 Specification mandates a programming model, i.e.
 conventions or protocols
 set of classes and interfaces
that make up EJB API
Provides bean developer and server vendors with a set of contracts that
define a common development platform with the
Goals of
 portability and
 support of rich functionality
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
1
EJB Container
An EJB container provides the EJB run time and lifecycle management
environment, i.e. the container hosts and manages EJBs
Analogies: EJB container
EJB
Web server
Servlet
Web browser
Applet
 EJB container manages all aspects of enterprise bean at lifetime including
 remote access to bean: EJB is isolated from direct access by client:
client invokes method; container intercepts it and assures that
 secuity;
 persistence;
are properly applied to all operations on bean
 transactions;
 concurrency – many beans are managed simultaneously;
 access to everything bean needs: JDBC connections, another EJB, selfreferences, access to properties
 pooling of resources and manages life cycle of EJB to reduce memory
and processing, e.g. if bean is not used container puts it in pool to be
used by another client or evicts bean from memory and brings it back
only if/when needed
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
2
EJB interfaces and implementation
EJBs consist of
 remote interface – declares the business methods that EJB client can
invoke;
home interface – provides create methods for new EJB instances, finder
methods to find EJB instances, and remove methods for cleaning up EJB
instances
EJB implementation – defines the methods in the remote and home
interfaces
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
3
Bean, EJB Container, and client
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
4
Bean-EJB Container Interaction
The enterprise bean interacts with its container through three mechanisms:
(i) Every bean has an EJBContext object which is a reference to the container
The EJB context interface provides methods for interacting with the container
so that the bean can request information about environment, such as
 client identity;
 status of transaction
 reference to bean itself
(ii) Java Naming and Directory Interface (JNDI) – standard extension to
access naming systems (e.g. LDAP, NetWare, file systems). Every bean has
automatically access to a special naming system, called the Environment
Naming Context (ENC), that is managed by container and accessed by beans
using JNDI. JNDI ENC allows access to resources such as JDBC connections,
other enterprise beans, and properties specific to bean.
(iii) Callbacks – methods called by container to notify bean to different events
in its lifecycle, e.g. container is about to
 activate bean
 removed bean from memory
 persist bean state in memory,
 end transaction
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
5
Beans and Interfaces
+interface(remote methods)
+class
HotelClerk
HotelClerkEJB
+interface(remote methods)
HotelClerkHome
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
6
Home and Remote Interfaces
+ interface (remote methods)
Customer
Remote Interface
represents
business methods,
e.g.
accessor/mutators
extends
+ interface
javax.ejb.EJBObject
extends
+ interface
+ interface (remote methods)
java.rmi.Remote
CustomerHome
Home Interface
represents
life cycle methods,
e.g. create, destroy,
find
Fall 2003
extends
+ interface
javax.ejb.EJBHome
MET CS 667: 10. Servlets, JSPs,
EJBs
extends
7
EJB Interfaces and Implementations
EJB remote interface must extend interface javax.ejb.EJBObject, e.g.
public interface Customer extends EJBObject
The EJB container creates a class that implements the remote interface. When a
client invokes a remote method, the EJB container invokes the corresponding
method of the implementation CustomerEJB of the remote interface
Customer
EJB home interface must extend interface javax.ejb.EJBHome, e.g.
public interface CustomerHome extends EJBHome
The EJB container provides an implementation of the home interface.
Depending on the EJB type – session or entity bean – the container invokes the
appropriate methods for creating, finding, and removing EJBs.
EJB implementation defines the business methods; a session bean must
implement interface javax.ejb.SessionBean, e.g.
public class HotelClerkEJB implements SessionBean
and an entity bean must implement interface javax.ejb.EntityBean, e.g.
public class CustomerEJB implements EntityBean
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
8
Roles for J2EE implementers
enterprise bean provider – implements Java classes for EJB;
application assembler – constructs application components from EJBs
implemented by the enterprise bean provider;
deployer – deploys application to an EJB container, ensuring that all
dependencies are met;
EJB server provider
–
implement application server suitable for
EJB container provider –
deployment of J2EE applications;
typically includes:
• EJB container;
• servlet container;
and provides services such as
• JNDI directories to locate data sources;
• DB connection pooling;
• integration with distributed systems;
• resource management.
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
9
Entity EJB
Entity EJBs:
 represent persistent business objects such as are stored in a relational DB;
 allow building an object based representations of data and behavior, and
 provides interface to data that would be normally accessed by JDBC or other
back-end API;
There are two types of entity EJBs:
container-managed persistence (CMP) –
 container manages persistence automatically;
 vendor tools are used to map the entity fields to the database
 no database access code written in the bean;
 container synchronizes bean state with data base;
 deployer must indicate what data fields are persistent, called container managed
fields, and the corresponding persistent data sources when deploying the EJB;
 easiest for developer to create, most difficult for EJB server to support
 restrictions on the types of the container managed field : container managed
fields can be any primitive or serializable type that has corresponding types
(columns) in the database directly or through Object Relational mapping (the
latter is typically provided by the DB vendor)
bean-managed persistence (BMP) –
 bean includes database access code;
 bean synchronizes its own state with database
Fall 
2003more difficult for developer
MET CSbut
667:great
10. Servlets,
JSPs,
10
flexibility
EJBs
Session EJB
Session EJBs – perform business logic for a client, e.g. access/manipulate DB.
Session EJB do not represent a DB directly and are not persistent, i.e.
instances are lost if EJB container crashes.
There are two types of session EJBs:
stateless – no state info is maintained between business method invocations.
stateful – maintains conversational state information between business
method invocations, e.g. maintain shopping cart info while customer browses
on-line store; each time the customer adds item to shopping cart, item info
(price, quantity, make,.etc.) is stored.
dedicated to one client, i.e. unlike stateless session beans clients do not share
stateful session beans
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
11
Entity and Session EJB Class Hierarchy
class
CustomerEJB
implements
interface
javax.ejb.EntityBean
extends
interface
javax.ejb.EnterpriseBean
class
HotelClerkEJB
Fall 2003
implements
interface
javax.ejb.SessionBean
MET CS 667: 10. Servlets, JSPs,
EJBs
extends
12
Example: Remote Interfaces for Entity and Session EJB
+ interface (remote methods)
Customer
accessor/
mutator
+getName(): Name
+setName(Name name): void
+getAddress(): Address
+setAddress (Address address): void
+ interface (remote methods)
HotelClerk
tasks /
processes
extends
+ interface
javax.ejb.EJBObject
+reserveRoom(Customer cust
RoomInfo ri ,
Date from,
Date to): void
+availableRooms (Location loc
Date from,
Date to): RoomInfo
extends
extends
+ interface
java.rmi.Remote
extends
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
13
Example: Implementation Classes for Entity and Session EJB
+ class
CustomerEJB
+getName() {…code..}
+setName(Name name) {…code..}
+getAddress() {…code..}
+setAddress (Address address) {…code..}
//Lifecycle methods
//Callbacks
+ interface
javax.ejb.EntityBean
implements
extends
+ interface
javax.ejb.EnterpriseBean
+ class
extends
HotelClerkEJB
+reserveRoom(Customer cust, RoomInfo ri ,
+ interface
Date from, Date to): {…code..}
javax.ejb.SessionBean
+availableRooms (Location loc, Date from,
implements
Date to) {…code..}
//Lifecycle methods from Home interface
//Callbacks
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
14
Notes on Bean Classes:
EJB implementation classes are not required to implement the remote and
home interface
In fact this is discouraged because their base types of the remote and home
inerface, EJBRemote and EJBHome, define many other methods that are
automatically implemented by the container
BUT: EJB class provides implementation for the business, lifecycle and
callback methods
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
15
Example: Home Interface – creating, destroying, locating beans
+ interface (remote methods)
CustomerHome
extends
javax.ejb.EJBHome
+create(Integer id): Customer;
+create(Integer id, Name name): Customer;
//other create methods
+findByPrimaryKey (Integer id): Customer
+findByZipCode (int zipCode): Customer
//other find methods
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
extends
java.rmi.Remote
16
Example: Customer – an Entity Bean
+ class
implements
CustomerHome
customerID: int
myAddress: Address ;
myName: Name ;
myCreditCard: CreditCard ;
// BUSINESS METHODS as in slide 25
+getName() { return myName;}:Name
// CREATION METHODS
+ejbCreate(Integer id) {
customerID = id.intValue(); return null; }: Customer
+ ejbPostCreate(Integer id) { }: void
+ ejbCreate(Integer id, Name name) {
myName = name; return ejbCreate(id); }: Customer
+ ejbPostCreate(Integer id, Name name) { }: void
//CALLBACKS
+ setEntityContext(EntityContext cntx) { }: void
+ ejbLoad() { } void
+ ejbStore() { } void
+ ejbActivate() { } void
+ ejbPassivate() { } void
Fall 2003
MET CS 667: 10. Servlets, JSPs,
+ ejbRemove(){
}: void
EJBs
EntityBean
extends
EnterpriseBean
17
Notes on Lifecycle Methods:
 Invoking create results in a new entity and a new record in the database;
 The home interface can have zero or more create methods with different arguments;
 Each create method must have a corresponding ejbCreate and ejbPostCreate
method in the bean implementation class;
 When create is invoked, the container delegates the call to ejbCreate of the bean
instance, and ejbCreate initializes the instance state before record is inserte in
database (here customerId and Name); in the case of CMP, when the ejbCreate is
finished the container reads the container managed fields and inserts a new record
into the database according to the mapping of data to columns
 the entity bean does not technically exist until its data is inserted into database; after
that the bean can access its own primary key and remote references;
 if a bean needs to access its primary key and remote references after it exists but
before using any business methods it can do it through the ejbPostCreate method
that allows any post create processing before serving client requests
 find methods query EJB server to locate specific entity beans; there is no standard
query language and implementation is vendor specific; in CMP the find methods are
not implemented with matching methods in the bean class; the deployer uses vendor
specific tools to tell the container how they behave;
 The EJBHome interface defines a number of other methods that CustomerHome
inherits, among them a remove() method for destroying the bean
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
18
Client Accesses EJB Through Home and Remote Interfaces
CustomerHome home = // Get a reference to the CustomerHome object
Customer customer = home.create(new Integer(777));
Name name = new Name("Moll", "Flanders");
customer.setName(name);
Address addr = customer.getAddress();
addr.zip = "02215";
customer.setAddress(addr);//change the zip code
Enumeration enumOfCustomers = home.findByZip(02215);
Customer customer2 = home.findByPrimaryKey( new Integer(777));
Name name2 = customer2.getName();
System.out.println(name); // output is " Moll Flanders "
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
19
Example: CreditServiceEJB – a Stateless Session EJB
+ interface (remote methods)
extends + interface
CreditService
javax.ejb.EJBObject
+verify(CreditCard card, double amount): void
+charge(CreditCard card, double amount): void
+ interface
java.rmi.Remote
+ interface(remote methods)
+ interface
CreditServiceHome
javax.ejb.EJBHome
+create(): CreditService
+ class
CreditServiceEJB
acmeURL: URL ; //URL resource factory
acmeCon: Connection; //connection to credit
//service obtained through acmeURL
+ejbCreate() {..code..}
+verify(CreditCard card, double amount)
{..code..}: void
+charge(CreditCard card, double amount)
{..code..}: void
//other methods
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
+ interface
javax.ejb.SessionBean
implements
extends
+ interface
javax.ejb.EnterpriseBean
20
J2EE Platform Overview
(Figure1.1 from Designing Enterprise Applications Using the J2EE platform)
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
21
J2EE Application Scenarios
(Figure1.2 from Designing Enterprise Applications Using the J2EE platform)
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
22
Multitier Application Scenario
(Figure1.3 from Designing Enterprise Applications Using the J2EE platform)
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
23
Stand-Alone Client Scenario
(Figure1.3 from Designing Enterprise Applications Using the J2EE platform)
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
24
Stand-Alone Client Scenario
(Figure1.4 from Designing Enterprise Applications Using the J2EE platform)
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
25
Stand-Alone Types: Direct Access
(Figure1.5 from Designing Enterprise Applications Using the J2EE platform)
Client accesses EJBs directly; RMI-IIOP used
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
26
Stand-Alone Client Types (continued)
 Stand-alone clients, implemented in the Java language or another
programming language, consuming dynamic Web content (usually XML
data messages).
Web container essentially handles XML transformations and
provides Web connectivity to clients.
Presentation logic occurs in the client tier.
The Web tier handles business logic and may directly access EIS
resources.
 Ideally, business logic is implemented as enterprise beans to take
advantage of the rich enterprise beans component model.
 Stand-alone Java application clients accessing enterprise information
system resources directly using JDBC or Connectors.
presentation and business logic are co-located on the client
platform and may in fact be tightly integrated into a single
application.
 classic two-tier client-server architecture, with its associated
distribution, maintenance, and scalability issues.
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
27
Web Centric Application Scenario
(Figure1.6 from Designing Enterprise Applications Using the J2EE platform)
.
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
28
Web Container in a Three-Tier Scenario
(Figure1.7 from Designing Enterprise Applications Using the J2EE platform)
.
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
29
Business-to-Business Scenario
(Figure1.8 from Designing Enterprise Applications Using the J2EE platform)
.
Fall 2003
MET CS 667: 10. Servlets, JSPs,
EJBs
30
References EJB
Deitel, Deitel, Santry: Advanced Java 2 Platforam –How to Program. Prentice Hall,
2002. Ch. 14 . This book has large scope, many examples, can serve as a quick
reference, but is not the best for teaching/learning the material.
jGuru Short Course: Enterprise JavaBeans Fundamentals (2000): good for a quick
overview and acquiring a taste of coding EJBs
http://developer.java.sun.com/developer/onlineTraining/EJBIntro/
Designing Enterprise Applications Using the J2EE platform: Java Blueprints online book on designing distributed applications; focus is on how to make
architectural decisions rather than on code
http://java.sun.com/blueprints/guidelines/designing_enterprise_applications_2e
/index.html
Developing Enterprise Applications Using the J2EE platform: - detailed but basis
tutorial to J2EE
http://developer.java.sun.com/developer/onlineTraining/J2EE/Intro2/j2ee.html
The J2EE Tutorial (2002): the updated Sun tutorial.
http://java.sun.com/j2ee/tutorial/1_3-fcs/
Enterprise JavaBeans 2.1 Documentation: what you need to read if you have a
substantial project: http://java.sun.com/products/ejb/javadoc-2_1-pfd/
Sun Microsystems, Inc. EJB Home page: http://java.sun.com/products/ejb/
FallMicrosystems,
2003
CS 667:
10. http://java.sun.com/j2ee/
Servlets, JSPs,
Sun
Inc. J2EE MET
Home
page:
EJBs
31