Questions ‘n’ Answers – Technology

Just another WordPress.com weblog

Archive for the ‘Uncategorized’ Category

What are good Java Profiling Tools?

Posted by qnaguru on September 17, 2009

Java Profiling Tools included in JDK:

Visual VM – <JDK_HOME>\bin\jvisualvm.exe

This is a very good visual tool, that automatically connects to all local java processes and provides details on them.

JConsole – <JDK_HOME>\bin\jconsole.exe

This is very similar to Visual VM.

JStat - <JDK_HOME>\bin\jstat.exe

This is Java Virtual Machine statistics monitoring tool. You need to know jvmid of the java vm to use it.

JHat – <JDK_HOME>\bin\jhat.exe

This is a Java Heap Analysis Tool.

Other Java Profiling Tools:

StackProbe Profiler

http://www.stackprobe.com/

JRockit Mission Control

http://www.oracle.com/technology/products/jrockit/missioncontrol/index.html

Eclipse Memory Analyzer Tool

http://www.eclipse.org/mat/

YourKit Java Profiler

http://www.yourkit.com/

Posted in Java, Performance, Uncategorized | Tagged: , , , | Leave a Comment »

How can we get a thread dump of running java process

Posted by qnaguru on September 3, 2009

First find out java processes:

# jps
19420 MyServer
19843 Jps
8259 Bootstrap

Then using that process id, run jstack to get a thread dump:

# jstack 19420

NOTE: jps and jstack are JDK tools.

Another way to get a thread dump would be to use:

# kill -3 19420

Posted in Java, Performance, Uncategorized | Leave a Comment »

How to configure Jetty (in various ways)?

Posted by qnaguru on November 18, 2008

Setting up Apache/Jetty (to talk to one another i.e apache will forward requests to jetty)

For Jetty – Edit the file: <JETTY_HOME>\etc\jetty.xml to include the following xml fragment:

<Call name=”addConnector”>
<Arg>
    <New class=”org.mortbay.jetty.ajp.Ajp13SocketConnector”>
      <Set name=”port”>8009</Set>
     </New>
  </Arg>
</Call>

For Apache – set it up to forward your requests to Jetty (no different than what you would do for it to forward requests to Tomcat).

For details on how to setup Apache/Tomcat to talk to one another, see the relevant post here.

Posted in Uncategorized | Leave a Comment »

How can i monitor and improve performance of my Apache/Tomcat/MySQL application?

Posted by qnaguru on October 30, 2008

Monitor Apache

You can view status of your Apache Web Server by going to a URL of the kind:

This allows us to view status of Apache by going to a URL like:
If the above URL does not work, your apache is probably not configured to return status. For information on how to set it up for this, see Apache Mod Status.

To view the JK Status, go to a URL of the kind:

http://host/jk-status

Monitor-Tomcat

You can view a lot of useful statistics of Tomcat by looking at its Admin Console:

http://<IP:port>/ > click on status link.

Things to look for here are under the approriate headings JVM, jk-8009 (assuming apache is configured to connect via jk on port 8009), http-8080.

JVM

Free Memory – Not sure how this works.

Total Memory – Not sure how this works.

Maximum Memory – this is the maximum amount of memory available to tomcat server, it is the value of the -mx setting in your tomcat startup java command (in catalina.sh). If no mx setting is done, a default value is assumed (which is different for windows and linux)

 

jk-8009 or http-8080

Here the important things to look at are:

Max threads: 200 Current thread count: 28 Current thread busy: 8
Request count: 19 Error count: 0

Max threads – this is maximum number of threads that tomcat can have to process incoming requests (and its internal functions).

Current thread count – this is total number of threads in tomcat’s threadpool at present. Threads will get garbage collected from this pool. And the pool size can grow to ‘Max threads’ size when necessary.

Currrent thread busy – this is total number of threads that are processing variour requests at present.

Request count – total number of http requests received by tomcat since the time it was started.

Improve -Tomcat

You have a performance problem in tomcat when:

