Walter White
Walter White

Reputation:

CDI - Observing Container Events

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?

Upvotes: 6

Views: 5552

Answers (2)

George Armhold
George Armhold

Reputation: 31074

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

Upvotes: 7

Walter White
Walter White

Reputation:

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

Upvotes: 15

Related Questions