eclipse-ee4j / glassfish

Eclipse GlassFish
https://eclipse-ee4j.github.io/glassfish/
377 stars 143 forks source link

[7.0.4] ServletContextListener.contextInitialized invoked multiple times #24415

Closed ctabin closed 12 months ago

ctabin commented 1 year ago

Hello,

In the context of a WAR, we have a simple ServletContextListener to initialize/shutdown our application when it is deployed. We only have one single servlet, so we expect the methods contextInitialized and contextDestroyed to be invoked only once when the application is deployed and undeployed.

Here is some piece of code:

@WebListener
public class DispatcherListener implements ServletContextListener {
    private static final Logger LOG = Logger.getLogger(DispatcherListener.class.getName());

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        log.log(Level.SEVERE, "Listener initialized");
    }
}
@WebServlet(urlPatterns="/*", loadOnStartup=1)
public class DispatcherServlet extends HttpServlet {
   //nothing special here
}

Here the log output after the deployment:

May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized
SEVERE: Listener initialized
May 13, 2023 2:01:56 PM my.application.DispatcherListener contextInitialized

In Payara 6.2023.4, the log does only appear once, which is (in my sense) the expected behavior.

Environment:

OndroMih commented 1 year ago

Thanks for reporting this. It happens also in Embedded 7.0.5

The reason is that in WebArchivist.postAnnotationProcess(), while GlassFish server processes JARs inside the WAR with a separate scanner, Embedded processes them always with the same scanner. Therefore it will discover each annotation in the WAR file multiple times, each time for each JAR file in the WAR.

ctabin commented 1 year ago

Hi @OndroMih, thanks for coming back. Indeed it seems linked to libraries included in standalone war. You'll find a testcase here.

OndroMih commented 1 year ago

It looks like this got broken with https://github.com/eclipse-ee4j/glassfish/pull/24082, which simplified handling of ScatteredArchive for GlassFish Embedded too much. The new ScatteredWebArchivist.EmbeddedWebScanner is called for the whole WAR and also for each JAR, as before, but after #24082 it reports the classes in the classes directory even when called for nested JARs.

https://github.com/eclipse-ee4j/glassfish/pull/24570 should fix this, but I raised is as draft for now because there are still some things to do (e.g. tests).