1

I have a Java application hosted in a Tomcat servlet container which queries a database and creates an in-memory model (very big) with the results. The code to create the in-memory graph takes around 3 minutes to run so I'm keen to be able to do this as the application is started and before the users can access the application.

Is there a typical patterned approach for doing this?

David
  • 19,577
  • 28
  • 108
  • 128
raven-king
  • 1,550
  • 2
  • 18
  • 40

2 Answers2

4

A recommended way to do that is to implement a javax.servlet.ServletContextListener and to run your code inside contextInitialized(ServletContextEvent).

Then add this to your web.xml

<listener>
 <listener-class>com.company.LoadEverythingINeedListener</listener-class>
</listener>

This will ensure that your users won't access your application before you're ready.

Guillaume Darmont
  • 5,002
  • 1
  • 23
  • 35
1

you could use a <load-on-startup>1</load-on-startup> parameter from a servlet, or maybe a context listener to do it.

fGo
  • 1,146
  • 5
  • 11
  • take a look at this http://stackoverflow.com/questions/809775/what-does-the-servlet-load-on-startup-value-of-0-zero-signify and http://www.devmanuals.com/tutorials/java/servlet/ContextListenerExample.html – fGo Jun 03 '13 at 15:14