12

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.

ewernli
  • 38,045
  • 5
  • 92
  • 123
aelgn
  • 821
  • 1
  • 11
  • 17

5 Answers5

26

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.

Nicholas DiPiazza
  • 10,029
  • 11
  • 83
  • 152
ewernli
  • 38,045
  • 5
  • 92
  • 123
  • 1
    Alternatively one can always use a MBean with start/stop lifecycle methods. The MBean is injected with the EJB needed and calls whatever methods from it that are required. – Bozhidar Batsov Jun 08 '10 at 08:00
  • Thanks. Two great posts. I ended up with a "ServletWrapper" instead. But a refactor is on the agenda. Cheers! – aelgn Jun 09 '10 at 15:11
  • I am using jboss 5.1 (not sure if it supports EJB3.1) I can use this Technic to invoke method on deploy? thanks. – rayman Jan 04 '11 at 09:32
4

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.

lmika
  • 1,725
  • 16
  • 23
4

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

Thijs
  • 283
  • 2
  • 5
1

Managed Beans can be used to do some process at JBoss startup, you have to add entry of that managed bean in configuration file.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
1

You should be able to add the following line to the top of the method you want to run at startup:

@Observer("org.jboss.seam.postInitialization")
Andy
  • 1,815
  • 2
  • 22
  • 49