tags:

views:

439

answers:

3

I'm attempting to create a Spring application (NOT web application) to perform some simple tasks. Eventually they will hook up with some other Spring apps around the network, but for now I'm keeping it simple. I have a CheckForNewItems class (extending Timer) which is configured to run every 10 seconds.

I can confirm it runs by calling it programmatically:

public class Tester {

  public static ApplicationContext context;

  private void loadContext() {
    String filename = "beans.xml";
    context = new FileSystemXmlApplicationContext(filename);
  }

  public static void main(String[] args) {
    Tester test = new Tester();
    test.loadContext();
    CheckNewItemsTask task = (CheckNewItemsTask)context.getBean("checkNewItemsTask");
  }
}

Running this works as expected, task.run() gets called every 10 seconds. Now I need to work out how to deploy this to either a JBoss or Tomcat server, in such a way that it automatically starts running the task.

Most of the tutorials I've found only describe how to get Spring MVC and servlets running, not a standalone application. Does anyone know better?

Cheers, Rob.

+1  A: 

You need a servlet that is set to autostart on deployment. The servlet can then call into your "Tester" class to trigger your "standalone" initialization process.

If you don't have a servlet (or potentially some other server related process) reference your code, then your initialization process will never be run.

jsight
But surely, a servlet is a web concept?I don't necessarily need Tester to run, I just need the task to be loaded and ran.
Robert Wilson
@Robert - "But surely, a servlet is a web concept?" - Who cares? You seem to be missing the point here... there are plenty of (app-server specific) ways to initialize Spring without having anything web-related at all. But a servlet, that does NOTHING other than calling into Spring to get it initialized and running (and therefore get your task scheduler running) is the best way to generically auto-initialize tasks like this. Servlets are generally used for web-related things, but that isn't the only reason to have one. I've built BatchInitServlet classes before for exactly this reason.
jsight
+3  A: 

You don't need JBoss or Tomcat to do that. If the app is headless and you have no intention of adding a UI, consider jsvc for unix or procrun on windows. If you need the ability to monitor and control an app and do not need a proper UI for doing that, you might want to look at JMX. This will work on a daemon without the rest of the jee stack.

sal