JavaTechie

Its all about Technology

Format Floating Point Number July 26, 2008

Filed under: Java — javatechie @ 5:48 am
public DecimalFormat(String pattern)

Creates a DecimalFormat using the given pattern and the symbols for the default locale. This is a convenient way to obtain a DecimalFormat when internationalization is not the main concern.

To obtain standard formats for a given locale, use the factory methods on NumberFormat such as getNumberInstance. These factories will return the most appropriate sub-class of NumberFormat for a given locale.

Eg.

String fmt = “0.00#”;

DecimalFormat df = new DecimalFormat(fmt);

double q = 10.0/4.0;
String str = df.format(q);
System.out.println(“q = “+str);

 

How can you set the session timeout in tomcat higher? July 4, 2008

Filed under: Java — javatechie @ 12:02 pm
Tags: ,

You can specify the timeout of a session in the deployment descriptor of your web application (web.xml):

<web-app>
  <session-config>
    <session-timeout>60</session-timeout>
  </session-config>

  ...
</web-app>

The number within the session-timout element must be expressed in minutes.

 

Integrating Apache 1.3.x and Tomcat 4.x with mod_jk on Linux July 2, 2008

Filed under: Apache, Java — javatechie @ 11:18 am
Tags: , ,

This FlashGuideTM covers integrating Apache 1.3.x and Tomcat 4.x on Unix via mod_jk. These instructions have been tested on SuSE 9.0, Red Hat 6.2 and Fedora Core 2. There are two ways to integrate Apache and Tomcat: mod_jk and mod_jk2. Mod_jk is the older but more stable version, which supports load balancing and non-standard web application locations. Mod_jk2 is newer, has bugs, and, as of 11/15/04, is no longer being actively developed.

These instructions are for the current version of Tomcat 4.1.31, but have worked the same for all previous versions of Tomcat.

If you are using an older version of Tomcat, note that there are known bugs when using mod_jk and Tomcat 4.0.1 or 4.0.2, so you must use 4.0.3 or greater.

1. Building Apache 1.3.x on Linux

Unless you can find a binary distribution of Apache with DSO support enabled, you will have to follow these instructions to build it yourself.

1. Check your prerequisites:

1. You will need GCC installed
2. You will need /usr/ccs/bin and the gcc executables in your $PATH

2. Download the latest Apache source from http://httpd.apache.org/download.cgi – currently, the latest is 1.3.33.
3. Unpack the distribution into a development directory (I used /usr/local)

The distribution directory will be something like apache_1.3.33
4. Cd into the distribution directory (e.g. /usr/local/apache_1.3.33)
5. Configure the makefile:

./configure –with-layout=Apache –prefix=/usr/local/apache –enable-rule=SHARED_CORE –enable-module=so

Read More

 

Apache performance tips July 2, 2008

Filed under: Apache — javatechie @ 11:11 am
Tags: ,

Apache 2.0 was running on several Linux boxes behind a load balancer. If you ran top on each box, the CPU was mostly idle, there was plenty of memory available, and yet Apache seemed sluggish. Here are a couple of things I did to speed things up.

1. Disable RedirectMatch directives temporarily

All the Apache servers had directives such as:

RedirectMatch /abc/xyz/data http://admin.mysite.com/abc/xyz/data

This was done so administrators who visited a special URL would be redirected to a special-purpose admin server. Since the servers were pretty much serving static pages, and they were under considerable load due to a special event, I disabled the RedirectMatch directives temporarily, for the duration of the event. Result? Apache was a lot faster.

2. Increase MaxClients and ServerLimit

This is a well-known Apache performance optimization tip. Its effect is to increase the number of httpd processes available to service the HTTP requests.

The 2 entries I had in httpd.conf in the IfModule prefork.c section were:

MaxClients 1000
ServerLimit 1000

Now I have a procedure for tuning the number of httpd processes on a given box:

1. Start small, with the default MaxClients (150).
2. If Apache seems sluggish, start increasing both MaxClients and ServerLimit; restart httpd every time you do this.
3. Monitor the number of httpd processes; you can use something like:

ps -def | grep httpd | grep -v grep | wc -l

If the number of httpd processes becomes equal to the MaxClients limit you specified in httpd.conf, check your CPU and memory (via top or vmstat). If the system is not yet overloaded, go to step 2. If the system is overloaded, it’s time to put another server in the server farm behind the load balancer.

WARNING: MaxClients of 1000 exceeds ServerLimit value of 256 servers, lowering MaxClients to 256. To increase, please see the ServerLimit directive.

To avoid above warning, You need to put the Serverlimit option before the maxclient in the apache config file.

Example config:

IfModule prefork.c

StartServers 20
MinSpareServers 5
MaxSpareServers 20

ServerLimit 1024 <====—— moved this line here rather than after the
Maxclients (otherwise Apache will gives error, “WARNING: MaxClients of 1000
exceeds ServerLimit value of 256 servers, lowering MaxClients to 256. To
increase, please see the ServerLimit directive”)

MaxClients 512 <====——– changed from 256 to 512

MaxRequestsPerChild 10000

IfModule

 

Apache Performance Tuning July 2, 2008

Filed under: Apache — javatechie @ 11:03 am
Tags: ,

Apache 2.x is designed to balance flexibility, performance and portability. Apache is a good all-purpose webserver. Since Apache is designed to fit most scenarios it hasn’t been optimized to set any kind of speed records, but Apache 2.x is capable of high performance.

There have been many improvements made in Apache 2.x and many are enabled by default. But, there are some changes you can make at compile-time and run-time that can positively affect performance.

Ram is the single biggest hardware issue that determines webserver performance, so the more the better. Once a server runs out of memory it starts using the swap space on the hard drive. You never ever want have to the web server use swap. Using swap slows down of each request to such slow speeds which causes users to hit “reload”, which increases the work swap has to do, slowing it further.
So I always recommend putting in as much ram as your server can hold.

The rest of the hardware is up to you: get a fast CPU, a fast network card, and fast hard drives, you’ll have to figure out what is fast enough for you based on needs, availability and price.

You can counter, and should, control the MaxClients setting so that your server does not spawn so many children it starts swapping. This procedure for doing this is simple: determine the size of your average Apache process, by looking at your process list via a tool such as top, and divide this into your total available memory, leaving some room for other processes.

Read more