Today I am going to to describe a simple and quick trick in Apache Wicket which allow our web application to send email everytime when something goes wrong.

Adding some method when exception appears in Wicket-based application is very easy task. In class extending WebApplication we should create our own implementation of method creating custom RequestCycle object where we override onRuntimeException method:

This method is executed by object responsible for request cycle processing named (what a surprise! 🙂 ) RequestCycleProcessor when any unhandled exception appears and allows us to return Page class where we want to redirect user after an error. But in this scenario we are going to use this method for a completely different purpose: sending e-mail message with detailed information about exception.

In this example to send emails I will use Spring framework module which can be configured in the following way:

a) applicationContext.xml fragment:

Second bean is a simple messages factory which builds emails with content. Its constructor need some variables from properties file.

b) WebApplication class fragment:

Because we passed Page object to the method prepare WicketExceptionMessage, we have access to more detailed data about application state when error appeared. We could read application name (useful when we use this mechanism in many projects), page parameters and through Session object also info about logged user. It’s good to know to who every error happened in case we would like to contact him and ask some questions about what he did, etc.