Showing posts with label tomcat. Show all posts
Showing posts with label tomcat. Show all posts

Monday, 9 June 2014

Enabling JMX in Tomcat

Any Java program can be monitored using Jconsole. Jconsole is an user interface which comes by default with any JDK package, you just need to type jconsole on your terminal and it will show you the interface. Jconsole works along with JMX, which has to be enabled in the respective java program or JVM.

How to enable JMX in tomcat?

You to enable these variables while starting of the JVM. In tomcat you can simple enable them by using CATALINA_OPTS variable in setenv.sh file in bin folder of tomcat.

-Dcom.sun.management.jmxremote 
-Dcom.sun.management.jmxremote.port={port to access} 
-Dcom.sun.management.jmxremote.authenticate=false 
-Dcom.sun.management.jmxremote.ssl=false
-Djava.rmi.server.hostname={optional, allow what ip to access this Tomcat}
export CATALINA_OPTS="$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9999 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname="

To know more details about variables, you can check here,
http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html

After changing this, start the tomcat and then run jconsole from anywhere and connect using
"IP:port", if authentication is enable, enter the credentials also. Now you can see all the JVM stuff, threads, memory usages, CPU usages, GCs performed etc.




Friday, 31 May 2013

Load Balancing using Apache mod_jk

After installing apache, install mod_jk module.

1. $ apt-get install libapache2-mod-jk

2. create a file jk.conf(if not present) in mods-available directory and write these lines
JkWorkersFile   /etc/apache2/workers.properties
JkLogFile       /var/log/apache2/mod_jk.log
JkShmFile       /var/log/apache2/mod_jk.shm
JkLogLevel      error

After creating file, may be you need to do symbol linking to mods-enabled directory. Or just disabling and enabling the mod_jk will do.

$ a2dismod jk
$ a2enmod jk

3. create a file /etc/apache2/workers.properties
and write lines for load balancing, creating a load balancer and workers and assembling the workers in load balancer. In below configuration server1 will take 60% load and server2 will take 40%.
#
worker.list=loadbalancer

worker.server1.port=8009 (-- The product server port where you want to forward the request)
worker.server1.host=server1 IP Address (-- The proxy server IP where you want to forward the request)
worker.server1.type=ajp13 (-- Protocol setting, in case of mod_jk module, it will be always ajp13)
worker.server1.lbfactor=60 (-- This parameter to set, how much load this server1 will be given)

worker.server2.port=8009
worker.server2.host=server2 IP Address
worker.server2.type=ajp13
worker.server2.lbfactor=40

worker.loadbalancer.type=lb
worker.loadbalancer.sticky_session=true
worker.loadbalancer.balance_workers=server1,server2

