Thread execution - Java EE - Jboss

Asked

Viewed 112 times

4

I have a web application using Jboss EAP, JSF, CDI, EJB and need to start a thread whenever Jboss starts.

The thread belongs to the Tinyradius package that implements a Radius server. How can I invoke the method start always when starting the application?

  • Have you tried using a Servletcontextlistener?

  • Yes, just like that =)

1 answer

2


There are some ways to do, such as using Servletcontextlistener, startup Servlets (see load-on-startup in specification), Resource Adapters in environments full profile, EJB 3.1 @Startup, etc..

The decision of which to use varies from project to project and the various approaches the most common are these:

@WebListener
public class YourStartupApplication implements ServletContextListener {

    @Inject
    private Service service;

    @Override
    public void contextInitialized(final ServletContextEvent sce)  { 
         service.doSomethingOnStartup();
    }

    @Override
    public void contextDestroyed(final ServletContextEvent sce)  {
         service.doSomethingOnShutdown();    
    }

}
@Startup
@Singleton
public class YourStartupApplication {

    @Inject
    private Service service;

    @PostConstruct
    public void startup()  { 
         service.doSomethingOnStartup();
    }

    @PreDestroy
    public void shutdown()  {
         service.doSomethingOnShutdown();    
    }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.