Skip to main content

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.

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

Post a Comment

Popular posts from this blog

Write Your Own Load Balancer: A worked Example

I was out walking with a techie friend of mine I’d not seen for a while and he asked me if I’d written anything recently. I hadn’t, other than an article on data sharing a few months before and I realised I was missing it. Well, not the writing itself, but the end result. In the last few weeks, another friend of mine, John Cricket , has been setting weekly code challenges via linkedin and his new website, https://codingchallenges.fyi/ . They were all quite interesting, but one in particular on writing load balancers appealed, so I thought I’d kill two birds with one stone and write up a worked example. You’ll find my worked example below. The challenge itself is italics and voice is that of John Crickets. The Coding Challenge https://codingchallenges.fyi/challenges/challenge-load-balancer/ Write Your Own Load Balancer This challenge is to build your own application layer load balancer. A load balancer sits in front of a group of servers and routes client requests across all of the serv...

Catalina-Ant for Tomcat 7

I recently upgraded from Tomcat 6 to Tomcat 7 and all of my Ant deployment scripts stopped working. I eventually worked out why and made the necessary changes, but there doesn’t seem to be a complete description of how to use Catalina-Ant for Tomcat 7 on the web so I thought I'd write one. To start with, make sure Tomcat manager is configured for use by Catalina-Ant. Make sure that manager-script is included in the roles for one of the users in TOMCAT_HOME/conf/tomcat-users.xml . For example: <tomcat-users> <user name="admin" password="s3cr£t" roles="manager-gui, manager-script "/> </tomcat-users> Catalina-Ant for Tomcat 6 was encapsulated within a single JAR file. Catalina-Ant for Tomcat 7 requires four JAR files. One from TOMCAT_HOME/bin : tomcat-juli.jar and three from TOMCAT_HOME/lib: catalina-ant.jar tomcat-coyote.jar tomcat-util.jar There are at least three ways of making the JARs available to Ant: Copy the JARs into th...

RESTful Behaviour Guide

I’ve used a lot of existing Representational State Transfer (REST) APIs and have created several of my own. I see a lot of inconsistency, not just between REST APIs but often within a single REST API. I think most developers understand, at a high level, what a REST API is for and how it should work, but lack a detailed understanding. I think the first thing they forget to consider is that REST APIs allow you to identify and manipulate resources on the web. Here I want to look briefly at what a REST API is and offer some advice on how to structure one, how it should behave and what should be considered when building it. I know this isn’t emacs vs vi, but it can be quite contentious. So, as  Barbossa from Pirates of the Caribbean said, this “...is more what you’d call ‘guidelines’ than actual rules.” Resources & Identifiers In their book, Rest in Practice - Hypermedia and Systems Architecture (‎ISBN: 978-0596805821), Jim Webber, Savas Parastatidis and Ian Robinson describe resour...