Handling Exceptions in Spring MVC with Logging

In your Spring servlet.xml, add a HandlerExceptionResolver.

	<bean id="exceptionResolver" class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
		<property name="exceptionMappings">
			<props>
				<prop key="java.lang.Exception">/error/default</prop>
			</props>
		</property>
		<property name="defaultErrorView" value="/error/default" />
		<property name="warnLogCategory" value="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" />
	</bean>

“/error/default” is the URL it will go to that will display an error message to the user. warnLogCategory is the logger category. Exceptions will be logged under the warning level and not the error level. The category value can be anything. Here we just set it to the full name of SimpleMappingExceptionResolver.

Spring HandlerExceptionResolver won’t handle errors in the view, so capture these using web.xml

	<error-page>
		<exception-type>java.lang.Exception</exception-type>
		<location>/error/default</location>
	</error-page>

Apache 2 Performance Tuning

Here’s a list of performance tips taken from the official Apache Performance Tuning documentation. This is meant to serve as a quick cheat sheet. For the details, the trade-offs, the why and when to use these optimizations, please read the complete documentation. All these are for tuning your Apache server for every last ounce of performance, and a few are not applicable to many real world Apache installations.

  • Configure MaxClients to prevent swapping. Use top to determine the size of the average Apache process, and divide it by the total available memory. Make sure to leave room for other processes.
  • Use HostnameLookups off.
  • Use IP addresses instead of Allow from domain or Deny from domain.
  • Set FollowSymLinks. Do not set SymLinksIfOwnerMatch.
  • Use AllowOverride None.
  • Do not use wildcards in DirectoryIndex.
  • Creating a type-map file provides better performance than using MultiViews.
  • Remove unused modules. Minimum modules are mod_mime, mod_dir, and mod_log_config.
  • Compile the appropriate MPM.
  • Use –enable-nonportable-atomics during compilation.
  • Set ExtendedStatus off.
  • Disable dynamically loaded modules during compilcation using -DDYNAMIC_MODULE_LIMIT=0.

It’s now unnecessary to configure the MinSpareServers, MaxSpareServers and StartServers settings.

Quick Byteman Tutorial

This is a quick tutorial on Byteman 2.0. You will need Eclipse and m2e to follow the steps below.

1. Download and unzip Byteman.

2. Create a new Maven Quickstart project in Eclipse.

3. Create the byteman script. This tutorial will use the filename quickbyteman.btm.

4. Set the content of the script.

RULE trace main entry
CLASS App
METHOD main
AT ENTRY
IF true
DO traceln("entering main")
ENDRULE 

RULE trace main exit
CLASS App
METHOD main
AT EXIT
IF true
DO traceln("exiting main")
ENDRULE

This script contains two rules, one injected on entry of App.main(), and another on exit. The code that runs calls the builtin function traceln(String) which prints to System.out().

5. Add the byteman javaagent to your Eclipse Run Configuration or in the Eclipse JRE. This tutorial will add it to the JRE.

 -javaagent:\byteman\lib\byteman.jar=script:quickbyteman.btm 

6. Run App.java and you should see the “entering main” and “exiting main” in the console.

For more information about Byteman, visit the Byteman documentation and the Byteman Programmer’s Guide.