publishEvent(new ContextClosedEvent(this));
publishEvent(new ContextClosedEvent(this));
@Test public void simpleApplicationEventMulticasterWithErrorHandler() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.setErrorHandler(TaskUtils.LOG_AND_SUPPRESS_ERROR_HANDLER); smc.addApplicationListener(listener); willThrow(new RuntimeException()).given(listener).onApplicationEvent(evt); smc.multicastEvent(evt); }
@Test public void multicastSimpleEvent() { multicastEvent(true, ApplicationListener.class, new ContextRefreshedEvent(new StaticApplicationContext()), null); multicastEvent(true, ApplicationListener.class, new ContextClosedEvent(new StaticApplicationContext()), null); }
@Test public void simpleApplicationEventMulticasterWithException() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.addApplicationListener(listener); RuntimeException thrown = new RuntimeException(); willThrow(thrown).given(listener).onApplicationEvent(evt); try { smc.multicastEvent(evt); fail("Should have thrown RuntimeException"); } catch (RuntimeException ex) { assertSame(thrown, ex); } }
@Test public void simpleApplicationEventMulticasterWithTaskExecutor() { @SuppressWarnings("unchecked") ApplicationListener<ApplicationEvent> listener = mock(ApplicationListener.class); ApplicationEvent evt = new ContextClosedEvent(new StaticApplicationContext()); SimpleApplicationEventMulticaster smc = new SimpleApplicationEventMulticaster(); smc.setTaskExecutor(new Executor() { @Override public void execute(Runnable command) { command.run(); command.run(); } }); smc.addApplicationListener(listener); smc.multicastEvent(evt); verify(listener, times(2)).onApplicationEvent(evt); }
publishEvent(new ContextClosedEvent(this));
@Override public void onApplicationEvent(Object event) { if (event instanceof StartupEvent) { eventPublisher.publishEvent(new ContextStartedEvent( applicationContext )); } else if (event instanceof ShutdownEvent) { eventPublisher.publishEvent(new ContextClosedEvent( applicationContext )); } } }
publishEvent(new ContextClosedEvent(this));
public void testBootstrapAndShutdown() throws Exception { // now bring up the bootstrap ApplicationContext ctx = new ClassPathXmlApplicationContext(APP_CONTEXT_XML); // the folder should be gone assertFalse("Folder was not deleted by bootstrap", dir.exists()); // now create the folder again dir.mkdir(); assertTrue("Directory not created", dir.exists()); // announce that the context is closing ctx.publishEvent(new ContextClosedEvent(ctx)); // the folder should be gone assertFalse("Folder was not deleted by shutdown", dir.exists()); }
@Test public void testDestroy() { Connection connection1 = this.connectionFactory.createConnection(); this.connectionFactory.destroy(); Connection connection2 = this.connectionFactory.createConnection(); assertSame(connection1, connection2); ApplicationContext context = mock(ApplicationContext.class); this.connectionFactory.setApplicationContext(context); this.connectionFactory.onApplicationEvent(new ContextClosedEvent(context)); this.connectionFactory.destroy(); try { connection2 = this.connectionFactory.createConnection(); fail("Expected exception"); } catch (AmqpApplicationContextClosedException e) { assertThat(e.getMessage(), containsString("is closed")); } }
@Test public void testNonManagedContainerStopsWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); final CountDownLatch latch = new CountDownLatch(1); container.setMessageListener(m -> { latch.countDown(); }); container.setQueueNames(Q1); container.setBeanName("stopAfterDestroy"); container.setIdleEventInterval(500); container.setFailedDeclarationRetryInterval(500); container.afterPropertiesSet(); container.start(); new RabbitTemplate(cf).convertAndSend(Q1, "foo"); assertTrue(latch.await(10, TimeUnit.SECONDS)); cf.onApplicationEvent(new ContextClosedEvent(context)); cf.destroy(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }
@Test public void testNonManagedContainerDoesntStartWhenConnectionFactoryDestroyed() throws Exception { CachingConnectionFactory cf = new CachingConnectionFactory("localhost"); ApplicationContext context = mock(ApplicationContext.class); cf.setApplicationContext(context); cf.addConnectionListener(connection -> { cf.onApplicationEvent(new ContextClosedEvent(context)); cf.destroy(); }); DirectMessageListenerContainer container = new DirectMessageListenerContainer(cf); container.setMessageListener(m -> { }); container.setQueueNames(Q1); container.setBeanName("stopAfterDestroyBeforeStart"); container.afterPropertiesSet(); container.start(); int n = 0; while (n++ < 100 && container.isRunning()) { Thread.sleep(100); } assertFalse(container.isRunning()); }