4

I have a Tomcat server deployed that receives observations transmitted from sensors in JSON format. I also have a sensor describing ontology that I want to make use of.

However, I'd like to load the ontology before any sensor observations are received by the server. How can I instantiate an object as soon as Tomcat is loaded?

Bailz
  • 605
  • 2
  • 8
  • 17
  • Dupes: http://stackoverflow.com/questions/3289737/load-on-startup-tomcat, http://stackoverflow.com/questions/2057563/how-do-i-run-a-method-before-republishing-to-jboss, http://stackoverflow.com/questions/3468150/using-init-servlet, http://stackoverflow.com/questions/2364390/i-want-to-use-a-method-which-is-called-only-once-after-deploying-to-server, http://stackoverflow.com/questions/4175726/how-do-i-load-a-java-class-not-a-servlet-when-the-tomcat-server-starts, etc..etc.. – BalusC Nov 19 '10 at 18:39

3 Answers3

7

To execute actions when your application starts or stops you should use a ServletContextListener: http://download.oracle.com/javaee/6/api/javax/servlet/ServletContextListener.html

In web.xml:

<web-app>
    ...
    <listener>
        <listener-class>com.example.Listener</listener-class>
    </listener>
    ...
</web-app>

In contrast to Peter Knego's proposal this solution is portable to any servlet container and not confined to Tomcat.

Philipp Jardas
  • 3,222
  • 3
  • 29
  • 42
  • +1: That's probably cleaner than my Servlet.init() solution, depending on whether the ontology conceptually *belongs* in the servlet class or is merely piggybacking on its lifecycle. – Andrzej Doyle Nov 19 '10 at 17:34
1

I presume that strictly speaking, what you want to do is instantiate an object as soon as your servlet is loaded by Tomcat. (It wouldn't really make any sense to modify Tomcat itself for an application-specific functionality).

In this case, your Servlet class can override/implement the init(ServletConfig config) method. This is called by the servlet container (Tomcat in this case) when the servlet is initialised, and is exactly the right place to perform static startup logic, such as the kind you are referring to here.

In fact, the servlet will not even be able to receive connections until its init method has returned, so you can guarantee that the ontology will be fully loaded before the sensor observations arrive.

Andrzej Doyle
  • 102,507
  • 33
  • 189
  • 228
  • This looks like it works! However, I'm having problems with the ontology now. Will accept answer when I can confirm. – Bailz Nov 19 '10 at 15:25
0

You could use event listeners which are invoked when the web app (contex) is loaded. There you initialize your objects and save them to ServletContext where they'll be available to all servlets in your app.

Implement ServletContextListener and in it's contextInitialized() put:

contextInitialized(ServletContextEvent sce){

    // Create your objects
    Object myObject = ...

    sce.getServletContext().setAttribute("myObjectKey", myObject);
} 

Then register the listener in Tomcat context.xml:

<Context path="/examples" ...>
    ...
    <Listener className="com.mycompany.mypackage.MyListener" ... >
    ...
</Context>
Peter Knego
  • 79,991
  • 11
  • 123
  • 154