Sunday, January 1, 2012

Tomcat: Application deployment/Logging/memory settings


Deployment application using tomcat manager:

1. Using the Tomcat Web Application Manager
Open a browser and connect to http://localhost:8080/manager/html. Provide the login/password of Tomcat's admin user. If you don't have one, check Tomcat's documentation and make sure you have the manager role and an admin user defined in CATALINA_HOME/conf/tomcat-users.xml as follows:

<role rolename="manager"/> 
<user username="admin" password="password" roles="manager"/> 

Please notice that the default content of CATALINA_HOME/conf/tomcat-users.xml is slightly different between Tomcat 6.x and Tomcat 5.5.x. Tomcat 5.5.x has the manager role predefined but has no user impersonating it. Tomcat 6.x has no role or user definitions at all. Regardless of the version you use, make sure the manager role and the admin user are defined as described above. 

Scroll down the page to the section called "Deploy directory or WAR file located on server". Enter the context name preceded by "/" (e.g., /myapp) in the "Context Path" field. 

Leave the "XML Configuration file URL" filed blank. 

Locate Portofino's WAR file and enter its absolute name (e.g., /absolute/path/portofino-war-3.1.war) in the "WAR or Directory URL" field. 

The section should look like this:

 


Click the "Deploy" button. The page will reload. At the top of the page, check that the message is "OK - Deployed application at context path /myapp". In the "Applications" section check that there is a line with the new Portofino application. The following screenshot shows the situation on Tomcat 5.5.x (On Tomcat 6.x it is very similar):

 

The application is now ready and available at the specified URL. 



2. Deploying to a remote server
The technique described in the previous section works if can copy Portofino's WAR file to the server's file system. If you cannot do that, you can still deploy it using a "remote deployment". 

First locate the WAR file on your local machine and rename it to the context name with "war" extension, e.g., myapp.war. 

Access the Tomcat Web Application Manager and scroll down to the section called "WAR file to deploy". 

Click on the "Browse..." button and select the myapp.war file on your local machine.

 

Click "Deploy". The browser will upload the war file to the server and Tomcat will deploy it.

3. Tomcat's auto-deploy

Many Tomcat installations do not offer the Tomcat Web Application Manager for security reason. Consequently, you have to deploy Portofino in some other way. 

Tomcat has a useful auto-deploy feature. You can check if it is enabled by looking at the CATALINA_HOME/conf/server.xml file. The "Host" section of the xml should look like this:

<Host name="localhost" appBase="webapps" 
    unpackWARs="true" autoDeploy="true" 
    xmlValidation="false" xmlNamespaceAware="false"> 

Please notice the values of autoDeploy and unpackWARs. If they are both true, you can procede as follows. 

Locate Portofino's WAR file and rename it to the context name with "war" extension, e.g., myapp.war. Then copy this file to CATALINA_HOME/webapps/. 

Tomcat will automatically detect the new WAR file, open it and deploy the application. The operation requires just a few seconds. 

4. Manually deploying and configuring


i. If Tomcat's auto-deploy is not enabled, there is still one way you can deploy Portofino. Go to CATALINA_HOME/webapps and create a directory with the context's name. Then unzip the WAR file into it.
$ cd CATALINA_HOME/webapps 
$ mkdir myapp 
$ cd myapp 
$ unzip /path/to/portofino-war-3.0.war 

Next go to CATALINA_HOME/conf/Catalina/localhost and create a file with the context's name and "xml" extension e.g., myapp.xml. Populate the file with the following content:



<Context path="/myapp" docBase=" /webapps/myapp " reloadable="true" />


Setting of Log4j in tomcat for logging:


Create a file called log4j.properties with the following content and save it into common/classes.
            log4j.rootLogger=DEBUG, R 
            log4j.appender.R=org.apache.log4j.RollingFileAppender 
            log4j.appender.R.File=${catalina.home}/logs/tomcat.log 
            log4j.appender.R.MaxFileSize=10MB 
            log4j.appender.R.MaxBackupIndex=10 
            log4j.appender.R.layout=org.apache.log4j.PatternLayout 
            log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
          


  1. Download Log4J (v1.2 or later) and place the log4j jar in $CATALINA_HOME/common/lib.
  2. Download Commons Logging and place the commons-logging-x.y.z.jar (not commons-logging-api-x.y.z.jar) in $CATALINA_HOME/common/lib with the log4j jar.
  3. Start Tomcat
This log4j configuration sets up a file called tomcat.log in your Tomcat logs folder with a maximum file size of 10MB and up to 10 backups. DEBUG level is specified which will result in the most verbose output from Tomcat.
You can (and should) be more picky about which packages to include in the logging. Tomcat 5.5 uses defines loggers by Engine and Host names. For example, for a default Catalina localhost log, add this to the end of the log4j.properties above. Note that there are known issues with using this naming convention (with square brackets) in log4j XML based configuration files, so we recommend you use a properties file as described until a future version of log4j allows this convention.

Memory setting for tomcat: Linux
put down the below option under the catalina.sh or setenv.sh file under the
apache-tomcat-5.5.34\apache-tomcat-5.5.34\bin\catalina.sh directory

export JAVA_OPTS="-Xms256m -Xmx512m -XX:MaxPermSize=128m"
Memory setting for tomcat: Windows



It’s quite common to run In memory problems when running some big Java EE application on a Tomcat server.
Some of the most commmon errors are like the following ones.
1) This is about a full Heap space:
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.OutOfMemoryError: Java heap space
This other is about the PermGen space that’s a memory area, where compiled classes (and JSPs) are kept,
and this error might happen often if the running web application have many .java and .jsp.
2) MemoryError: PermGen space
java.lang.OutOfMemoryError: PermGen space
Solution :
To increase the memory available to Tomcat, about heap and permgen the correct options are the following ones.
This sets the max heap available to Tomcat at 1Gb of memory:
–JvmMx 1024
This sets the max permgen available to Tomcat at 256Mb of memory:
-XX:MaxPermSize=256m
To change the Tomcat memory settings (when Tomcat is installed on Windows as system service), it’s required to use the
command-line tool tomcat6.
The next command changes the memory settings for the Tomcat service named Tomcat6
tomcat6 //US//Tomcat6 –JvmMx 1024 ++JvmOptions=”-XX:MaxPermSize=256m”
The label //US//Tomcat6 has the meaning of Updating Server parameters for the service named Tomcat6.
Obviously this command should be executed from the directory C:\Program Files\Apache Software Foundation\Tomcat 6.0\bin
or from wherever is the bin directory of your Tomcat installation. Or to make things simple, that directoy should be
added to your PATH environment variable.
It’s even possible to update memory settings from a GUI frontend, or to view what happened after running the command
line tool. Running the following command:
tomcat6w //ES//Tomcat6
a window will open showing all the parameters about the windows service Tomcat6.
It’s possible to see in this image that, after running the previous command, for setting higher memory limits, in the sections
Maximum memory pool and at the end of the Java Options the new memory limits are set.Tomcat Memory Settings on Windows