Archive for the ‘Miscellaneous’ Category
Load Balancing Tomcat with Apache
Recently I was working on server scalability issue of one of my projects. So I had to went through some RnD on load balancing and finally was able to configure it. I used tomcat as servlet container and apache as load balancer. I used mod_proxy and mod_proxy_balancer module of apache for load balancing between two tomcat instances running on port 8080 and 3333.
Here is the details:
Server used:
Tomcat 6.0.16 as servlet container x 2
Apache 2.2.6 as load balancer x 1
Testing Tool:
Jakarta JMeter 2.6.1
Apache Configuration:
Here, I created a cluster named mycluster using 2 tomcat server and sticksession named as JSESSIONID. My test application myapp was running on both of the tomcat server.
I used loadfactor 50 on both of the tomcat server (meaning both of them will be sharing equal load). I
1. Enable mod_proxy: In httpd.conf enable/add
LoadModule proxy_module modules/mod_proxy.so
2. Enable mod_proxy_balancer: In httpd.conf
LoadModule proxy_balancer_module modules/mod_proxy_balancer.so
3. Configure the load balancer on the cluster and balancer manager:
In httpd.conf add these lines:
ProxyPass /myapp balancer://mycluster stickysession=JSESSIONID
<Proxy balancer://mycluster>
BalancerMember http://localhost:8080/myapp route=tomcat1 loadfactor=50
BalancerMember http://localhost:3333/myapp route=tomcat2 loadfactor=50
</Proxy>
<Location /balancer-manager>
SetHandler balancer-manager
</Location>
After restarting the server and hitting http://localhost/balancer-manager in a browser it showed me the configurations of the cluster.
Tomcat Configuration:
1. In server.xml file of tomcat running on 8080 port I configured the jvm route as tomcat1 as :
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
By editing the engine tag.
2. In server.xml file of tomcat running on 3333 port I configured the jvm route as tomcat1 the same way:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
The testing application:
I used a single servlet for the test application myapp.
Here is the code snippet of the servlet named myservlet:
ServletContext sc = this.getServletContext();
String servername = sc.getInitParameter("
servername
"
);
out.print("
This is from ::
"
+ servername);
In web.xml of the application I added a context parameter named servername which value was server1 while deploying into tomcat1(on port 8080) and server2 on tomcat2(running on 3333 port).
Testing:
With JMeter I created a script for hiitting the url http://localhost/myapp/myservlet using 400 concurrent thread in 20 loops to see the effect.
Sending array in GET method in Java Servlet
Suppose I want to send an array of names from any http client in get method to a servlet like,
name[0] = “sihan”
name[1] = “rasel”
name[2] = “siam”
name[3] = “shamim”
All I have to do build the query string like this :
http://server_url:server_port/servletname?name=sihan&name=rasel&name=siam&name=shamim
And in the servlet we can get the array like this:
String[] names = request.getParameterValues("name");
Then ‘names‘ array will contain all the four values passed in ‘name‘ parameter.
Capturing website thumbnail
Some months before I had to capture and show the thumbnail of website dynamically and I found an excellent tool named ‘IECapt’. It is a sourceforge project.
“IECapt is a simple command line utility that uses the Internet Explorer rendering engine to render a web page into a GIF/JPEG/BMP/PNG image file” (Quoted from sourceforge.net).
It is a written in C++, licensed under GPL and runs in 32 Bit Windows XP/NT/2000. It can be found here.
Sample command:
From windows command line type the following command:
IECapt.exe https://sihantech.wordpress.com thumb.png
This will create an image ‘thumb.png’ in local disk by capturing a screenshot of the given url.
Now we can execute this by PHP using exec() function or by java using Runtime.getRuntime().exec() method.