J2EE: Best Practices for Application Development and Achieving High-Volume Throughput
Michael S Pallos, MBASession: 3567, 4:30 pmAugust 11, 2003
Agenda
• Architecture Overview• WebSphere Application Server scalability• A proven design pattern• Servlets and Java Server Pages• Sessions• Memory usage
Agenda (continued)
• Enterprise Java Beans• Connecting to legacy systems and
databases– Connection Pooling– Prepared Statements– Session Database– Java Database Connector (JDBC)
• Systems Management
Architectural Overview
WebSphere Application Server –
•3.5 & 4.x
• 5.x
WAS Architecture (3.5 & 4.x)
Admin Server
EmbeddedHTTP Server
Servlet
Admin Server - JVM
Application Server - JVM
HTTP Server
Plug-in
plugin-cfg.xml
EJB
Servlet
JSP
Web Container
EJB ContainerIIOP
HTTP(S)
HTTP(S)
Node (machine)
Client PC
WebBrowser
ApplicationDatabase
WAS Admin ConsoleGUI Administration
RepositoryJava Admin Consolehttp://localhost:9090/admin
WebSphere ControlProgram (WSCP)CMDXMLConfig
WAS Architecture (5.x)
plugin-cfg.xml
WAS Administrative ConsoleStandard J2EE 1.3 Web App
HTTP(S)
EJB
JSP
Servlet
XML Configuration Files
wsadmin Scripting command line Tool
(BSF & JMX based)
AppServerXClient PC
WebBrowser HTTP(S)
HttpServer1Application Instance (JVM)
HTTP Server Servlet Container
EmbeddedHTTP Server
Plug-in
ApplicationDatabase
EJB Container
Application Server Clones
• Server Groups– Templates Application server and contents– Changes to group propagate to all clones– Not JVMs and Not Application Servers
• Clones– Exact Copy configured/functional application server– Managed by parent server group
• Vertical Clones– Additional running copy of Application Server on same node
• Horizontal Clone– Additional running copy of Application Server on different node
Vertical Cloning
ApplicationDatabase
AdministrationRepository
Admin Server - JVM
Application Server #1 Clone A - JVM
Web BrowserClient
HTTP Server
Plug-in
plugin-cfg.xml
EJBsServlets
JSPs
Web Container
EJB Container
Node (machine #2)
Application Server #1 Clone B - JVM
EJBsServlets
JSPs
Web Container
EJB Container
HTTP(S)
Machine #1
Vertical Cloning• Strengths
– Improved throughput on large machines– Easy to maintain and support
• Weaknesses– Limited available resources on single machine
• Threats– Single point of failure– Memory/CPU overhead if have too many clones
• Opportunities– Can be combined with other topologies
Horizontal Cloning
ApplicationDatabase
AdministrationRepository
Admin Server - JVM
Application Server #1 Clone A - JVM
Web BrowserClient
HTTP Server
Plug-in
plugin-cfg.xml
EJBsServlets
JSPs
Web Container
EJB Container
Node (machine #2)HTTP(S)
machine #1 Admin Server - JVM
Application Server #1 Clone A - JVM
EJBsServlets
JSPs
Web Container
EJB Container
Node (machine #3)
Horizontal Cloning
• Strengths– Distribution of load among machines– Failover support if one machine goes down– More machines provide more CPU power
• Weaknesses– More difficult to setup and maintain– More $$ for hardware and software
• Threats– Requires session persistency– Increased network traffic between machines
• Opportunities– Can increase number of machines as needed, highly scalable
Design Pattern
Model View Controller
Design Pattern
• MVC– Model – responsible for the data or
business process (persistence/business logic)
– View – responsible for the user’s display (CUI/GUI)
– Controller – is responsible for the flow of the application (traffic cop)
Model-View-Controller
HTML
HTML
Beans
BeansJSP
Servlet
View ModelController
Submit
Request
Forward
Response
Cookie
DB
Servlets &
Java Server Pages (JSP)
Servlets & JSP
• Small, thin Servlets– 50 < Lines of code
• Avoid single-threaded• Avoid System.Out.Println() • Avoid string concatenation
– Use Java StringBuffer• Avoid Serialization• Synchronize statements are costly
Synchronize statementsH
TTP
Req
uest
s/Se
c(H
undr
eds)
No synchronization Synchronization0
10
20
30
40
50
60
www.developer.ibm.com
Sessions
Sessions
• Keep session objects small < 2k• Designing session data
– Life of Session• Persistent session as dedicated data
source– Single row persistence
• Multi row persistence w/ a large field
– Keep large object graphs out
Sessions (continued)
• Do not create HttpSession objects by default on JSPs– Even though it is part of J2EE spec– Use <%@page session=”false”%>
• Writing your own JDBC calls
Data Source & Backend Systems
Data Source
Connection Pooling
Prepare Statements
Session Database
Java Database Connector (JDBC)
Data Source
• Set up data-source once– Object initialization phase– Servlet.init(), ejbActive() – Release to connection pool
Data Source CachingH
TTP
Req
uest
s/Se
c(H
undr
eds)
Cached DatasourceNon-cached Datasource
0102030405060
8070
www.developer.ibm.com
Connection Pooling
• Obtain and close the connection in the same method
• JNDI lookups are expensive• Do not declare connections as static objects• Do not close connections in the finalize
method• If you open a connection, close the
connection• Do not manage data access in Container
Managed Persistence (CMP) beans
Connection PoolingH
TTP
Req
uest
s/Se
c(H
undr
eds)
Conn. Pooling No Conn. Pooling0102030405060
8070
www.developer.ibm.com
Prepared Statements
• Statement– Compiled prior to execution
• Prepared Statement– Separates the compilation process and adding
substitution variables– Use this one!
• Callable Statement– Removes SQL entirely by making a Stored
Procedure Call
How is SQL effecting CPU?
Session Persistence
• Enable session persistence• Reduce session size• Release HttpSession when finished• Choose persistence options• Avoid creating HttpSession by JSP
default– <%@page session=“false “%>
• Tune the cache size
Session Persistence
Persist!
Cache! InputWaitsUntil
SessionsExpire
OverflowConfigured
(Default)
Add UntilHeap
isExceeded
Persistence
CacheExceeded
(1000 Default)
Yes
Yes
Yes
No
NoNo
In-memory cache overflow algorithm.
Session Persistence (continued)
• Tune the cache size• Add additional application server clones• Tune the session timeout interval• Tune multi-row persistence management
Simplified multi-row session representation.
Session ID
Web Application
Property Small Value Big Value
AB12345 CandleDemo CandleDemo.First.Name “Michael”
AB12345 CandleDemo CandleDemo.last.Name “Pallos”
AB12345 CandleDemo CandleDemo.String A huge string…
JDBC
• Choose your driver wisely• JDBC API levels supported
– JDBC 1.0 is the default– JDBC 2.0 is required by WAS
• Select a Type 2 JDBC driver– When incorporating distributed transactions and
cataloged databases, Type 2 JDBC driver offers the best performance.
DB Connections
Memory Usage
Memory Usage• Allocation
– Avoid dynamic allocation– Hidden object creation
• Streams• Print writer
• Garbage collections– Pool and reuse objects– Servlet guideline < 50 lines of code
• Health checks– Watch for memory usage growth– Small page size– Small message size
Enterprise Java Beans (EJB)
Enterprise Java Beans• One size DOES NOT fit all
– Ensure you need distributed objects• Alternative to Entity beans
– Tactical– Non-Scalable
• Access Entity beans from Session beans– Triple performance as opposed to accessing
directly from client– Reduces the number of remote method calls– Single transaction context
Accessing Entity BeansH
TTP
Req
uest
s/Se
c(H
undr
eds)
Access from Client CodeAccess via Session Bean05
10
15
20
25
30
www.developer.ibm.com
Enterprise Java Beans
• Read-Only getter methods / Access Intent• Reduce Tx Level
– TRANSACTION_READ_UNCOMMITTED– TRANSACTION_READ_COMMITTED– TRANSACTION_REPEATABLE_READ– TRANSACTION_SERIALIZABLE
• Cache EJB Home– Reduce JNDI calls
EJB CachingH
TTP
Req
uest
s/Se
c(H
undr
eds)
Client Code Home Caching010203040506070
Session Beanwww.developer.ibm.com
Thread Pools
• Create thread pool funnel• Minimize the size of thread pools
ApplicationDatabase
Web BrowserClient
HTTP Server
& plug-in
Application ServerWeb ContainerEJB Container
Data Source
Listener Threads Pool
Servlet/JSP Thread Pool
Database Connection Pool
100 20 10
Request for Static Pages & Dynamic Pages Queue Queue
Servlet Threads Requesting DB Connections
Dynamic PagesStatic Pages
Monitoring Tools
What to look for.
Analyst Recommendations.
Monitoring Tools
• Customer (End-User) Experience – End-To-End (More than just J2EE environment)– Latency Time
• WebSphere Application Server– Instrumentation (class loader hooks)– Workflow Analysis– IBM API (PMI)– Application Server Log– SMF 120 Records (mainframe)
Monitoring Tools (continued)
• Legacy Systems– CICS– IMS
• Database– Connection Pools– DBMS
WebSphere Application Server
Source:
Summary
• Distributed Applications are Complex– A proven design pattern– Servlets and Java Server Pages (JSP)– Sessions– Connecting to legacy systems and
databases• Connection Pooling• Prepared Statements• Session Database• Java Database Connector
Summary• Distributed Applications are Complex
– Memory usage– Enterprise Java Beans– Solid Monitoring Tool
• Hire Experience (consultants)– Follow road to success
• Architecture• Development• Testing • Deployment (Migration)• Monitoring
– Best Practices
Thank you for your participation• WebSphere Advisor, Nov/Dec 2002
– Best Practices: Application Performance– http://advisor.com/doc/10050
• WebSphere Advisor, April 2003– 5 Java Performance Best Practices– http://advisor.com
• WebSphere Developers Journal, May/June 2003– Best Practices: WAS / DBMS Persistence– http://www.candle.com (Link from home page, Aug 03)
• Webinar expoQ Dailey– Best Practices: J2EE– http://www.ebizq.net/expoq/events/event22.html
A note from Legal…This presentation represents customer's use of Candle's products.
Because each customer's systems, uses and needs are different, Candle does not guarantee comparable results. Candle does not warrant the accuracy of information provided by customers or Candle's business partners. If a product mentioned herein is not yet generally available to Candle's commercial customers, Candle has no obligation to develop the product, to commercially release it, or to incorporate any features or functionalities of the product into any future commercially released product.
Other product names and terms in this document may be trademarks of their respective companies.
-- Candle Corporation Legal Department