We had an amazingly annoying problem in a Wicket application. A specific user was continuously having problems with ajax controls on pages (search fields, auto-complete fields etc). The problems were caused by PageExpiredExceptions. We couldn’t understand why only this one user had these problems. This went on for ages, until today I found out that Wicket 1.4 sets a default limit of 5 page maps per session. This specific user typically worked with multiple browser tabs on the application and once he went over 5, some of the page maps got evicted and the ajax stuff started failing. The solution was to call “getSessionSettings().setMaxPageMaps(100)” to allow up to 100 page maps per session.

monit is a very cool system for keeping your linux servers working – highly recommended. With a few lines of configuration, you can have it check any aspect of your system and services and when problems occur, have it alert or take remedial actions (like restarting services, cleaning up log or temporary files etc). For example, we had the problem that we are running clustered web apps (using Terracotta) in VMware VMs. The cluster nodes were being suspended regularly for backups and this caused them to be evicted from the cluster. A simple solution was to use monit to monitor the apps (via the same health check port we were using for the availability check for the HAProxy load balancer)[…]

Logging is tricky. Even some major open-source projects don’t do it correctly, so if you use their libraries, you end up with log files you didn’t ask for cluttering your machine. Current best practices are to use a logging facade like commons-logging or slf4j to avoid these kind of problems by allowing libraries to conform to whatever logging strategy the application which uses the library is using. This means that if your app logs to myapp.log, then the library using slf4j will also log to myapp.log. Here’s how we use slf4j in our projects: Our libraries use slf4j-api – here’s the maven dependency you’ll need: [code lang=”xml”] org.slf4j slf4j-api 1.4.2 [/code] Our applications use slf4j-log4j12 – here’s the maven dependency[…]

We’re using Quartz to handle scheduled jobs in our java applications. Since we cluster our apps for failover using Terracotta, we need to address the issue of how to failover scheduled jobs (we only want each job to execute exactly once, but for fault tolerance it should be scheduled on multiple servers). Quartz handles this situation with clustered job stores and Terracotta provides a job store for Quartz. Its easy to use – just add the quartz-terracotta jar (you’ll also need a quartz version > 1.7 – I’m using 1.8.4 below). [sourcecode language=”xml”] org.quartz-scheduler quartz 1.8.4 org.terracotta.quartz quartz-terracotta 1.2.0 [/sourcecode] then set two systems properties. I’m doing it programmatically below because I use a commandline option to enable clustering in[…]

A while back we installed a new Dell server with a low-power Xeon 3GHz L3110 CPU to run some other essential network infrastructure. We chose the specific server configuration because it dissipates less than 30W while running 5 VMware VMs. It runs for hours on a UPS and doesn’t require cooling, so even if our server room air-conditioning were to die, this server should keep our network, firewalls, VPNs, DNS, DHCP and primary Terracotta server and a few other vital services up long enough for us to figure out what’s going on. The server is running CentOS 5.5 and VMWare 1.0.10 – normally a very stable combination. However, we found that Linux guests running on this server were not keeping[…]

A while back I blogged about using an annotated constraint behavior to limit input in Wicket TextFields based on domain model annotations (https://blog.armstrongconsulting.com/?p=22). So if we annotate our domain models as follows: [sourcecode language=”java”] @Length(max = 40) private String firstName; [/sourcecode] that gets automatically be translated into a maxlength=40 attribute on any text field bound to firstName (we use a form visitor on a custom form class to add the behavior to all text fields in the form). That’s pretty cool, but unfortunately, it only works for text inputs because textarea doesn’t support the maxlength attribute. This really bothered me because its really important to limit the length of textareas since that’s where people try to paste in huge blocks[…]

When I started with Wicket, I remember fighting with CheckBoxMultipleChoice for hours trying to get it to work. I just used it again today and was surprised at how easy it is to use once you’re familiar with how Wicket works. So for anyone struggling with it, here’s how to do it. Say we want to display a list of Projects associated with a User and we want to do it with the CheckBoxMultipleChoice. The projects which the user currently has access to should be checked initially. Checking or unchecking boxes will add or remove access to projects. You can accomplish this with the following code: [sourcecode language=”java”] add(new CheckBoxMultipleChoice(“projects”, new Model(userProjects), allProjects)); [/sourcecode] Assuming you have a span with[…]

Its often useful to use an enum as the model for a dropdown (or radio group) in Wicket, but not immediately obvious how to handle the display of the values. Lets say I have an enum with three colors: [sourcecode language=”java”] enum Color { RED, GREEN, BLUE }; [/sourcecode] I can hook this up to a dropdown with the following code: [sourcecode language=”html”] [/sourcecode] [sourcecode language=”java”] enum Color { RED, GREEN, BLUE }; private Color color = Color.GREEN; add(new DropDownChoice(“color”, new Model(color), Arrays.asList(Color.values()))); [/sourcecode] However, that would display “RED”, “GREEN”, “BLUE”. I probably don’t want the dropdown to use the default toString() form of my enum declarations, but rather I want it to load translatable texts from property files. To[…]

We just started using Mockito for unit testing of our Wicket applications. Its a great tool, allowing us to easily mock out our service layers on a test-by-test basis. In the example below, we’re testing a page called ItemPage. It takes an id of an item to load – internally the page uses itemService.find(id) to load the item from the database. Using Mockito, we mock out the itemService.find call to return a test item. [sourcecode language=”java”] public class TestItemPage extends WicketTestcase { private static final long serialVersionUID = 1L; @Test public void TestItemPage() { when(itemService.find(Mockito.anyInt())).thenAnswer(new Answer() { @Override public Item answer(InvocationOnMock invocation) throws Throwable { final int id = (Item) invocation.getArguments()[0]; Item item = new Item(id); item.setTitle(“test”); item.setDeadline(new Date()); return[…]

After fooling around with other methods, we finally accepted the advice I got on the Wicket IRC channel and used Terracotta to cluster our Wicket-based apps running under Jetty. It turned out to be straightforward to implement. The first thing to do was to add the Terracotta dependencies to our pom.xml. [sourcecode language=”xml”] org.terracotta.session terracotta-session 1.1.1 org.terracotta terracotta-toolkit-1.1-runtime 2.0.0 [/sourcecode] Then you just need to add a Terracotta filter to the jetty WebAppContext as follows: [sourcecode language=”java”] FilterHolder tcFilterHolder = new FilterHolder(TerracottaJetty61xSessionFilter.class); tcFilterHolder.setInitParameter(“tcConfigUrl”, “terracotta:9510,terracotta2:9510”); context.addFilter(tcFilterHolder, “/*”, Handler.ALL); [/sourcecode] That’s it. Terracotta will cluster the session (in the example we’re using two terracotta servers called “terracotta” and “terracotta2” – a main server and a standby). We’re using a HAProxy load-balancer with[…]