From 282e1a7b1a7123abe21de8ac022b7f5636da79da Mon Sep 17 00:00:00 2001 From: Matt Gilman Date: Fri, 24 Mar 2017 16:30:48 -0400 Subject: [PATCH] NIFI-3421: This closes #1620. - On contextDestroyed, referencing beans created during contextInitialized to prevent successive attempts to create a bean if that bean failed to be created initially. Signed-off-by: joewitt --- .../ApplicationStartupContextListener.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/contextlistener/ApplicationStartupContextListener.java b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/contextlistener/ApplicationStartupContextListener.java index 6ce7cb4f8d..70f2cecf34 100644 --- a/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/contextlistener/ApplicationStartupContextListener.java +++ b/nifi-nar-bundles/nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/contextlistener/ApplicationStartupContextListener.java @@ -43,15 +43,18 @@ public class ApplicationStartupContextListener implements ServletContextListener private static final Logger logger = LoggerFactory.getLogger(ApplicationStartupContextListener.class); + private FlowController flowController = null; + private FlowService flowService = null; + private RequestReplicator requestReplicator = null; + @Override public void contextInitialized(ServletContextEvent sce) { - - ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); - NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class); - FlowService flowService = null; + final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); + final NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class); try { flowService = ctx.getBean("flowService", FlowService.class); - final FlowController flowController = ctx.getBean("flowController", FlowController.class); + flowController = ctx.getBean("flowController", FlowController.class); + requestReplicator = ctx.getBean("requestReplicator", RequestReplicator.class); // start and load the flow if we're not clustered (clustered flow loading should // happen once the application (wars) is fully loaded and initialized). non clustered @@ -81,7 +84,7 @@ public class ApplicationStartupContextListener implements ServletContextListener logger.info("Flow Controller started successfully."); } } catch (BeansException | RepositoryPurgeException | IOException e) { - shutdown(flowService, ctx.getBean("requestReplicator", RequestReplicator.class)); + shutdown(); throw new NiFiCoreException("Unable to start Flow Controller.", e); } @@ -90,21 +93,19 @@ public class ApplicationStartupContextListener implements ServletContextListener ctx.getBean("loginIdentityProvider", LoginIdentityProvider.class); ctx.getBean("authorizer", Authorizer.class); } catch (final BeansException e) { - shutdown(flowService, ctx.getBean("requestReplicator", RequestReplicator.class)); + shutdown(); throw new NiFiCoreException("Unable to start Flow Controller.", e); } } @Override public void contextDestroyed(ServletContextEvent sce) { - ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); - logger.info("Initiating shutdown of flow service..."); - shutdown(ctx.getBean("flowService", FlowService.class), ctx.getBean("requestReplicator", RequestReplicator.class)); + shutdown(); logger.info("Flow service termination completed."); } - private void shutdown(final FlowService flowService, final RequestReplicator requestReplicator) { + private void shutdown() { try { // ensure the flow service is terminated if (flowService != null && flowService.isRunning()) {