sateesh
sateesh

Reputation: 83

how to instantiate a class while jboss startup

I would like to instantiate my own java class (One time only) when the time of startup of JBOSS 5 and i will use that object until i shut down the jboss.

How can it be possible to instantiate.

Upvotes: 0

Views: 6063

Answers (1)

Ken Chan
Ken Chan

Reputation: 90447

You can implement your class with the ServletContextListener interface , which make your class able to receive notifications from the application server (i.e JBoss) when it starts and shut-downs .

For example:

public class MyServletContextListener implements ServletContextListener {

            /**This method will run when the web application starts***/
           public void contextInitialized(ServletContextEvent sce) {
            /**Put your codes inside , it will run when JBoss starts ***/
           }

}

Then register your MyServletContextListener in the web.xml :

<?xml version="1.0"?>
<web-app version="2.5" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 
    <listener>
             <listener-class>com.abc.xyz.MyServletContextListener </listener-class>
    </listener>

</web-app> 

Package the application in the format of WAR and deploy it into the JBoss. When the JBoss starts , the contextInitialized() in the MyServletContextListener will run too.

Upvotes: 4

Related Questions