Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call method in EJB on JBoss startup [duplicate]

I'm looking for an entry point in an EJB deployed on JBoss.

Servlets have the load-on-startup tag to use in its web.xml.

I'm searching for similar init() functionality for an EJB.

like image 911
Elijah Avatar asked Jun 08 '10 07:06

Elijah


3 Answers

That didn't exist for EJB until 3.1. With EJB 3.1 you can use a singleton bean to simulate that:

From Application Startup / Shutdown Callbacks:

   @Startup
   @Singleton
   public class FooBean {

       @PostConstruct 
       void atStartup() { ... }

       @PreDestroy
       void atShutdown() { ... }

   }

Otherwise, you will need to rely on the good old trick to use a ServletContextInitializer.

There are some application-specific extension, e.g. lifecycle listener for Glassfish. Maybe there's such a thing for JBoss.

But if I were you I would try to rely on standard features as much as possible. The problem with non-standard extension is that you never know exactly what can be done or not, e.g. can you start transaction or not, etc.

like image 103
ewernli Avatar answered Sep 19 '22 17:09

ewernli


This article describes seven different ways of invoking functionality at server startup. Not all will work with JBoss though.

Seven ways to get things started. Java EE Startup Classes with GlassFish and WebLogic

like image 4
Thijs Avatar answered Sep 21 '22 17:09

Thijs


If you're targeting JBoss AS 5.1, and you don't mind using the JBoss EJB 3.0 Extensions, you can build a service bean to bootstrap your EJB. If your service implements an interface annotated with the @Management annotation and declares a method with the signature public void start() throws Exception, JBoss will call this method when it starts the service. You can then call a dedicated init() method on the EJB you want to initialize:

@Service
public class BeanLauncher implements BeanLauncherManagement
{
    @EJB private SessionBeanLocal sessionBean;

    @Override
    public void start() throws Exception
    {
        sessionBean.init();
    }
}

@Management
public interface BeanLauncherManagement
{
    public void start() throws Exception;
}

More information on this, including additional life-cycle events, can be found here.

like image 4
lmika Avatar answered Sep 19 '22 17:09

lmika