Professional Documents
Culture Documents
J2EEBestPractices Webinar
J2EEBestPractices Webinar
Push
J2EE your development
Best furtherusing
Practices Tech
Days
a Real-life Example.
Carol McDonald
Technology Evangelist
carol.mcdonald@sun.com
1
Sun™
Push
eBay yourArchitecture:
development further How to Go… Tech
Days
Configuration
eBayISAPI.dll
Security
Logging
EJB™ Container
Integration Tier
Monolithic Layered
Proprietary Loosely coupled
Modular
Standards based
2
Sun™
Push
What yourIs
development
Needed… further Tech
Days
3
Sun™
™
Push your development further
Core J2EE Patterns Tech
Days
http://java.sun.com/blueprints
Java Technology Series
TM
books
– Designing Enterprise Applications
with the J2EE Platform, 2nd Edition
● Covers J2EE 1.3 platform features
TM
5
Sun™
Push your development further
Agenda Tech
Days
6
Sun™
Push your development further Tech
Days
7
Sun™
Push your development further
Layering Tech
Days
8
Sun™
Push your development
Rationale further
for Layers Tech
Days
9
Sun™
Push your development
Model further
View Controller Tech
Days
10
Sun™
Push your development
Model further
View Controller Tech
Days
✗Separate:
●Model data
●Control
Forward Business
Event Event
Response
JSP Bean
View Model Model
Data
MVC Pattern
11
Sun™
Push your development
Service to Worker further Tech
Days
6. Execute Command
2. Common logic Helper
8. Dispatch
10. Response 7. Business
View logic
Dispatcher
Servlet View
Helper
JSP Data
9. Get Data Bean
Java™ Objects
12
Sun™
Push
Useyour
andevelopment further
MVC Framework Tech
Days
Modularity
Maintainability
13
Sun™
Push your development further
Struts MVC Framework Tech
Days
Struts-config.xml
........
Action
........
........
14
Sun™
Push your development
Struts and Core further
J2EE Patterns Tech
Days
Client
Uses
Response
Action Action
JSP View
Forward Form
Request Handler
Request
Intercepting
Filter
Front
Controller
Command ? Business
Service
Client
Navigator Dispatcher
16
Sun™
Business Delegate
Push your development further Tech
Days
Client
independent from
ejb tier
Ejb details
hidden
Mock objects can
be used to test
client w/o ejb tier
17
Sun™
Push your development
Ebay.com: further
Presentation Tier Tech
Days
Request
Intercepting Front Business Business
Command
Filter Controller Delegate Service
Client Service
Locator
Navigator Dispatcher
18
Sun™
Push
Cacheyour development further
for Performance Tech
Days
19
Sun™
Push your development
Service Locatorfurther
Pattern Tech
Days
DON'T DO
EJB JMS EJB JMS
Client Client Client Client
Service
Locator
JNDI
20
Sun™
Push your development further
ServiceLocator code Tech
Days
21
Sun™
Push your development further
ServiceLocator code Tech
Days
22
Sun™
Push your development
Client code further Tech
Days
01 ServiceLocator serviceLocator =
02 ServiceLocator.getInstance();
03 try {
04 ProjectHome projectHome =(ProjectHome)
05 serviceLocator.getHome(“ProjectHome.class”);
...
23
Sun™
Push
Tipsyour
fordevelopment
Servletfurther
Performance Tech
Days
24
Sun™
Push your development
Servlet Tips further Tech
Days
25
Sun™
Push
Tipsyour
fordevelopment
JSPs further Tech
Days
26
Sun™
Push your development further Tech
Days
27
Sun™
Push
Do your
youdevelopment
need EJBs?further Tech
Days
29
Sun™
Push
ebay yourUse
development
case further
realization Tech
Days
View Items
by category
Any User
30
Sun™
Push
View yourItems
development further Requirements
Design Tech
Days
Convert to
Biz Command
Assemble
Data
Use case
Biz logic
Retrieve
Data by category
31
Sun™
Push
View yourItems
development further
Design Tech
Days
Transfer
Action Object Transfer
Command Object
Assembler
32
Sun™
Push yourApplication
Core development further
Design Patterns Tech
Days
FRONT
VIEW
CONTROLLER
DATA
MODEL Value List Handler ACCESS
OBJECT
COMMAND DATA
TRANSFER DATA
object TRANSFER
OBJECT
OBJECT
Client
Object EJB
Network
boundary
GetCity()
GetState()
DON'T DO
Before GetZipCode()
too much network traffic
GetItemInfo()
Return DTO
GetCity()
After DO
GetState()
Reduce network traffic
GetZipCode() with Data Transfer Object
Network
boundary
34
Sun™
Push yourApplication
Core development further
Design Patterns Tech
Days
FRONT
VIEW
CONTROLLER
DATA
MODEL Value List Handler ACCESS
OBJECT
COMMAND DATA
TRANSFER DATA
object TRANSFER
OBJECT
OBJECT
36
Sun™
Push your
EJB Tierdevelopment
patterns further Tech
Days
FRONT
VIEW
CONTROLLER
DATA
MODEL Value List Handler ACCESS
OBJECT
COMMAND DATA
TRANSFER DATA
object TRANSFER
OBJECT
OBJECT
<<List>>
Client ValueListHandler ValueList TransferObject
DataAccessObject
39
Sun™
Push your development
Example CachingfurtherResults Tech
Days
Create
Return Sub List Return Value List
Get Next
Get Sub-list
Return Sub-list
Get Current
Get Current
Return Value Object
Get Size
Get Size
Return Size
40
Sun™
Push your development
Returning Searchfurther
Results Options Tech
Days
42
Sun™
Push yourApplication
Core development further
Design Patterns Tech
Days
FRONT
VIEW
CONTROLLER
DATA
MODEL Value List Handler ACCESS
OBJECT
COMMAND DATA
TRANSFER DATA
object TRANSFER
OBJECT
OBJECT
3. process
Java™
Use the Session Facade: Object
44
Sun™
Push
Useyour
thedevelopment
Session further
Facade pattern Tech
Days
Network
Do:
EJB
Network F
a
Client c
a
d
e
Network
46
Sun™
Push your development
Design your localfurther& remote EJBs Tech
Days
WebComp
onent
Remote WebComp
Facade
WebComp
onent Determine which EJBs
onent will be collocated within same
Business
Delegate
VM
LineItem
Relationship Admin
Remote CreditCard
Reference Facade Purchase Order
Address
47
Sun™
Push
Useyour
thedevelopment
Session further
Facade Tech
Days
Commit
or
Start Prepare rollback
Check context
x to see if
updates will work
48
Sun™
Push your development
eBay.com: further
Business Tier Tech
Days
Application Domain
DAO Data
Controller Store
Source
Business
Command Context Logic/Data
Object
Application
Service
Business
Facade Object
Transfer
Object
Transfer Value
Object List
Core J2EE
Assembler Handler
Pattern
Strategy
49
Sun™
Push your development
Session EJB tips further Tech
Days
50
Sun™
Push your development
Stateless further
or Stateful Session EJB? Tech
Days
51
Sun™
Push your development further Tech
Days
Persistence
Best
Practices
52
Sun™
Push
Data yourAccess
development further
Options in J2EE: Tech
Days
Database
EJB Container
1) Bean provider manages State and Data consistency
2) Bean provider handles relationships and OR Mapping
54
Sun™
Push
CMP your2.0
development
Entity further
Beans Tech
Days
<<interface>>
java.io.Serializable
<<interface>>
javax.ejb.EnterpriseBean
<<interface>>
javax.ejb.EntityBean
55
Sun™
Push your development
Container further
Managed Relationships Tech
Days
Address
Order
Local Entity
1
Shipping Address
1 Local Entity
Client OrderBean Product
1 1
LineItems ProductOrdered
M M
OrderHome LineItem
Local Entity
EJB Tier
56
Sun™
Push your development
Accessors further and
for CMP CMR Tech
Days
57
Sun™
Push
CMP your2.0
development further
Relationship Handling Tech
Days
58
Sun™
Push your development
Example further
CMP Wizard Tech
Days
59
Sun™
Push your development
Advantages of further
CMP 2.0 Tech
Days
for developer
container manages the persistence and
the relationships, not you!
No SQL or persistence logic in source code
Freedom from maintaining interactions
with the data store
EJB™ Query Language (EJB QL)
Portable code
60
Sun™
Push your development
Advantages of further
CMP 2.0 Tech
Days
for Container
Optimization is possible because
persistent fields are only accessible
via get and set methods
Containers knows
Which field is being accessed
Whether or not field is in a relation
Whether of not there is a transaction
What's in cache
The current clustering environment
Can optimize based on above
61
Sun™
Push
Some your CMP
development further
Optimizations Tech
Days
Aggressive Loading
Loading data for Entities in a relationship in the
same query
Lazy Loading
Deferring loading of any data until it is accessed
Dirty Writes
Only update data which has been changed
in the database
62
Sun™
Push
CMP: yourStandard
development further
Vs. Tech
Days
Standard features
Declarative specification of:
Persistent attributes, abstract schema, relationships,
queries for finder/select methods(via EJBQL),
transactional attributes
Vendor specific
Tools, concurrency and consistency
semantics, caching semantics, performance
and usability
63
Sun™
Push your development
Pessimistic further
Locking Tech
Days
C1 C2
Time State in
Database
1000 Begin Txn
1000
read initial
balance w/Lock Begin Txn
.. read initial
balance += 1000 balance w/lock
(blocks ..)
..
2000 2000 Commit
2000 unblocks
balance +=
3000 3000 1000
Commit
“ Update-conflict” Detection
Concurrent Access to same Bean
C1 C2
Time State in
Database
1000 Begin Tx Begin Tx Retry
1000 1000
Select initial Select initial
balance balance
..
2000
..
balance += 1000 balance += 1000
update
2000 2000 ..
Commit update 3000
Exception
..
3000 Commit
65
Sun™
Push
Usageyour development
Guidelines further Tech
Days
Pessimistic Optimistic
Recommended for Use when
consistency when concurrent
concurrent updates access is mostly
would be frequent reading
For scalability
Requires
exception
SunOne deployment descriptor handling
<consistency>
<lock-when-loaded/>
</consistency>
66
Consistency Levels
further in CMP
Sun™
Push your development Tech
Days
67
Consistency Levels
further in CMP
Sun™
Push your development Tech
Days
68
Sun™
Push your development
Database further Modes
Isolation Tech
Days
70
Sun™
Push your development further Tech
Days
J2EE Performance
Tips:
how to get the most
out of your Application
Server
71
Sun™
Push your development
Application further
Resources Tech
Days
72
Sun™
Push
Tune yourApp
development
Serverfurther Tech
Days
73
Sun™
Push
Tune yourKey
development further
Container Parameters Tech
Days
74
Sun™
The EJB
Push your Container
development further Tech
Days
75
Sun™
Push your development
Entity Bean further Tech
Days
EjbActivate()
Does
not exist
NewInstance()
Bean pool size setEntityContext() unsetEntityContext()
minimizes creation
and initialization
pooled
ejbFind()
EJB
Instance
ejbCreate() ejbRemove()
ejbPostCreate()
ejbActivate() ejbPassivate()
Bean cache size
minimizes activation ready
business method
76
Sun™
Push your development
Entity Bean Cachingfurther Tech
Days
Commit Option A
At the end of the transaction, the instance stays
ready (cached) and the instance state is valid
Commit Option B
At the end of the transaction, the instance stays
ready (cached) but the instance state is NOT
valid ( ejbLoad will be called )
Commit Option C
At the end of the transaction, neither the instance
nor its state is valid (passivated)
Best Option: Usually B, if Entity will be
accessed again. do profiling with your
77 application on your app server
Sun™
Push your development
Stateful Session further
Bean Tech
Days
1) newInstance() ejbRemove()
2) setSessioncontext()
removal time out
3)ejbCreate() ejbPassivate()
Method-ready
78
Sun™
Stateless
Push Session
Bean
your development further Tech
Days
1) newInstance()
2) setSessioncontext() EjbRemove()
3)ejbCreate()
method-ready
pool
Business method
EJB
Instance
79
Sun™
Push
Theyour development
Sun's EJBfurther
Container Tech
Days
Cache Tunables
commit-option (B|C) (entity beans)
Max-cache-size (0 = unbounded)
cache-idle-timeout-in-seconds
(0 = indefinitely)
removal-timeout-in-seconds
(stateful session)
Adjust cache size: Increase until a good cache hit
rate is reached
– For fine tuning, understand a bean’s usage
pattern (reads, updates, creates, removes ...)
80
Sun™
Push
Theyour development
Sun's EJBfurther
Container Tech
Days
Pool Tunables
steady-pool-size (not for message-
driven)
max-pool-size (0 = unbounded)
pool-idle-timeout-in-seconds
(0 = indefinitely)
Adjust a bean’s pool size:
– Increase when observing excessive creation
and deletion of bean instances
– Decrease when accumulating a large number
of instances in pool
81
Sun™
Push
Theyour development
Sun's EJBfurther
Container Tech
Days
82
Sun™
Push
JDBC your development
Tips further Tech
Days
Statement caching
– 15–25% Performance improvement
– Requires knowledge of database specific
properties, i.e. Oracle
<jdbc-connection-pool datasource-classname=
"oracle.jdbc.pool.OracleDataSource" ...>
<property name="ImplicitCachingEnabled"
value="true"/>
<property name="MaxStatements" value="200"/>
</jdbc-connection-pool>
84
Sun™
Push your development
Database further
Performance Tech
Days
85
Sun™
Push your developmentTips
Performance furtherSummary Tech
Days
86
Sun™
Push
EJB your development
Tips Summaryfurther Tech
Days
87
Sun™
Push your development
Manage further
Expensive Resources Tech
Days
88
Sun™
Push your development
Design Patterns further
can Tech
Days
Session Facade
Service Locator
Value List Handler
Data Transfer Object
89
Sun™
Push your development further Tech
Days
Some JMS
Messaging,
J2EE Best
Practices
90
Sun™
Push your
JMS development further
Communication Tech
Days
JMS Provider
Producer Destination Consumer
Loosely coupled:
– Producer doesn't need to know consumers or consumer method
arguments
Reliable:
– Message is stored until it can be delivered
Asynchronous
– Producer is not blocked, consumer does not have to be available
91
Sun™
Push
Useyour
JMSdevelopment
for further Tech
Days
Asynchronous Interaction
Concurrent processing
Scalability
Batch processing
Broadcasting messages to multiple
recipients
Reliable messaging
Messaging with Transaction Support
Loose Coupling
92
Sun™
Push your development further
Message-Driven Bean Tech
Days
Container
JMS Container
Provider
Desti MDB
Msg-
Consume MDB
Queue
n- Consumer driven
Instances
Instances
r
ation Bean
Msg-Driven
Bean Class
93
Sun™
Push yourFacade
MDB development further
Pattern Tech
Days
Mailer MDB
EJB
Asynchronous
Message
LineItem Tier
Delivery Order Entity
Card
Approval 1:m
Entity
MDB Purchase
Order Entity
Address
Entity
95
Sun™
Push
XML yourMessage
development further
Facade Tech
Days
Ref JavaOne
96
Sun™
Push
Tips your
fordevelopment further
XML messaging Tech
Days
97
Sun™
Push your development further
Asynchronous Web Services Tech
Days
MDB
ERP
98
Sun™
Push your development
FUTURE: further
JBI and J2EE Tech
Days
Web App.
EJB JBI BP Module
Module
Module (WS-BPEL and
(JSP/Servlet
(EJB API) WSDL APIs)
API)
208 “Machine”
SPI
208 “Binding”
SPI
Binding Framework
99
Sun™
Push your development further Tech
Days
Tools
Summary
Resources
100
Sun™
Push your
Use development further
Tools Tech
Days
101
Sun™
Push your development further
Summary Tech
Days
http://java.sun.com/blueprints
Designing Enterprise Applications
with the J2EE Platform, 2nd Edition
102
Sun™
Push your development further
Resources: Tech
Days
Carol McDonald
Technology Evangelist
carol.mcdonald@sun.com
104