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 <joewitt@apache.org>
This commit is contained in:
Matt Gilman 2017-03-24 16:30:48 -04:00 committed by joewitt
parent e203535163
commit 282e1a7b1a

View File

@ -43,15 +43,18 @@ public class ApplicationStartupContextListener implements ServletContextListener
private static final Logger logger = LoggerFactory.getLogger(ApplicationStartupContextListener.class); private static final Logger logger = LoggerFactory.getLogger(ApplicationStartupContextListener.class);
private FlowController flowController = null;
private FlowService flowService = null;
private RequestReplicator requestReplicator = null;
@Override @Override
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
final ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext()); final NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class);
NiFiProperties properties = ctx.getBean("nifiProperties", NiFiProperties.class);
FlowService flowService = null;
try { try {
flowService = ctx.getBean("flowService", FlowService.class); 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 // 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 // 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."); logger.info("Flow Controller started successfully.");
} }
} catch (BeansException | RepositoryPurgeException | IOException e) { } catch (BeansException | RepositoryPurgeException | IOException e) {
shutdown(flowService, ctx.getBean("requestReplicator", RequestReplicator.class)); shutdown();
throw new NiFiCoreException("Unable to start Flow Controller.", e); 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("loginIdentityProvider", LoginIdentityProvider.class);
ctx.getBean("authorizer", Authorizer.class); ctx.getBean("authorizer", Authorizer.class);
} catch (final BeansException e) { } catch (final BeansException e) {
shutdown(flowService, ctx.getBean("requestReplicator", RequestReplicator.class)); shutdown();
throw new NiFiCoreException("Unable to start Flow Controller.", e); throw new NiFiCoreException("Unable to start Flow Controller.", e);
} }
} }
@Override @Override
public void contextDestroyed(ServletContextEvent sce) { public void contextDestroyed(ServletContextEvent sce) {
ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sce.getServletContext());
logger.info("Initiating shutdown of flow service..."); 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."); logger.info("Flow service termination completed.");
} }
private void shutdown(final FlowService flowService, final RequestReplicator requestReplicator) { private void shutdown() {
try { try {
// ensure the flow service is terminated // ensure the flow service is terminated
if (flowService != null && flowService.isRunning()) { if (flowService != null && flowService.isRunning()) {