Well, you really should get up to speed on Java servlets. Vaadin relies on it. Some depends on which version you are using.
With Servlets 3.0 supported by Tomcat 7, just creating that ServletContextListener class in your code will cause Tomcat (or any Servlet 3 container) to call your contextInitialized method on startup, which can just call automaticGetResult();
The annotation makes the association that this class is a context listener to be called by the container as it starts/stops. If you are on an older Tomcat/Servlet, you’ll need to add web.xml entry that defines the context listener.
Of course your thread leaves much to be desired because it doesn’t appear to have a way to stop it. Typically, you’ll want to create a flag like ‘boolean continueThread’ set to true and use that instead of ‘while (true)’ you are then ‘while( continueThread )’ and then provide a method to set continueThread to false so the next time it wakes up it will stop itself.
Then in the contextDestroyed() method of the context listener, you’d tell the thread to stop.
Consider a pattern like this, perhaps instead:
To start your background thread (called directly or indirectly from the contextInitialized() method):
try {
backgrounder = new Backgrounder();
Thread thread = new Thread( backgrounder );
thread.setName("MyBackgrounderThread");
thread.setDaemon(true);
thread.setPriority(Thread.currentThread().getPriority()-1);
thread.start();
} catch( Exception e ) {
// report it
}
Then stop it in the contextDestroyed() method:
if ( backgrounder != null ) {
backgrounder.stop();
synchronized(backgrounder) {
backgrounder.notifyAll();
backgrounder = null;
}
}
And your backgroud thread something like:
public class Backgrounder implements java.lang.Runnable {
private boolean shouldContinue = true;
public Backgrounder() {
}
public void run() {
while ( shouldContinue ) {
try {
synchronized(this) {
wait( waitTimeMsec );
}
if ( shouldContinue )
doBackgroundTasks(); // call a method or do whatever you want to do when you wake up
} catch( java.lang.InterruptedException e ) {
stop();
}
}
}
public void stop() {
shouldContinue = false;
}
}