Friday 21 September 2012

The Flow of J2EE Application


The typical application flow is as follows:
1. A Web client requests a URL in the browser (input page).

2. The request is routed to the Web server over the Internet.

3. The Web server immediately passes the request to the Web server plug-in.
All requests go to the Web server plug-in first.

4. The Web server plug-in examines the URL, verifies the list of host name
aliases from which it will accept traffic based on the virtual host information,
and chooses a server to handle the request.

5. A stream is created. A stream is a connection to the Web container. It is
possible to maintain a connection (stream) over a number of requests. The
Web container receives the request and, based on the URL, dispatches it to
the proper servlet.

6. If the servlet class is not loaded, the dynamic class loader loads the servlet
(servlet init(), then doGet() or doPost()).

7. JNDI is used for lookup of either datasources or EJBs required by the servlet.

8. Depending upon whether a datasource is specified or an EJB is requested,
the JNDI directs the servlet:

– To the corresponding database and gets a connection from its connection
pool in the case of a data source.
– To the corresponding EJB container, which then instantiates the EJB when
an EJB is requested.

9. If the EJB request involves an SQL transaction, it goes back to the JNDI to
look up the datasource.

10.The SQL statement is executed and the retrieved data is sent back either to
the servlet or to the EJB.

11.Data beans are created and handed off to JSPs in the case of EJBs.

12.The servlet sends data to JSPs.

13.The JSP generates the HTML that is sent back through the plug-in to the Web
server.

14.The Web server sends the output page (output HTML) to the browser.


Sunday 9 September 2012

Heap Dumps generation



Heap Dumps
Heap dump is a text file which keeps record of all objects in the java heap. It contains the information like size and address of all objects, as well as the addresses of all objects that it references.

Heap dump is nothing but a snap shot of our JVM’s Heap memory,contains all active java objects and their activities which object doing what and how much memory it is occupying.

whenever OutOfMemoryException occurs Websphere application Server creates heap dump in profile's home directory.

It is useful in situations, most notable for memory leak problems or memory allocation problems for exaple OutOfMemoryExceptions.

Automated heap dump generation

Click Servers > Application servers in the administrative console navigation tree.
Click server_name >Performance and Diagnostic Advisor Configuration.
Click the Runtime tab.
Select the Enable automatic heap dump collection check box.
Click OK.

Generating Heap dump Manually

set jvm [$AdminControl completeObjectName type=JVM,process=server1,*]
Where server1 is the name of application server for which we want heapdump.
For generating heap dump:
$AdminControl invoke $jvm generateHeapDump.


In order to manually generate a heapdump, you must first enable signal-based heapdumps by setting the IBM_HEAPDUMP=TRUE and IBM_NOSIGHANDLER=TRUE environmental variables.

What is the effect of changing max heap and min heap size of a jvm
                             Max Heap Size: When you have established the maximum heap size that you need,  you might want to set the minimum heap size to the same value;  Using the same values is not usually a good idea, because it delays the start of garbage collection until the heap is full. The first time that the Garbage Collector runs, therefore, becomes a very expensive operation. Also, the heap is more likely to be fragmented and require a heap compaction. Again this is a very expensive operation.
                             Min Heap Size: The recommendation is to start your application with the minimum heap size that it needs. When it starts up, the Garbage Collector will run often and, because the heap is small, it runs efficiently.

Whats the max heap allowed by AIX
                             Minimum heap size allowed
                             AIX:             16 MB
                             LINUX:          16MB
                             WINDOWS:    16 MB

                             Maximum heap size allowed
          AIX: Half the available memory with a minimum of 16 MB and a maximum of 512 MB
          LINUX:          Half the available memory with a minimum of 16 MB and a maximum of 512 MB
          WINDOWS: Half the real (physical or RAM) memory with a minimum of 16 MB and a maximum of 2 GB

What are the effects of large initial heap/max heap
                   The time between garbage collections will increase, but the garbage collection will take longer, which indirectly effects the turn-around-time of the applicaiton performance.
                                                (or)
                             Because it delays the start of garbage collection until the heap is full. The first time that the Garbage Collector runs, therefore, becomes a very expensive operation. Also, the heap is more likely to be fragmented and require a heap compaction. Again this is a very expensive operation.




Hung thread detection in WebSphere Application Server


A Java dump, also known as a Java core, Java thread dump, or a thread dump is a file that contains the information about thread like active thread, hung thread, dead lock, runnable and inactive thread info.
The thread dump is a snapshot of exactly what's executing at a moment in time.
We are finding thread hung in logs like this.
WSVR0605W: Thread threadname has been active for hangtime and may be hung.  There are totalthreads  threads in total in the server that may be hung.
Example:
WSVR0605W: Thread "WebContainer : 4" (0000003b) has been active for 758692 milliseconds and may be hung.  There is/are 1 thread(s) in total in the server that may be hung.
WSVR0605W: Thread "WebContainer : 4" (0000003b) has been active for 758693 milliseconds and may be hung.  There is/are 2 thread(s) in total in the server that may behung .
.
.
WSVR0605W: Thread "WebContainer : 4" (0000003b) has been active for 75869N milliseconds and may be hung.  There is/are n thread(s) in total in the server that may be hung.

Automated Thread dump generation.

Navigate to Servers --> Applicaiton Servers --> server_name --> administration --> custom propertie>New-
com.ibm.websphere.threadmonitor.dump.java    when value is set to 'true', creates a java core when a hung thread is detected false
ManualThread dump generation
set jvm [$AdminControl completeObjectName type=JVM,process=server1,*]
Where server1 is the name of application server for which we want heapdump.
$AdminControl invoke $jvm dumpThreads.

Genarate through python Script
Download the file windows_hang.py and copy the file to your <PROFILE_ROOT>\bin directory. If instead copied to <WAS_HOME>\bin, the default server, which may be the deployment manger (dmgr), will be accessed when wsadmin.bat is launched.


NOTE: This script only works for WebSphere Application Server 6.1 and higher.


To launch the script to produce 3 javacores spaced 2 minutes apart, run this command:

wsadmin -lang jython -f windows_hang.py -j -s SERVER_NAME

Replacing SERVER_NAME with your server's name in AppServer profile root.
EX:
javacore.date.time.id.txt
For example: javacore.20120909.204717.27050.t

IN Linux.
use kill -3 PID on unix/linux machines.

Thread Monitoring settings

    • Navigate to Servers --> Applicaiton Servers --> server_name --> administration --> custom properties
    • Add the following 4 custom properties
#
Property
Description
Default
A
com.ibm.websphere.threadmonitor.interval
 How frequently thread monitor should check all the managed threads for hung threads
180
B
com.ibm.websphere.threadmonitor.threshold
 After how many seconds a thread can be considered as hung
600
C
com.ibm.websphere.threadmonitor.false.alarm.threshold
The number of times that false alarms can occur before automatically increasing the threshold (T)
100
D
com.ibm.websphere.threadmonitor.dump.java
 when set to 'true', creates a java core when a hung thread is detected
false
    • Click OK and save the changes.
    • Sync the changes and restart the servers for the changes to take effect.