6

I am trying to observe both the startup and shutdown events for a CDI web application. I have an ApplicationScoped bean that listens for those events:

@ApplicationScoped
public class PrettyfacesStartupObserver
{
    private static final Log LOGGER = LogFactory.getLog(PrettyfacesStartupObserver.class);

    public PrettyfacesStartupObserver()
    {
        LOGGER.debug("\n\n\n\n\n\n\n\n\n\nconstructor");
    }

    public void onStartup(@Observes
    AfterBeanDiscovery afterBeanDiscovery
                                             )
    {
        LOGGER.debug("\n\n\n\n\n\n\n\n\n\nafter bean discover");
    }

    public void onStartup(@Observes
    AfterDeploymentValidation afterDeploymentValidation
                                             )
    {
        LOGGER.debug("\n\n\n\n\n\n\n\n\n\n\nafter deployment validation");
    }

    public void onShutdown(@Observes
    BeforeShutdown beforeShutdown
                                                )
    {
        LOGGER.debug("\n\n\n\n\n\n\n\n\n\n\nbefore shutdown:" + beforeShutdown);
    }

I don't see anything in the logs.

What am I missing?

Stuart Cook
  • 3,994
  • 25
  • 23
  • I have an empty beans.xml in this archive. I don't believe this bean ever gets constructed as I don't see any log statements produced by it. –  Aug 24 '10 at 12:16
  • is your logger configured properly? Try System.out – Bozho Aug 24 '10 at 13:23
  • Na, I tried that along with System.exit, both were a no go. It's never being called. Here is what I'm trying to run: http://github.com/walterjwhite/prettyfaces.extension.sitemap http://github.com/walterjwhite/prettyfaces-tests mvn clean package embedded-glassfish:run -P Glassfish,development –  Aug 24 '10 at 21:19
  • Check out the weld and cdi-jee6 branches. Let me know if that works for you. –  Aug 24 '10 at 21:22
  • I need to use @Stateless instead of @ApplicationScoped on the class. Also, my after deployment validation and before shutdown method aren't being called? –  Aug 25 '10 at 02:17

2 Answers2

15

Thanks to Pete Muir, the solution was to implement the Extension interface. Once I did that, along with creating a special file, it worked perfectly.

The thing to remember is, if you want to observe (or act on) container events, you must implement the extension interface as it is a special event.

https://docs.jboss.org/weld/reference/latest/en-US/html/extend.html#d0e4984

Walter

7

The "special file" mentioned by Walter White is:

META-INF/services/javax.enterprise.inject.spi.Extension

That file should contain the fully-qualified name of your Extension class. ie:

org.mydomain.extension.MyExtension
ondrej kosatka
  • 458
  • 4
  • 7
George Armhold
  • 30,824
  • 50
  • 153
  • 232