Archive

Posts Tagged ‘tomcat’

How to log incomplete request in apache tomcat

May 11th, 2017 No comments

We had been looking into  options on how to identify requests which are not logged in access logs because they could not be completed for any reason. So for those of you who are unaware,  access log only logs the request which are complete. Let us say users requests a page and for some reason the request never gets completed (Server crash, out of memory), you will never see that in access logs.

But more often than not you will need that request to identify what caused the crash

There are 3 potential options

Option 1:

Use Apache as a reverse proxy in front of Tomcat  and enabled mod_log_forensic to log all request

How does it work ?

Apache comes with a module mod_log_forensic

http://httpd.apache.org/docs/current/mod/mod_log_forensic.html

In this each request is logged two times

The first line logs the forensic ID, the request line and all received headers, separated by pipe characters (|).  A sample line looks like the following (all on one line):

+yQtJf8CoAB4AAFNXBIEAAAAA|GET /manual/de/images/down.gif HTTP/1.1|Host:localhost%3a8080|User-Agent:Mozilla/5.0 (X11; U; Linux i686; en-US; rv%3a1.6) Gecko/20040216 Firefox/0.8|Accept:image/png, etc…

The plus character at the beginning indicates that this is the first log line of this request. The second line just contains a minus character and the ID again:

Then next line would be , which signifies that request has been completed.

-yQtJf8CoAB4AAFNXBIEAAAAA

 

Advantage : Least invasive with very little noise. No code change in application.

Disadvantage : Apache is not a part of our deployment. So this requires IT to modify the infrastructure.

 

Option 2:

Development team does code changes to log incomplete request.

Advantage : Can control what to log and when to log.

Disadvantage : Code changes. Testing. Patch required for earlier versions of applications.

 

 Option 3:

Enable Request Dumper Filter in tomcat.

Tomcat comes with a  request dumper filter which logs all incoming and outgoing request.

https://tomcat.apache.org/tomcat-7.0-doc/config/filter.html

 

Advantage : Changes required in application’s web.xml  and server’s logging.properties. Technically this is code change but we can do directly on the application server.

Disadvantage : Very verbose. Logs everything in request.

From tomcat docs

WARNING: Using this filter has side-effects. The output from this filter includes any parameters included with the request. The parameters will be decoded using the default platform encoding. Any subsequent calls to request.setCharacterEncoding() within the web application will have no effect.

 

This is the output from one request with dumper filter enabled on webservice.

 

10-Apr-2017 11:49:27.409 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1 START TIME        =10-Apr-2017 11:49:27

10-Apr-2017 11:49:27.409 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         requestURI=/cdpImport

10-Apr-2017 11:49:27.409 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1           authType=null

10-Apr-2017 11:49:27.410 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1  characterEncoding=utf-8

10-Apr-2017 11:49:27.410 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1      contentLength=45

10-Apr-2017 11:49:27.410 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1        contentType=text/plain; charset=utf-8

10-Apr-2017 11:49:27.410 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1        contextPath=

10-Apr-2017 11:49:27.410 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             cookie=dummyCookie=make-sure-there-is-always-a-cookie

10-Apr-2017 11:49:27.420 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         remoteAddr=127.0.0.1

10-Apr-2017 11:49:27.421 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         remoteHost=127.0.0.1

10-Apr-2017 11:49:27.421 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         remoteUser=null

10-Apr-2017 11:49:27.421 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1 requestedSessionId=null

10-Apr-2017 11:49:27.421 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             scheme=http

10-Apr-2017 11:49:27.421 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         serverName=localhost

10-Apr-2017 11:49:27.422 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         serverPort=8085

10-Apr-2017 11:49:27.422 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1        servletPath=

10-Apr-2017 11:49:27.422 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1           isSecure=false

10-Apr-2017 11:51:07.619 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1           authType=null

10-Apr-2017 11:51:07.619 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1        contentType=text/plain;charset=ISO-8859-1

10-Apr-2017 11:51:07.620 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             header=Content-Type=text/plain;charset=ISO-8859-1

10-Apr-2017 11:51:07.620 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             header=Content-Length=211

10-Apr-2017 11:51:07.620 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             header=Date=Mon, 10 Apr 2017 15:51:07 GMT

10-Apr-2017 11:51:07.620 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1         remoteUser=null

10-Apr-2017 11:51:07.620 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1             status=202

10-Apr-2017 11:51:07.621 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1 END TIME          =10-Apr-2017 11:51:07

