Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate object when Tomcat starts

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?

like image 636
Bailz Avatar asked Feb 26 '23 19:02

Bailz


1 Answers

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.

like image 88
Philipp Jardas Avatar answered Mar 07 '23 02:03

Philipp Jardas