org.springframework.context.event.ApplicationContextEvent Java Examples

The following examples show how to use org.springframework.context.event.ApplicationContextEvent. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringProcessApplication.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
  try {
    // we only want to listen for context events of the main application
    // context, not its children
    if (event.getSource().equals(applicationContext)) {
      if (event instanceof ContextRefreshedEvent && !isDeployed) {
        // deploy the process application
        afterPropertiesSet();
      } else if (event instanceof ContextClosedEvent) {
        // undeploy the process application
        destroy();
      } else {
        // ignore
      }
    }
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example #2
Source File: AstrixFrameworkBean.java    From astrix with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
	if (event instanceof ContextRefreshedEvent && !servicePublisherStarted) {
		// Application initialization complete. Export astrix-services.
		if (isServer()) {
			this.astrixContext.startServicePublisher();
		}
		servicePublisherStarted = true;
	} else if (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent) {
		/*
		 * What's the difference between the "stopped" and "closed" event? In our embedded
		 * integration tests we only receive ContextClosedEvent
		 */
		destroyAstrixContext();
	}
}
 
Example #3
Source File: SleuthContextListener.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationEvent event) {
	if (event instanceof ContextRefreshedEvent
			|| event instanceof ContextClosedEvent) {
		if (log.isDebugEnabled()) {
			log.debug("Context refreshed or closed [" + event + "]");
		}
		ApplicationContextEvent contextEvent = (ApplicationContextEvent) event;
		ApplicationContext context = contextEvent.getApplicationContext();
		BeanFactory beanFactory = context;
		if (context instanceof ConfigurableApplicationContext) {
			beanFactory = ((ConfigurableApplicationContext) context).getBeanFactory();
		}
		SleuthContextListener listener = CACHE.getOrDefault(beanFactory, this);
		listener.refreshed.compareAndSet(false,
				event instanceof ContextRefreshedEvent);
		listener.closed.compareAndSet(false, event instanceof ContextClosedEvent);
		CACHE.put(beanFactory, listener);
	}
}
 
Example #4
Source File: GaeAppEventListener.java    From gae with MIT License 6 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
    // 上下文初始化完成时启动vertx
    if (event instanceof ContextRefreshedEvent) {
        startHttpServer(event.getApplicationContext());

    } else if (event instanceof ContextClosedEvent) {
        // 上下文关闭时关闭vertx
        IndexIncrementLoader loader = event.getApplicationContext().getBean(IndexIncrementLoader.class);

        try {
            vertx.close();
            if (null != loader) {
                loader.shutdown();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
 
Example #5
Source File: HierarchicalSpringEventPropagateDemo.java    From geekbang-lessons with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
    if (processedEvents.add(event)) {
        System.out.printf("监听到 Spring 应用上下文[ ID : %s ] 事件 :%s\n", event.getApplicationContext().getId(),
                event.getClass().getSimpleName());
    }
}
 
Example #6
Source File: JMXControl.java    From cougar with Apache License 2.0 5 votes vote down vote up
/**
 * Return JMXControl bean from the ApplicationContext *if* the given event is
 * an {@link ApplicationContextEvent} (or null otherwise).
 * <p>
 * A utility method wrapping one way in which we'd call
 * {@link #getFromContext(ApplicationContext)}.
 */
public static JMXControl getFromEvent(ApplicationEvent event) {
	if (event instanceof ApplicationContextEvent) {
		return getFromContext(((ApplicationContextEvent) event).getApplicationContext());
	}
	else {
		return null;
	}
}
 
Example #7
Source File: MuleContextInitializer.java    From mule-spring-boot-starter with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
	if (event instanceof ContextRefreshedEvent && event.getApplicationContext().getParent() == null) {
		startMuleContext(event.getApplicationContext());
	} else if (event instanceof ContextClosedEvent) {
		stopMuleContext();
	}
}
 
Example #8
Source File: GenericEventAdapter.java    From inception with Apache License 2.0 5 votes vote down vote up
@Override
public boolean accepts(Object aEvent)
{
    return aEvent instanceof ApplicationEvent && !(
            aEvent instanceof ApplicationContextEvent || 
            aEvent instanceof ServletRequestHandledEvent ||
            aEvent instanceof SessionCreationEvent ||
            aEvent instanceof SessionDestroyedEvent ||
            aEvent instanceof AbstractAuthorizationEvent ||
            aEvent instanceof AbstractAuthenticationEvent ||
            aEvent instanceof WebServerInitializedEvent);
}
 
Example #9
Source File: ContainerTest.java    From tephra with MIT License 5 votes vote down vote up
@Test
public void contextClosed() throws Exception {
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextClosedEvent(getApplicationContext()));
        Assert.assertEquals(2 + i * 2, contextClosedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++)
            Assert.assertEquals(j % 2 + 1, numeric.toInt(contextClosedList.get(j)));
    }

    Field field = ContainerImpl.class.getDeclaredField("closedListeners");
    field.setAccessible(true);
    Object object = field.get(container);
    field.set(container, Optional.empty());
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextClosedEvent(getApplicationContext()));
        Assert.assertEquals(4, contextClosedList.size());
        for (int j = 0; j < contextClosedList.size(); j++)
            Assert.assertEquals(j % 2 + 1, numeric.toInt(contextClosedList.get(j)));
    }

    ((ContainerImpl) container).onApplicationEvent(new ApplicationContextEvent(getApplicationContext()) {
    });
    Assert.assertEquals(4, contextClosedList.size());
    for (int i = 0; i < contextClosedList.size(); i++)
        Assert.assertEquals(i % 2 + 1, numeric.toInt(contextClosedList.get(i)));

    field.set(container, object);
}
 