4. In virtual host configuration of Apache
In below example, all the request starting with /web/static/css(js)(images), will be JkUnMounted so , they will be served from apache document root directory(which is /var/www ), and rest all requests will be JkMounted, so they will be forwarded to load balancer and load balancer will forward the reuqest either to server1 or server2. This is done using AJP protocol, so make sure that you have configured AJP protocol in your application server like tomcat. TO configure the tomcat, you can check this url Configuration Apache and Tomcat to user Mod_jk connector for proxy passing


  ServerAlias www.example.com
  DocumentRoot /var/www
  ServerName example.com

  DocumentRoot /var/www/

  JkMount /* loadbalancer
  JkUnMount /web/static/css/* loadbalancer
  JkUnMount /web/static/js/* loadbalancer
  JkUnMount /web/static/images/* loadbalancer

  ErrorLog /var/log/apache/example-com-error_log
  CustomLog /var/log/apache/example-com-access_log combined



Wednesday, 16 January 2013

After setting javaagent in classpath tomcat is not starting



Q. I am trying to use newrelic as javaagent in tomcat on ubuntu machine. When I set the javaagent in classpath, tomcat fails to start. I tried with tomcat5 and tomcat7, its not starting. And I am not getting any log also to check anything.
I tried doing the same with tracelytics javaagent, still its the same, tomcat is not starting. And clue, no log, someone please help.
A. It was happening due to setting of Xss configuration at the time server start in CATALINA_OPTS. I had set it to 128K, somehow it was failing to start. But when I removed that setting, it started, then I changed to 256K then also it started. I am not sure about the inside story, but at least it is working now :)

Wednesday, 4 May 2011

Make your development system up on Mac

This post is about persisting the experience while installing Mac 10.6.3 (SnowLeopard) on my personal computer.

First of all you should decide if you want to install by using binary then I have no idea. I can tell you to install so many things easily using MacPort software manager.

So first of all follow this link to download and install MacRport http://www.macports.org/install.php

Basic command usages for MacPort
---------------------------
port list variant:no_ssl
port uninstall name:sql
port echo depof:mysql5
port echo apache*
port install mysql5
--------------------------

Now you need Apache + PHP + Mysql
Step 1: sudo port selfupdate
Step 2: sudo port install gawk
Step 3: sudo port install nawk (For me this step was failed, but I gave a damn !!!)
Step 4: sudo port install php5 +apache2 +mysql5-server

Now use the command below to setup the mysql db.
$ sudo /opt/local/lib/mysql5/bin/mysql_install_db --user=mysql

You can change mysql root password using this command
$ /opt/local/lib/mysql5/bin/mysqladmin -u root password 'new-password'

Next, you can configure apache and mysql to start automatically by enter the command below:-
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
$ sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist

Now you need to configure apache to load php file, open /opt/local/apache2/conf/httpd.conf and add this 2 line:-
LoadModule php5_module modules/libphp5.so
AddType application/x-httpd-php .php

Once saved, you may restart your apache

To start your apache2 and mysql5 manually, type the command below:-
$ sudo /opt/local/etc/LaunchDaemons/org.macports.apache2/apache2.wrapper start
$ sudo /opt/local/etc/LaunchDaemons/org.macports.mysql5/mysql5.wrapper start


Now you'll want Java
Java is usually installed with Mac OS and XCode (and yes you need to install XCode at the very very first thing of above all, you can locate XCode in any of your given DVD with Mac, or you can download also). You can usually find java and javac commands are usually working. The working java home would be usually at /Library/Java/Home

Eclipse : Download it from here http://eclipse.org/downloads/

Tomcat :
Step 1: Download it from here http://tomcat.apache.org/download-60.cgi
Step 2: After downloading tomcat, extract the file and go to bin
Step 3: Create a file setenv.sh and write this line JAVA_HOME=/Lbrary/Java/Home (or whatever java home you have)
Step 4: Save the file, and run "chmod 777 setenv.sh" and you are ready with tomcat
Step 5: ./startup.sh will run the tomcat, you can see the log at ~tomcat_home/logs/catalina.out

Tuesday, 5 October 2010

Configuring Apache/Tomcat for serving Maximum number of requests

In Apache if you get to receive more number of request then it's time to configure your web server.

Apache has got MPM (Multi-Processing-Module) which has got many implementations, but mostly people are using 2 models, either prefork model or worker model. Prefork model is based on creating process as per request comes and the all the existing processes (as a pool) are like shared socket of master socket, So the delegation of request to any respective process is been handled by kernel. Worker model has got threads to handle the request. Prefork is stable in terms of No multi-threading so no clash or ambiguity of any shared library of Apache, though any unknown library can behave erroneous in case of worker model. But at the same time prefork model consumes more memory than worker model.

Using PHP, people suggest to use worker model, in case you are using AJP and then Tomcat, people suggest to use Prefork.

Configuring Prefork MPM for handling more number of requests

< IfModule mpm_prefork_module >
StartServers 10
MinSpareServers 10
MaxSpareServers 20
MaxClients 512
ServerLimit 512
MaxRequestsPerChild 0
< /IfModule>

Max value of MaxClient can go to 256 untill you set ServerLimit also, but once you set ServetLimitm you can increase MaxClients, in general people have reported that Apache can handle max of 4000 requests after that it dies.

You should reduce KeepAliveRequests in apache, because this cause more memory usages by Apache and set the KeepAliveTimeOut for sure.

MaxKeepAliveRequests 50
KeepAliveTimeout 15


Now...
If you are using AJP protocol to connect tomcat, you should configure AJP connector, if you are using HTTP you should configure HTTP connector. In case of mod_proxy, Apache will redirect the request to tomcat in HTTP form, in case of mod_jk Apache will redirect the request to tomcat usin AJP.

< connector port="8009" maxthreads="512" minsparethreads="25" maxsparethreads="75" connectiontimeout="10000" enablelookups="false" redirectport="8443" protocol="AJP/1.3">


< connector port="8181" maxhttpheadersize="8192" maxthreads="50" minsparethreads="10" maxsparethreads="20" enablelookups="false" redirectport="8443" acceptcount="100" connectiontimeout="10000" disableuploadtimeout="true">

And you should make sure that you are able to serve the request from tomcat as fast as possible, I have seen as google give good value, if you can serve request in less than 80ms.

Ref:
1. http://httpd.apache.org/docs/2.0/mod/mpm_common.html#serverlimit
2. http://tomcat.apache.org/tomcat-5.5-doc/config/ajp.html
3. http://www.howtoforge.com/configuring_apache_for_maximum_performance
4. http://af-design.com/blog/2009/02/17/increasing-apache-serverlimit-on-ubuntu/