I have a Dynamic webproject created in eclipse with Glassfish server, I want to execute a specific class automatically when the server get started. That class has a listener that listens to a JMS queue, How can I achieve this? Do I have to configure it in web.xml? If so how to do it? Any code example would be really appreciated.
Asked
Active
Viewed 214 times
2 Answers
3
There are alot of ways to achieve this. You can make a Startup EJB Singleton. Something like this:
@Singleton
@Startup
public class MyStartupBean {
@PostConstruct
private void init() {
LOG.info("Initializing startup bean");
// Do stuff
}
}

crea1
- 11,077
- 3
- 36
- 46
1
as a solution you can have a class which implements javax.servlet.ServletContextListener
and register it in your web.xml
<listener>
<listener-class>YourListener</listener-class>
</listener>
ServletContextListener
has two methods:
public void contextInitialized ( ServletContextEvent sce );
public void contextDestroyed ( ServletContextEvent sce );
so this class run automatically when your web project starts If you don'e want to change your JMS listener class you can call it's appropriate method from another context loader class like what I said.

Blue Ocean
- 282
- 1
- 10