/** * Handles {@link ContextRefreshedEvent} to invoke * {@link #reschedulePersistedMessages} as late as possible after application context * startup. Also it checks {@link #initialized} to ignore other * {@link ContextRefreshedEvent}s which may be published in the 'parent-child' * contexts, e.g. in the Spring-MVC applications. * @param event - {@link ContextRefreshedEvent} which occurs after Application context * is completely initialized. * @see #reschedulePersistedMessages */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().equals(getApplicationContext()) && !this.initialized.getAndSet(true)) { reschedulePersistedMessages(); } }
@Test //INT-1132 // Can happen in the parent-child context e.g. Spring-MVC applications public void testDoubleOnApplicationEvent() { this.delayHandler = Mockito.spy(this.delayHandler); Mockito.doAnswer(invocation -> null).when(this.delayHandler).reschedulePersistedMessages(); TestApplicationContext ac = TestUtils.createTestApplicationContext(); this.delayHandler.setApplicationContext(ac); ContextRefreshedEvent contextRefreshedEvent = new ContextRefreshedEvent(ac); this.delayHandler.onApplicationEvent(contextRefreshedEvent); this.delayHandler.onApplicationEvent(contextRefreshedEvent); Mockito.verify(this.delayHandler, Mockito.times(1)).reschedulePersistedMessages(); ac.close(); }
dfa.setPropertyValue("requestDate", requestDate.getTimeInMillis()); this.taskScheduler.afterPropertiesSet(); this.delayHandler.reschedulePersistedMessages(); Queue<?> works = TestUtils.getPropertyValue(this.taskScheduler, "scheduledExecutor.workQueue", Queue.class); int n = 0;
@Test //INT-3560 /* It's difficult to test it from real ctx, because any async process from 'inbound-channel-adapter' can't achieve the DelayHandler before the main thread emits 'ContextRefreshedEvent'. */ public void testRescheduleAndHandleAtTheSameTime() { QueueChannel results = new QueueChannel(); delayHandler.setOutputChannel(results); this.delayHandler.setDefaultDelay(10); startDelayerHandler(); this.input.send(new GenericMessage<>("foo")); this.delayHandler.reschedulePersistedMessages(); Message<?> message = results.receive(10000); assertNotNull(message); message = results.receive(50); assertNull(message); }
/** * Handles {@link ContextRefreshedEvent} to invoke * {@link #reschedulePersistedMessages} as late as possible after application context * startup. Also it checks {@link #initialized} to ignore other * {@link ContextRefreshedEvent}s which may be published in the 'parent-child' * contexts, e.g. in the Spring-MVC applications. * @param event - {@link ContextRefreshedEvent} which occurs after Application context * is completely initialized. * @see #reschedulePersistedMessages */ @Override public void onApplicationEvent(ContextRefreshedEvent event) { if (event.getApplicationContext().equals(getApplicationContext()) && !this.initialized.getAndSet(true)) { reschedulePersistedMessages(); } }