Current Thread Count is near about Max Threads. This is a clear indicator that tomcat is receiving more requests faster than it can handle. The default Max Thread setting is 200 which is good enough for most applications, but you can change it here in <TOMCAT_HOME>/conf/server.xml – look for your connector element, here you can change (or add if not present) maxThreads attribute. See http://tomcat.apache.org/tomcat-5.5-doc/config/http.html . But beware, you cannot increase this value indiscrimnately(will affect memory and performance both), the optimum value is based on your hardware, and primarily the number of CPUs you have (more the better for increasing maxThreads). Thumb Rule: Do not change defaults, unless you really have to.

You see Out of Memory Exceptions in your catalina log. See the <TOMCAT_HOME>\logs\catalina.out log file. This is a clear indicator that your application needs more memory than what is available. The min,max memory available can be changed here: <TOMCAT_HOME>\bin\catalina.sh or .bat.  You can add a entry like this somewhere  (A good place would be immediately before this line: # Set juli LogManager if it is present)

(on linux): JAVA_OPTS=”$JAVA_OPTS -ms256M -mx512M”

(on windows): set JAVA_OPTS=-ms256M -mx512M

Monitor MySQL

You can check lots of important mysql params by going to mysql prompt and typing as follows:

mysql> SHOW VARIABLES;

One of things of interest in the response would be: max_connections. This will tell you how many max concurrent connections are allowed.

You can also find out how many concurrent MySQL connections exist at any point in time by using following command:

mysql> show processlist;
The number of rows in the response is equal to number of connections to MySQL at that time.

Improve MySQL

The most important thing you need to find out is if you are running out of db connections. This you can find out by using the above mentioned commands i.e show processlist shows equal number of connections as max_connections. If this happens you need to increase the max_connections.. HOW?

Ofcourse check the usual things in your db connection pool, you pool size cannot be greater than max_connection value ofcourse.

How to see Log of DB Queries being executed?

How to find how much time is being spent in execution of each query?

How to find the amount of data(bytes) being returned by each query?

Posted in Apache, Deployment, Java, MySQL, Performance, Tomcat, Uncategorized | Tagged: , , , | Leave a Comment »

How can i prevent automated registrations using a (captcha) image?

Posted by qnaguru on October 11, 2008

In your user registration pages you may want to provide a generated image, which the user will be required to read and type its contents onto a text box. This will prevent automated bots etc. from filling up the form doing fake registrations.

What you need is:

A image tag in your user form, the src of it should point to a program (.jsp,struts-action etc.) which returns a randomly image (which has text), but before returning this program must store the image text in Session too, so that it when the form is submitted, you can retrieve what the user typed for the image text and compare it against what i stored in the session.

Not clear?

Ok your user form looks like this:

<form action=”/registerUser”>

Email: <input type=”text” name=”email”>

Name: <input type=”text” name=”thename”>

<img src=”/myapp/getMySecretImage.do”>

Type the image text here: <input type=”text” name=”thesecret”>

</form>

So, as you see above, the registration form has a image with an src that is pointing to a struts action. It could as well point to a JSP page or a CGI Script or whatever is capable of generating a image and returning.

Now, let us see what the image generation program looks like. Remember that it will not just return the image but also store the text that will display inside the image in user session.

So, if you are writing your struts action’s (/getMySecretImage) executre method will look like this (you can put the same thing in a servlet or JSP):

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Iterator;

import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class MyCaptchaStrutsAction extends Action
{

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
    
        String imageFormat = “jpg”;
      response.setContentType((new StringBuilder(“image/”)).append(imageFormat).toString());
         try
         {
             Color backgroundColor = Color.red;
             Color borderColor = Color.black;
             Color textColor = Color.white;
             Color circleColor = new Color(160, 160, 160);
             //Font textFont = new Font(“Arial”, 0, paramInt(request, “fontSize”, 24));
             Font textFont = new Font(“Arial”, 0, 24);
             int charsToPrint = 6;
             int width = 130; //paramInt(request, “width”, 130);
             int height = 40; //paramInt(request, “height”, 40);
             int circlesToDraw = 6;
             float horizMargin = 20F;
             float imageQuality = 0.95F;
             double rotationRange = 0.69999999999999996D;
             BufferedImage bufferedImage = new BufferedImage(width, height, 1);
             Graphics2D g = (Graphics2D)bufferedImage.getGraphics();
             g.setColor(backgroundColor);
             g.fillRect(0, 0, width, height);
             g.setColor(circleColor);
             for(int i = 0; i < circlesToDraw; i++)
             {
                 int circleRadius = (int)((Math.random() * (double)height) / 2D);
                 int circleX = (int)(Math.random() * (double)width – (double)circleRadius);
                 int circleY = (int)(Math.random() * (double)height – (double)circleRadius);
                 g.drawOval(circleX, circleY, circleRadius * 2, circleRadius * 2);
             }

             g.setColor(textColor);
             g.setFont(textFont);
             FontMetrics fontMetrics = g.getFontMetrics();
             int maxAdvance = fontMetrics.getMaxAdvance();
             int fontHeight = fontMetrics.getHeight();
             String elegibleChars = “ABCDEFGHJKLMPQRSTUVWXYabcdefhjkmnpqrstuvwxy23456789″;
             char chars[] = elegibleChars.toCharArray();
             float spaceForLetters = -horizMargin * 2.0F + (float)width;
             float spacePerChar = spaceForLetters / ((float)charsToPrint – 1.0F);
             g.getTransform();
             StringBuffer finalString = new StringBuffer();
             for(int i = 0; i < charsToPrint; i++)
             {
                 double randomValue = Math.random();
                 int randomIndex = (int)Math.round(randomValue * (double)(chars.length – 1));
                 char characterToShow = chars[randomIndex];
                 finalString.append(characterToShow);
                 //maxAdvance * 2;
                 //fontHeight * 2;
                 int charWidth = fontMetrics.charWidth(characterToShow);
                 int charDim = Math.max(maxAdvance, fontHeight);
                 int halfCharDim = charDim / 2;
                 BufferedImage charImage = new BufferedImage(charDim, charDim, 2);
                 Graphics2D charGraphics = charImage.createGraphics();
                 charGraphics.translate(halfCharDim, halfCharDim);
                 double angle = (Math.random() – 0.5D) * rotationRange;
                 charGraphics.transform(AffineTransform.getRotateInstance(angle));
                 charGraphics.translate(-halfCharDim, -halfCharDim);
                 charGraphics.setColor(textColor);
                 charGraphics.setFont(textFont);
                 int charX = (int)(0.5D * (double)charDim – 0.5D * (double)charWidth);
                 charGraphics.drawString((new StringBuilder()).append(characterToShow).toString(), charX, (charDim – fontMetrics.getAscent()) / 2 + fontMetrics.getAscent());
                 float x = (horizMargin + spacePerChar * (float)i) – (float)charDim / 2.0F;
                 int y = (height – charDim) / 2;
                 g.drawImage(charImage, (int)x, y, charDim, charDim, null, null);
                 charGraphics.dispose();
             }

             g.setColor(borderColor);
             g.drawRect(0, 0, width – 1, height – 1);
             Iterator iter = ImageIO.getImageWritersByFormatName(imageFormat);
             if(iter.hasNext())
             {
                 ImageWriter writer = (ImageWriter)iter.next();
                 ImageWriteParam iwp = writer.getDefaultWriteParam();
                 if(imageFormat.equalsIgnoreCase(“jpg”) || imageFormat.equalsIgnoreCase(“jpeg”))
                 {
                     iwp.setCompressionMode(2);
                     iwp.setCompressionQuality(imageQuality);
                 }
                 writer.setOutput(ImageIO.createImageOutputStream(response.getOutputStream()));
                 IIOImage imageIO = new IIOImage(bufferedImage, null, null);
                 writer.write(null, imageIO, iwp);
             } else
             {
                 throw new Exception(“No encoder found.”);
             }
             request.getSession().setAttribute(“CAPTCHA”, finalString.toString());
             g.dispose();
         }
         catch(Throwable e)
         {
          System.out.println(“Unable to build Captcha image: ” + e);
         }    
    
     return null;
    }

}

(NOTE: This image generation code in its JSP version is here. It has only been put in struts action form above.)

You will see that this line above:

request.getSession().setAttribute(“CAPTCHA”, finalString.toString());
stores the image text in session.

So, when the registration form is submitted, you can check against it.

 

 

 

 

Posted in Uncategorized | Tagged: , , | Leave a Comment »