Friday, 22 May 2009

Tomcat Servlet with Spring Timer

recently had a requirement to write a service, in Java, that monitors a directory and when new files with the correctly formatted name appear, send them to another system. All fairly simple stuff. There are many different ways of writing Java services, but we use Tomcat quite heavily, so rather than investigate another way, I decided to write a Tomcat servlet to act as the service.

I started off by extending GenericServlet and overriding the init and destroy methods to write log messages to standard out. Then I wrote the appropriate web.xml to tell Tomcat about the servlet and wrapped it all up in a war file (basically a zip file with a Tomcat specific directory layout) and deployed it to my local Tomcat installation. I then checked the logs and found the log messages I'd put in the code. Not bad going for twenty minutes work and my first Tomcat servlet written from scratch.

We've been gradually learning about Spring recently and I remembered reading that Spring had timers that would be perfect for polling the directory for files. So I integrated Spring into my servlet, repackaged and redeployed it and then checked the logs to make sure the Spring application context had fired up correctly. It had.

Next I created a Ticker class by implementing the Java TimerTask interface and implementing the run method to write "Tick" to standard out. I then registered the class as a Spring bean and created a Spring ScheduledTimerTask, set the tick interval to one second and created an anonymous TimerFactoryBean. Making the TimerFactoryBean anonymous causes it to be instantiated when the Spring context is started, rather than waiting for an explicit instantiation from code somewhere. So, what should happen is that the ticker should start as soon as the application starts. Sure enough as soon as I repackaged and deployed, "Tick" was written to standard out every second.

It occurred to me that the class extending GenericServlet was redundant. So, not expecting it to work, I removed the class from the servlet and web.xml entirely and repackaged and redeployed. That's when I had my real "Whoah! That's really neat!" moment. To my amazement and joy the ticker started again and kept ticking every second. I already knew Spring and Tomcat worked well together, but having Tomcat start the Spring context without needing a servlet class is pure genius.

It may seem like such a small and simple thing, but creating my first Java service and Spring timer and having them work together in a very simple way was a real revelation for me.

3 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete
  2. Paul,

    Thanks for your article. I have something that I would like to do in the same way.
    But being still somewhat new to the Java world, I have a dumb question.

    Does Tomcat "automatically" know about Spring, or is that a separate setup altogether?

    Thanks,

    Mitch McConnell
    Tampa, FL, USA

    ReplyDelete
  3. Tomcat knows nothing of Spring. You need to configure a Spring context listener in the web.xml for the application you're developing. It's very straight forward. Drop me an email and I'll see if I can help you out.paul.grenyer@gmail.com.

    ReplyDelete