Alternative for ServletContextListener in webflux

287 Views Asked by At

Since, ServletContextListener part of servlet-api which is not available in netty. Can someone help me for alternative for ServletContextListener in webflux ?

Code

public abstract class AppContextListenerBase implements ServletContextListener {
    protected LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    protected String appName;

    public void contextInitialized(final ServletContextEvent event) {
      initializeAppParameters(event.getServletContext());
      appName = AppUtil.getApplicationName();
        

        try {

            doInit(event);

        } catch (FileNotFoundException ex) {
            LogbackConfigurer.initDefaults();
            event.getServletContext().log("[" + appName + "] Invalid logbackConfigLocation", ex);
        } catch (Exception e) {
            LogbackConfigurer.initDefaults();
            event.getServletContext().log("[" + appName + "] Unexpected error during logback initialization ", e);
        }
    }

    public void contextDestroyed(ServletContextEvent event) {
        
      //clean -up context 
    }


    private void initializeAppParameters(ServletContext appContext) {
        String appNameLocal          = getAppNameFromContext(appContext);
        String implementationVersion = getImplementationVersionFromContext(appContext);
        //some more code using above variables 
    }

      protected void doInit(ServletContextEvent event) throws Exception {
        ServletContext sc  = event.getServletContext();
        WebApplicationContext c = WebApplicationContextUtils.getWebApplicationContext(sc);

        AppConfigurer appConfigurer = c.getBean(AppConfigurer.class);
        appConfigurer.configure();
      }

}

Alternative, which could help my application to initialise required service/context at the start-up.

0

There are 0 best solutions below