CDI - Observing Container Events
Asked Answered
S

2

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?

Sideswipe answered 22/8, 2010 at 4:25 Comment(5)
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.Sideswipe
is your logger configured properly? Try System.outCosine
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: github.com/walterjwhite/prettyfaces.extension.sitemap github.com/walterjwhite/prettyfaces-tests mvn clean package embedded-glassfish:run -P Glassfish,developmentSideswipe
Check out the weld and cdi-jee6 branches. Let me know if that works for you.Sideswipe
I need to use @Stateless instead of @ApplicationScoped on the class. Also, my after deployment validation and before shutdown method aren't being called?Sideswipe
S
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

Sideswipe answered 27/8, 2010 at 22:59 Comment(2)
Thanks for posting this. +1 all around.Pains
You mention creating a special file. What file was that?Parenthesize
D
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
Dustydusza answered 15/12, 2011 at 0:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.