10-Apr-2017 11:51:07.621 INFO [http-nio-8085-exec-1] org.apache.catalina.filters.RequestDumperFilter.doLog http-nio-8085-exec-1 ===============================================================

This is what you add in web.xml of your application

</pre>
<filter>
 <filter-name>requestdumper</filter-name>
 <filter-class>
 org.apache.catalina.filters.RequestDumperFilter
 </filter-class>
 </filter>
 <filter-mapping>
 <filter-name>requestdumper</filter-name>
 <url-pattern>*</url-pattern>
 </filter-mapping>

Categories: Programming / tutorials Tags:

What is default Connector in Tomcat 8

May 23rd, 2016 No comments

What is the default connector option in Tomcat 8 if you do not define explicitly ?

Given this configuration in server.xml

<Connector port="8080" protocol="HTTP/1.1" enableLookups="false" compression="on"

maxThreads="200" acceptCount="100" maxKeepAliveRequests="1" URIEncoding="UTF-8" />

what do you think will be default connector ?

Answer :  NIO(Non blocking connector)

This is the screen shot from Jconsole

default-protocol-tomcat8

From Tomcat documentation

Sets the protocol to handle incoming traffic. The default value is HTTP/1.1 which uses an auto-switching mechanism to
select either a non blocking Java NIO based connector or an APR/native based connector.
If thePATH (Windows) or LD_LIBRARY_PATH (on most unix systems) environment variables contain the Tomcat native library,
the APR/native connector will be used. If the native library cannot be found, the non blocking Java based connector will be used.
Note that the APR/native connector has different settings for HTTPS than the Java connectors.

So even though you have not explicitly defined a connector, tomcat 7, tomcat 8 and above will be using NIO Connector

Now I change the server.xml connector to


<Connector port="8080" protocol="org.apache.coyote.http11.Http11Nio2Protocol"
 maxThreads="5000" SSLEnabled="false" scheme="https" secure="true"
 socket.appReadBufSize="1024"
 socket.appWriteBufSize="1024"
 clientAuth="false" sslProtocol="TLS"
 enableLookups="false" />

tomcat-connector-protocol

The data is available through JConsole

Categories: Programming / tutorials Tags:

Cross Domain scripting settings on Tomcat

February 23rd, 2011 3 comments

In last post we had explained how to configure Apache to allow cross domain scripting. details can be found here

Cross domain Scripting problem with XmlHttp

If you do not plan to use Apache and for some reasons using tomcat or any other similar web container which supports filter, here is a ready made solution, Cors Filter

This gives you a servlet filter which is compatible with any Java Servlet 2.5+ web container.

Installation is very simple. Add the jar to your libraries

In you web.xml

add this line

<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>CORS</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

And you are all set

Configuring Tomcat with Apache

February 2nd, 2011 4 comments

In this post we will describe how to set up Apache web server with Tomcat.

We have used  Tomcat 6.0.29 and Apache 2.2.17 but the settings describe here will work for Tomcat 7 as well.

1. We will need to install mod_jk on apache

Mod_jk can be downloaded  from this location

http://www.fightrice.com/mirrors/apache//tomcat/tomcat-connectors/jk/binaries/win32/

2. Copy mod_jk-1.2.30-httpd-2.2.3.so to <Install Location>\Apache Software Foundation\Apache2.2\modules

3. Create a workers.properties in conf folder of apache

A sample workers.properties

workers.tomcat_home=/usr/local/jakarta-tomcat-4.1.18

# workers.java_home should point to your Java installation. Normally
# you should have a bin and lib directories beneath it.
#
workers.java_home=/usr/lib/java2

# You should configure your environment slash… ps=\ on NT and / on UNIX
# and maybe something different elsewhere.
#
ps=/

# The workers that your plugins should create and work with
#
worker.list=worker1

#—— DEFAULT ajp13 WORKER DEFINITION ——————————
#———————————————————————
# Defining a worker named ajp13 and of type ajp13
# Note that the name and the type do not have to match.
#
worker.worker1.port=8009
worker.worker1.host=localhost
worker.worker1.type=ajp13

#—— CLASSPATH DEFINITION —————————————–
#———————————————————————
# Additional class path components.
#
worker.inprocess.class_path=$(workers.tomcat_home)$(ps)lib$(ps)tomcat.jar

# The JVM that we are about to use
#
# Unix – Sun VM or blackdown
worker.inprocess.jvm_lib=$(workers.java_home)$(ps)jre$(ps)lib$(ps)i386$(ps)classic$(ps)libjvm.so

