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

Thursday, January 12, 2017

Tomcat Connections Getting Disconnected After Period of Inactivity

In some network environments, the firewall or router might terminate some connections after a period of inactivity.

However, this is not enabled by default:
# netstat -anpo | grep :80
tcp6       0      0 192.168.1.7:80      123.123.123.123:38123    ESTABLISHED 21345/java           off (0.00/0/0)

To prevent this, KeepAlive must be enabled in Tomcat. This is done by editing the Connector configuration in the server.xml file and adding the "socket.soKeepAlive" attribute.
   <Connector port="8080" protocol="HTTP/1.1"
              connectionTimeout="20000"
              redirectPort="8443"
               socket.soKeepAlive="true"
        />

However, this alone may not be enough, because by default in some Linux distros, the default KeepAlive time is 2 hours, which means the first KeepAlive packet does not get sent until 2 hours later. For networks that disconnect idle connections within e.g. 15 minutes, this is too late. To reduce this to e.g. 5 minutes:
echo 300 > /proc/sys/net/ipv4/tcp_keepalive_time
Note: this change will not survive reboot. To make this change permanent the /etc/sysctl.conf needs to be edited.

After restarting Tomcat, the connection will look like this:
# netstat -anpo | grep :80
tcp6    0      0 192.168.1.7:80    123.123.123.123:38123   ESTABLISHED 21345/java      keepalive (82.52/0/0)

References:

Tuesday, April 14, 2015

Friday, November 14, 2014

Tomcat Enable Self-Signed SSL

Assuming Java tools are already on your PATH:

As root user:
keytool -genkey -alias tomcat -keyalg RSA

Then answer the questions accordingly

Reference: http://tomcat.apache.org/tomcat-6.0-doc/ssl-howto.html

Tuesday, August 19, 2014

Tomcat Suppress Stacktrace on Status 500 Error Page for REST Services

Whenever there is a HTTP Status 500 error, Tomcat by default displays an error page with the stacktrace on it. In production servers, this should be disabled. One common way to do this is to specify custom error pages in web.xml, like this:
<error-page>
<error-code>500</error-code>
<location>/error-500.html</location>
</error-page>

This works in most cases, but for a lot of Java REST backends, all URLs are mapped to the REST framework. For example, with Jersey, all URLs are mapped to the Servlet handling the REST requests.
<servlet-mapping>
<servlet-name>RestServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
In this situation the above solution will fail because RestServlet is unable to serve "error-500.html".

The solution is to use another Servlet to handle error paths. For static files, the "default" Servlet works. In order for Tomcat to use the default Servlet for error pages, this has to appear before the RestServlet in web.xml.
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/error*</url-pattern>
</servlet-mapping>

However, there is a problem with this solution. If the original REST request is a PUT, the default Servlet will reject it with a 403. The solution is to create a custom error handling Servlet that does not process the request but instead prints out the error page depending on the error code embedded in the request URI. This is a Servlet that overrides doGet, doPost, doPut and doDelete, and within each of them, outputs the appropriate error page using out.print().

Friday, June 1, 2012

Memory Settings for Tomcat

Edit catalina.sh

Add this line before the "Execute the Requested Command" comment:

JAVA_OPTS="$JAVA_OPTS -Xms2048m -Xmx2048m -XX:NewSize=256m -XX:MaxNewSize=256m -XX:PermSize=256m -XX:MaxPermSize=256m"

Getting Tomcat to Use Specific JDK

In certain situations, you'd want to use a different JDK for Tomcat than the one installed by default. E.g. in Amazon Linux where it's messy to uninstall OpenJDK due to dependencies from the Amazon tools.

To use a different SDK, first install it, then in Tomcat, add a line to setclasspath.sh

export JAVA_HOME="/opt/java/jdk"

This can be added right at the top of the file.