Παρασκευή, 27 Φεβρουαρίου 2009

J2ee Start a class when application server starts

If you want to start a class when your application server starts, you simply have to follow two steps:

1) create a new class that implements ServletContextListener.
then implement the two methods: contextInitialized(ServletContextEvent event) and contextDestroyed(ServletContextEvent event).

example:

public final class MyContextListener implements
ServletContextListener {

public void contextInitialized(ServletContextEvent event) {

/* This method is called when the servlet context is
initialized(when the Web Application is deployed).
You can initialize servlet context related data here.
*/
MainThread mainthread1 = new MainThread();
mainthread1.start();
}

public void contextDestroyed(ServletContextEvent event) {

/* This method is invoked when the Servlet Context
(the Web Application) is undeployed or
WebLogic Server shuts down.
*/

System.out.println("goodbye");
//thread's clean up code
}
}



Then add:

Tools.MyContextListener


under the web-app of web.xml file.

That's it!

Δεν υπάρχουν σχόλια: