3

How do I know when my Vaadin 7 web app first starting/launching, so I can do some initial set-up work?

Likewise, how do I know when my web app is ending, getting shutdown/exiting?

Basil Bourque
  • 218,480
  • 72
  • 657
  • 915
  • Another question in this area: [How to access `ServletContext` from within a Vaadin 7 app?](http://stackoverflow.com/q/27933599/642706) – Basil Bourque Apr 18 '16 at 01:19

1 Answers1

8

ServletContextListener

Vaadin is built on top of Java Servlet technology. A “context” is the technical term for your web app in Servlet terms. So the answer here is not Vaadin-specific, it applies to any Servlet -- and at the end of the day, Vaadin is just one big Servlet.

Since Servlet spec version 2.3, a Servlet container such as Tomcat, Jetty, etc. must promise to be on the lookout for any Java class you define as implementing the ServletContextListener interface. That interface has two simple methods:

The ending could be caused by the Servlet container (ex: Tomcat) is being shutdown so all the web apps (“contexts”) are ending, or because just your Vaadin app’s context is ending (if your Servlet container supports per-context shutdown).

The contract every Servlet container must fulfill is that each of your ServletContextListener classes (you can have more than one) must have its contextInitialized invoked before any servlet or filter executes. So this is the perfect time to do initialization work that might benefit more than a single Servlet request-response cycle. If you need to startup a database such as [H2 Database), this is a good time. If you load some data into memory as a cache to be used by the servlet(s) repeatedly, this is a good time. Also a good time to test your apps resources, to be certain logging works or certain expected files are in place, for example.

Likewise, every compliant Servlet container invokes contextDestroyed only after the servlet(s) and filters have finished their last invocation. So this is a good place to shutdown your database, make backups, or do any other clean-up chore appropriate to your web app.

We are discussing the life cycle of your web app’s “context”. That context may involve one, or more than one, servlet. This life cycle of the context goes beyond the life cycle of any one of the servlets participating in this context. The context is kinda-sorta like the queen bee who gives birth to all her drones in a new hive, where she was living before them and she will outlive them all as they die off in dutiful service to her (if that is how a hive works?).

Defining your ServletContextListener

Making a ServletContextListener is quite easy: Make a class with a pair of methods plus an annotation.

Add a new Java class as part of your Vaadin app. You can name the class anything you want.

I add my context listeners in the same package as my main Vaadin app UI class (MyUI.java may have been generated by your Vaadin plugin or by Maven archetype). Seems like a natural place as the context listener is the beginning of my Vaadin app launching before any user is handled while the designated UI class will then be the first piece of my Vaadin app being run for each user.

Declare your class as implementing ServleContextListener. Add the two required methods discussed above; your IDE may assist with this chore.

One more trick: You must inform the Servlet container about this context listener. There is more than one way to do this, but I use the simplest, an annotation @WebListener on the class.

Here is an entire example class.

package com.example.amazingapp;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
 *
 * @author Basil Bourque
 */
@WebListener
public class WebAppListener implements ServletContextListener {

    @Override
    public void contextInitialized ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is starting. " );
    }

    @Override
    public void contextDestroyed ( ServletContextEvent sce ) {
        System.out.println ( "My Vaadin web app is shutting down." );
    }

}
Basil Bourque
  • 218,480
  • 72
  • 657
  • 915