Search in sources :

Example 6 with ContextRefreshedEvent

use of org.springframework.context.event.ContextRefreshedEvent in project tephra by heisedebaise.

the class ContainerTest method contextRefreshed.

@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);
}
Also used : Field(java.lang.reflect.Field) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ApplicationContextEvent(org.springframework.context.event.ApplicationContextEvent) Test(org.junit.Test)

Example 7 with ContextRefreshedEvent

use of org.springframework.context.event.ContextRefreshedEvent in project spring-integration by spring-projects.

the class DelayHandlerTests method testDoubleOnApplicationEvent.

// INT-1132
@Test
public // Can happen in the parent-child context e.g. Spring-MVC applications
void testDoubleOnApplicationEvent() throws Exception {
    this.delayHandler = Mockito.spy(this.delayHandler);
    Mockito.doAnswer(invocation -> null).when(this.delayHandler).reschedulePersistedMessages();
    ContextRefreshedEvent contextRefreshedEvent = new ContextRefreshedEvent(TestUtils.createTestApplicationContext());
    this.delayHandler.onApplicationEvent(contextRefreshedEvent);
    this.delayHandler.onApplicationEvent(contextRefreshedEvent);
    Mockito.verify(this.delayHandler, Mockito.times(1)).reschedulePersistedMessages();
}
Also used : ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Test(org.junit.Test)

Example 8 with ContextRefreshedEvent

use of org.springframework.context.event.ContextRefreshedEvent in project spring-integration by spring-projects.

the class NotificationListeningMessageProducerTests method simpleNotification.

@Test
public void simpleNotification() throws Exception {
    QueueChannel outputChannel = new QueueChannel();
    NotificationListeningMessageProducer adapter = new NotificationListeningMessageProducer();
    adapter.setServer(this.server);
    adapter.setObjectName(this.objectName);
    adapter.setOutputChannel(outputChannel);
    adapter.setBeanFactory(mock(BeanFactory.class));
    adapter.afterPropertiesSet();
    adapter.start();
    adapter.onApplicationEvent(new ContextRefreshedEvent(Mockito.mock(ApplicationContext.class)));
    this.numberHolder.publish("foo");
    Message<?> message = outputChannel.receive(0);
    assertNotNull(message);
    assertTrue(message.getPayload() instanceof Notification);
    Notification notification = (Notification) message.getPayload();
    assertEquals("foo", notification.getMessage());
    assertEquals(objectName, notification.getSource());
    assertNull(message.getHeaders().get(JmxHeaders.NOTIFICATION_HANDBACK));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Notification(javax.management.Notification) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) Test(org.junit.Test)

Example 9 with ContextRefreshedEvent

use of org.springframework.context.event.ContextRefreshedEvent in project x-pipe by ctripcorp.

the class SpringComponentLifecycleManager method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof ContextRefreshedEvent) {
        try {
            logger.info("[onApplicationEvent][ContextRefreshedEvent, startAll]");
            startAll();
        } catch (Exception e) {
            throw new XpipeRuntimeException("[startAll][fail]", e);
        }
    }
    if (event instanceof ContextClosedEvent) {
        try {
            logger.info("[onApplicationEvent][ContextClosedEvent, stopAll]");
            stopAll();
        } catch (Exception e) {
            logger.error("[onApplicationEvent][stop all]", e);
            throw new XpipeRuntimeException("[stopAll][fail]", e);
        }
    }
}
Also used : XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) BeansException(org.springframework.beans.BeansException) XpipeRuntimeException(com.ctrip.xpipe.exception.XpipeRuntimeException) ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) ContextClosedEvent(org.springframework.context.event.ContextClosedEvent)

Example 10 with ContextRefreshedEvent

use of org.springframework.context.event.ContextRefreshedEvent in project molgenis by molgenis.

the class ImportServiceIT method beforeClass.

@BeforeClass
public void beforeClass() {
    ContextRefreshedEvent contextRefreshedEvent = Mockito.mock(ContextRefreshedEvent.class);
    Mockito.when(contextRefreshedEvent.getApplicationContext()).thenReturn(applicationContext);
    importServiceRegistrar.register(contextRefreshedEvent);
    RunAsSystemAspect.runAsSystem(() -> dataService.add(USER, getTestUser()));
}
Also used : ContextRefreshedEvent(org.springframework.context.event.ContextRefreshedEvent) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

ContextRefreshedEvent (org.springframework.context.event.ContextRefreshedEvent)28 Test (org.junit.Test)8 Test (org.junit.jupiter.api.Test)8 ApplicationContext (org.springframework.context.ApplicationContext)6 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)5 ContextClosedEvent (org.springframework.context.event.ContextClosedEvent)4 Notification (javax.management.Notification)3 BeanFactory (org.springframework.beans.factory.BeanFactory)3 AbstractApplicationContext (org.springframework.context.support.AbstractApplicationContext)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 XpipeRuntimeException (com.ctrip.xpipe.exception.XpipeRuntimeException)1 Field (java.lang.reflect.Field)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 MalformedObjectNameException (javax.management.MalformedObjectNameException)1 ObjectName (javax.management.ObjectName)1 RunAsWork (org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork)1 RoutesCollector (org.apache.camel.spring.boot.RoutesCollector)1 BeansException (org.springframework.beans.BeansException)1