Example #10
Source File: ContainerTest.java    From tephra with MIT License 5 votes vote down vote up
@Test
public void contextRefreshed() throws Exception {
    Assert.assertEquals(2, contextRefreshedList.size());
    for (int i = 0; i < contextRefreshedList.size(); i++)
        Assert.assertEquals(i + 1, numeric.toInt(contextRefreshedList.get(i)));

    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextRefreshedEvent(getApplicationContext()));
        Assert.assertEquals(4 + i * 2, contextRefreshedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++)
            Assert.assertEquals(j % 2 + 1, numeric.toInt(contextRefreshedList.get(j)));
    }

    Field field = ContainerImpl.class.getDeclaredField("refreshedListeners");
    field.setAccessible(true);
    Object object = field.get(container);
    field.set(container, Optional.empty());
    for (int i = 0; i < 2; i++) {
        ((ContainerImpl) container).onApplicationEvent(new ContextRefreshedEvent(getApplicationContext()));
        Assert.assertEquals(6, contextRefreshedList.size());
        for (int j = 0; j < contextRefreshedList.size(); j++)
            Assert.assertEquals(j % 2 + 1, numeric.toInt(contextRefreshedList.get(j)));
    }

    ((ContainerImpl) container).onApplicationEvent(new ApplicationContextEvent(getApplicationContext()) {
    });
    Assert.assertEquals(6, contextRefreshedList.size());
    for (int i = 0; i < contextRefreshedList.size(); i++)
        Assert.assertEquals(i % 2 + 1, numeric.toInt(contextRefreshedList.get(i)));

    field.set(container, object);
}
 
Example #11
Source File: ShutdownIndicator.java    From alfresco-data-model with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event)
{
    if (event instanceof ContextClosedEvent && event.getSource() == applicationContext)
    {
        synchronized (this)
        {
            shuttingDown = true;
        }
    }
}
 
Example #12
Source File: ThumbnailRegistry.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void onApplicationEvent(ApplicationContextEvent event)
{
    lifecycle.onApplicationEvent(event);
}
 
Example #13
Source File: AutoProxyLazyInitTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #14
Source File: AutoProxyLazyInitTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #15
Source File: CMISConnector.java    From alfresco-repository with GNU Lesser General Public License v3.0 4 votes vote down vote up
public void onApplicationEvent(ApplicationContextEvent event)
{
    lifecycle.onApplicationEvent(event);
}
 
Example #16
Source File: AutoProxyLazyInitTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #17
Source File: OnceApplicationContextEventListener.java    From spring-context-support with Apache License 2.0 4 votes vote down vote up
public final void onApplicationEvent(ApplicationEvent event) {
    if (isOriginalEventSource(event) && event instanceof ApplicationContextEvent) {
        onApplicationContextEvent((ApplicationContextEvent) event);
    }
}
 
Example #18
Source File: AutoProxyLazyInitTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #19
Source File: AutoProxyLazyInitTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #20
Source File: InitFileConfig.java    From plumemo with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final ApplicationContextEvent event) {
    initFile();
}
 
Example #21
Source File: InitDatabaseConfig.java    From plumemo with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(final ApplicationContextEvent applicationContextEvent) {
    init();
}
 
Example #22
Source File: InitSystemConfig.java    From plumemo with Apache License 2.0 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent applicationContextEvent) {
    init();
}
 
Example #23
Source File: AutoProxyLazyInitTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public void onApplicationEvent(ApplicationContextEvent event) {
}
 
Example #24
Source File: OnceApplicationContextEventListener.java    From spring-context-support with Apache License 2.0 2 votes vote down vote up
/**
 * The subclass overrides this method to handle {@link ApplicationContextEvent}
 * @param event {@link ApplicationContextEvent}
 */
protected abstract void onApplicationContextEvent(ApplicationContextEvent event);
 
Example #25
Source File: AnnotatedEventListenerOnMultiEventsBootstrap.java    From thinking-in-spring-boot-samples with Apache License 2.0 2 votes vote down vote up
/**
 * 单一 {@link ApplicationContextEvent} 参数监听 {@link ContextRefreshedEvent} 和 {@link ContextClosedEvent} 事件
 *
 * @param event {@link ApplicationContextEvent}
 */
@EventListener({ContextRefreshedEvent.class, ContextClosedEvent.class})
public void onApplicationContextEvent(ApplicationContextEvent event) {
    System.out.println("onApplicationContextEvent : " + event.getClass().getSimpleName());
}