Professional Documents
Culture Documents
Ravi Saraswathi
August 25, 2009
System Architecture Non-Dynapub Web application architecture Apache-Tomcat connection (Active-Active, Active-Passive)
Tomcat Architecture
Basics
Architecture Basics
XML Representation One Server - One JVM Service/Engine/Host/Context
All are Containers All implement Lifecycle Listeners
of Tomcat Containers
<Server>
<Service> <Connector /> <Engine> <Host> <Context> </Context> </Host> </Engine> </Service> </Server>
Lifecycle Events
How objects get initialized, started & stopped
Object relationships are established during creation (digester parsing) Digester : XML-to-Java-object mapping
Service represents grouping of connectors Connector for protocol handling Engine for request processing Host for virtual host handling Context for web application execution
Source: http://www.vsj.co.uk/java/display.asp?id=319
UML Associations: 1..* One or more 0..1 Zero or one 1 One only 0..* Zero or more
Realm
For Container Managed Security for Web apps (<security-constraint> elements, and
a <login-config> element in web.xml)
Database of username, Passwords & Roles Realm can be defined in Engine, Host or Context
JDBCRealm DataSourceRealm JNDIRealm MemoryRealm (tomcat-users.xml) JAASRealm UserDatabaseRealm
Architecture Basics
context.xml
The Context element represents a web application Process each HTTP request based on the context path Each Context MUST have a unique context path. META-INF/context.xml has all the resources E.g.. Database Can use for configuring web application class loader, session manager or Realm (database of users & roles).
Valve
Context
Valves JSPs
Servlets
Valves
Architecture Basics - Valve Valve allows you to associate an instance of a Java class with a Engine/Host/Context container Component that can be inserted into the request processing pipeline Will preprocess all requests received by the connectors Similar concept to Servlet filter Interceptor Pattern Request processing pipeline Configured using a <Valve> element in the server.xml file
Custom Valve
Register in Server.xml
Tomcat Listeners
Tomcat Listeners
Java Code modules that Performs actions when events occur
Just before startup During startup Just after startup Just before stop During stop Just after stop
Tomcat Default Listeners APR Lifecycle Listener Checks for the presence of the APR/native library and loads the library if it is present Jasper Listener Initializes the Jasper 2 JSP engine before any web applications that may use it are loaded
Server Lifecycle Listener Initializes the MBeanServer for the MBeans that may be used to manage Tomcat via JMX. Global Resources Lifecycle Listener Initializes the Global JNDI resources defined in server.xml as part of the Global Resource element
Custom Listeners
JVM
Tomcat produces very little GC !
GC
Java objects
Minor GC
Full GC
Young generation
New space, when you create an object memory here is allocated.
Tenured generation
If it last longer than a while it is moved here
Permanent generation
Hold data needed by VM to describe objects (describing classes & Methods)
60 % of available memory
Setting too high can cause wasted memory. 32-bit OS cap the heap size at between 1.5 and 2.5GB
Collectors
Mark and sweep Minimizes pause times, avoid "stopping the world Collect the tenured generation concurrently with the execution of the application Start VM - mark each object Remove -XX:+UseConcMarkSweepGC
Throughput collector Parallel Collector Distributes GC load across CPUs. Maximize throughput XX:+UseParallelGC Can be used for Multiprocessor Machines (with four or more processors)
Not Good: - Entire heap Decreased only by Good: - Young Generation Decreased by 3968K 1602k
This indicates that only about 40% objects in the young generation were garbage and the rest survive the collection and are being promoted into the old generation.
With an 8 Mbyte size, most of young generation is garbage at the time of the minor collection.
[GC [DefNew: : 8128K->64K(8128K), 0.0453670 secs] 13000K->7427K(32704K), 0.0454906 secs] Good: - Young Generation Decreased Good: - Total heap is decreased by 5573k 8064k
With an 8 Byte Young Generation size, most of young generation is garbage at the time of the minor collection
Connectors
Catalin a Coyote HTTP Connector webap p Servlet Servlet Apache httpd Coyote AJP Connector context war file separate class loader
Connectors
Connectors
NIO Allows a Servlet to process I/O asynchronously Available on Tomcat6 (Current Standard is Tomcat 5.5.27) NIO can be used to keep a connection open without wasting waiting threads. Tomcat 6 provide CometProcessor API to facilitate event-based model.
Configure mod_proxy
Httpd.conf
Consumer
Apache Host -1
Apache Host -2
Tomcat Host -3
Databas e
Configure mod_jk
Httpd.conf
Consumer
1. Add mod_jk modules 2. Add workers. properties 3. Add the URL pattern (JkMount /myproject/* worker name
Apache Host -1
Apache Host -2
Tomcat Host -3
Databas e
Mod_JK vs Mod_Proxy
Mod_jk Mod_Proxy
Mod_JK = AJP Connector Web server plug-in/ Tomcat Redirector Supports load balancing
Connectors available for FTP, HTTP, AJP or HTTPS mod_proxy, mod_proxy_balancer, mod_proxy_http, and mod_proxy_ajp Easy to Configure
Native mod_jk download available for different OS. Very Fast Complex Configuration Let Apache serve static pages
Mod_proxy modules comes as part of apache Lacks sticky session load balancing. mod_proxy_ajp supports load balancing
HTTP Connector Options MaxThreads Maximum number of simultaneous requests To Increase CPU usage Increase (from 150) MinSpareThreads Number of threads created at start time MaxSpareThreads Unused threads allowed to exist
AcceptCount Max incoming connections Increase to avoid connection refused errors Connectiontimeout Number of milliseconds wait for the request URL to be presented
Class loaders
Class loaders
Find Class files Parent Child relation ship Delegate the request to parent class loader
Class loaders
Tomcat 5.5
Tomcat 6.0
Class loaders
1. Bootstrap JVM Run time classes 2. System Load classes to initialize Tomcat - $CATALINA_HOME/bin 3. Common $CATALINA_HOME/lib, as well as classes and resources in JAR files. Preferably no Application Classes
Class loaders
4. Catalina Tomcat classes $CATALINA_HOME/server/classes & lib 5. Shared Put classes and resources that you wish to share across ALL web applications - $CATALINA_BASE/shared 6. WebAppX /WEB-INF/classes and /WEB-INF/lib
Deployment
HCM Console Copy the files to webapps folder HCM Composition Packages
From ADM Host - ssh root@<hostname> hcmbcs --sync --profile=<profile_id>)
Use Maven or Ant Copy to a Shortcut or Symbolic Link (webapps) Use the -d Option of javac or IDE
.war files will be deployed /ROOT is hardcoded for path= Runtime deployment triggered by Lifecycle Event
Main programs are HostConfig.java & StandardHost.java
Deployment Descriptor
Works in Tomcat 6 Supports Servlets 2.5 vs. 2.4 or JSP 2.1 vs. JSP 2.0
Works in Tomcat 5 and 6 For JSP expression language 2.4 or later required.
Maven
Maven manage a project's build, reporting and documentation All the information required for the build is contained in a single POM, Project Object Model, XML file The developer provides information about what is being built and not how The POM contains detailed METADATA information about the project:
Versioning and configuration management Dependencies Project structure Application and testing resources
Maven - POM
Apache Tomcat
Apache Host -1
Apache Host -1 Apache & Tomcat Host -1 Apache & Tomcat Host -2
Tomcat Host -1
Tomcat Host -2
Databas e
Databas e
Apache Host -1
Apache Host -2
Apache Host -1
Apache Host -2
Tomcat Host -2
Databas e
Databas e
To mca t Hos t -4
Databas e
Tomcat JNDI
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential Copied from book Professional Apache Tomcat 5
The GlobalNamingResources element - defines the global JNDI resources for the Server Per-web-application Initial Context can be configured in context.xml (META-INF) Reference the JNDI resources in web.xml
Servlet
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential
Servlet - Basics
Servlet containers usually manage concurrent requests by creating a new Java thread for each request.
JspServlet.java - handles all JSP files Request response model Read form data Read HTTP request headers Set HTTP Status codes and response headers Use cookies and session tracking Share data among Servlets.
Init
Servlet loaded in to memory using the init call
Service
For each incoming request a service() is called
Destroy
Called once Servlet is garbage collected from the memory
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential
Filter
Change client request/response before/after it goes to Servlet. Within filter-mapping Element define the <url-pattern>
Listener
Servlet Context events get notified when web apps deployed /shutdown etc HTTP Session Events get notified with session got activated etc
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential
<security-constraint>
Defines the access privileges to a collection of resources defined by the <web-resource-collection> element.
<login-config> configure how the user is authenticated; the realm name that should be
used for this application; and the attributes that are needed by the form login mechanism
JMX
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential
MBeans Usage Get and set applications configuration (Pull) Collect statistics (Pull) Performance Resources usage Problems Notify events (Push) Faults State changes
JMX Architecture
Components:
Managed (Instrumented) Resources Agents (and their services) Connectors / Adapters
Goals:
Instrument Java Code Create Smart Java Agents Implement Management Middleware Integrate with Industry Standard Management Platforms
Questions ?
Operations Track Ver. 3/Course Title Ver.# /AOL LLC Privileged and Confidential