I'm trying to create a simple RESTful application that helps people create and retrieve certain objects. The objective of the service is to serve up these objects as quickly as possible, which is why I'm considering cheating a little by "pre-creating" the objects during the server startup time in an asynchronous fashion. I can get away with this model because I know that there are only 500 different flavors of objects that people can ask for so i'd rather have them readily sitting in my persistent store (redis) warmed up such that by the time a user is asking for it, it's ready to serve.
My question is around how to enable this "background/async" processing right around the server startup (for a server like jBoss/tomcat in a RESTful service written in java). Putting it into a static block in the class serving results doesn't work, so I was wondering if there's a config in the catalina files that can link to loading a class which in turn could do the needful.
I think you may be looking for load-on-startup
in your web.xml file, and then if you use the Init
method in your HttpServlet
class you may get the behavior you want.
Load-on-startup is a directive to tell the container to start that class immediately upon startup.
<servlet>
<servlet-name>ServletOne</servlet-name>
<servlet-class>com.yourcomp.MainServlet</servlet-class>
<description>This servlet is an example servlet</description>
<load-on-startup>1</load-on-startup>
</servlet>