# Setting the place for the stdout and stderr of tomcat
#
worker.inprocess.stdout=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stdout
worker.inprocess.stderr=$(workers.tomcat_home)$(ps)logs$(ps)inprocess.stderr

4. Use Tomcat auto configure.

To be added at the end of your httpd.conf

Include $TOMCAT_HOME/conf/jk/mod_jk.conf-auto

5. Re start tomcat and re start Apche.

You will find two folders auto and jk created under TOMCAT_HOME/conf.

http://tomcat.apache.org/connectors-doc/webserver_howto/apache.html

You will also find this value inserted in conf.xml

<Listener className=”org.apache.jk.config.ApacheConfig” modJk=”<Your apache directory>/Apache2.2/modules/mod_jk-1.2.30-httpd-2.2.3.so” />

Common Issues: Read more…

Setting up tomcat for remote debugging

December 28th, 2010 No comments

In windows environment , open up your startup.bat and add to file somewhere in beginning

rem set remote debugger
set JPDA_ADDRESS=8001
set JPDA_TRANSPORT=dt_socket
echo Remote debugging started

Add this to end of your startup.sh file

call “%EXECUTABLE%” jpda start %CMD_LINE_ARGS%

Make sure you comment out

call “%EXECUTABLE%” start %CMD_LINE_ARGS%

When you are connecting with eclipse, set your debug port at 8001

Now when you start your server , you would see the message Remote debugging started


Categories: Programming / tutorials Tags:

Creating a data source for tomcat

November 22nd, 2010 1 comment

To create a datasource in tomcat , add this in your conf/context.xml

<Resource name=”jdbc/TestDB” auth=”Container” type=”javax.sql.DataSource”
maxActive=”100″ maxIdle=”30″ maxWait=”10000″
username=”javauser” password=”mypassword” driverClassName=”com.mysql.jdbc.Driver”
url=”jdbc:mysql://localhost:3306/javatest”/>
And if you are using Spring applicationContext.xml file, add this to applicationCOntext.xml file

<jee:jndi-lookup id=”dataSource” jndi-name=“jdbc/TestDB” />

Categories: Programming / tutorials Tags: ,

Troubleshooting application performance with VisualVM

November 11th, 2010 1 comment

Those of you who have not heard about VisualVm , we had talked about it in earlier post Performance tuning your java and J2ee applications.
This is one of the best free tools for monitoring the performance of your java/j2ee application. If you try to use VisualVM with myeclipse, you will have to upgrade from basic license of $30 ! But if you follow the rest of the post , I will explain how to do it for free.

Not many people have worked with it but it sure gives you a deeper insight of your application and helps in locating the problematic areas. The motivation for us was to identify the out of memory exceptions were facing in our application and pinpoint the cause.

In this post we will talk about Visual VM, setting it up with your eclipse and how to monitor your application. We will set up a web application and run visualVM to monitor the application

1. What is Visual VM

Java Management Extensions (JMX) had been part of java since JDK 5.0. JMX enables you to get all the information from the JVM.  VisualVM is a visual tool that integrates some of the commandline JDK tools and gives you powerful profiling capabilities.  VisualVM uses JMX and gives you detailed info about JVM memory, CPU usage, Garbage Collection . It can also profile your objects for CPU and memory usage suggested for local JVM’s only.

More about VisualVM

2.  Installing Visual VM

Visual VM comes bundled with your JDK install ,(post JDK 1.6 update 7). Alternatively you can go to https://visualvm.dev.java.net/download.html and download the file.

Once installed , go to <installed directory>/bin and click visualvm.exe.

It will fire up the visualVM for you. Read more…

How to enable verbose garbage collection in tomcat ?

October 7th, 2010 No comments

If you had been working with Tomcat in production performance or out of memory issues, take a look at our recnet posts on How to increase heap size on tomcat and Increasing permgen on your tomcat server.

Now comes another important part. How do you know your tomcat is loading / unloading classes as you have been thinking . How is GC happening and is it causing the bottleneck ?

Well simple answer is enable the verbose GC settings on tomcat.

How do you do that ?

On Windows :

set JAVA_OPTS=-XX:+PrintGCDetails -XX:-HeapDumpOnOutOfMemoryError -verbose:gc

On Linux :

You can add on catalina.sh .You may also want to check the file under $TOMCAT_HOME/bin/setenv.sh/.bat.

Add the above line in this file.

Categories: Programming / tutorials Tags: ,