use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class ConnectionEventTests method testServerExceptionGuts.
private void testServerExceptionGuts(AbstractServerConnectionFactory factory) throws Exception {
ServerSocket ss = null;
try {
ss = ServerSocketFactory.getDefault().createServerSocket(0);
} catch (Exception e) {
fail("Failed to get a server socket");
}
factory.setPort(ss.getLocalPort());
final AtomicReference<TcpConnectionServerExceptionEvent> theEvent = new AtomicReference<TcpConnectionServerExceptionEvent>();
final CountDownLatch latch = new CountDownLatch(1);
factory.setApplicationEventPublisher(new ApplicationEventPublisher() {
@Override
public void publishEvent(ApplicationEvent event) {
theEvent.set((TcpConnectionServerExceptionEvent) event);
latch.countDown();
}
@Override
public void publishEvent(Object event) {
}
});
factory.setBeanName("sf");
factory.registerListener(message -> false);
Log logger = spy(TestUtils.getPropertyValue(factory, "logger", Log.class));
doNothing().when(logger).error(anyString(), any(Throwable.class));
new DirectFieldAccessor(factory).setPropertyValue("logger", logger);
factory.start();
assertTrue(latch.await(10, TimeUnit.SECONDS));
String actual = theEvent.toString();
assertThat(actual, containsString("cause=java.net.BindException"));
assertThat(actual, containsString("source=" + "sf, port=" + factory.getPort()));
ArgumentCaptor<String> reasonCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<Throwable> throwableCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(logger).error(reasonCaptor.capture(), throwableCaptor.capture());
assertThat(reasonCaptor.getValue(), startsWith("Error on Server"));
assertThat(reasonCaptor.getValue(), endsWith("; port = " + factory.getPort()));
assertThat(throwableCaptor.getValue(), instanceOf(BindException.class));
ss.close();
}
use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class RedisQueueMessageDrivenEndpointTests method testInt3196Recovery.
@Test
@RedisAvailable
@Ignore("JedisConnectionFactory doesn't support proper 'destroy()' and allows to create new fresh Redis connection")
public void testInt3196Recovery() throws Exception {
String queueName = "test.si.Int3196Recovery";
QueueChannel channel = new QueueChannel();
final List<ApplicationEvent> exceptionEvents = new ArrayList<>();
final CountDownLatch exceptionsLatch = new CountDownLatch(2);
RedisQueueMessageDrivenEndpoint endpoint = new RedisQueueMessageDrivenEndpoint(queueName, this.connectionFactory);
endpoint.setBeanFactory(Mockito.mock(BeanFactory.class));
endpoint.setApplicationEventPublisher(event -> {
exceptionEvents.add((ApplicationEvent) event);
exceptionsLatch.countDown();
});
endpoint.setOutputChannel(channel);
endpoint.setReceiveTimeout(100);
endpoint.setRecoveryInterval(200);
endpoint.afterPropertiesSet();
endpoint.start();
waitListening(endpoint);
((DisposableBean) this.connectionFactory).destroy();
assertTrue(exceptionsLatch.await(10, TimeUnit.SECONDS));
for (ApplicationEvent exceptionEvent : exceptionEvents) {
assertThat(exceptionEvent, Matchers.instanceOf(RedisExceptionEvent.class));
assertSame(endpoint, exceptionEvent.getSource());
assertThat(((IntegrationEvent) exceptionEvent).getCause().getClass(), Matchers.isIn(Arrays.asList(RedisSystemException.class, RedisConnectionFailureException.class)));
}
((InitializingBean) this.connectionFactory).afterPropertiesSet();
RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
redisTemplate.setConnectionFactory(this.getConnectionFactoryForTest());
redisTemplate.setEnableDefaultSerializer(false);
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
redisTemplate.afterPropertiesSet();
String payload = "testing";
redisTemplate.boundListOps(queueName).leftPush(payload);
Message<?> receive = channel.receive(10000);
assertNotNull(receive);
assertEquals(payload, receive.getPayload());
endpoint.stop();
}
use of org.springframework.context.ApplicationEvent in project spring-integration by spring-projects.
the class ApplicationEventListeningMessageProducerTests method testInt2935CheckRetrieverCache.
@Test
@SuppressWarnings({ "unchecked", "serial" })
public void testInt2935CheckRetrieverCache() {
GenericApplicationContext ctx = TestUtils.createTestApplicationContext();
ConfigurableListableBeanFactory beanFactory = ctx.getBeanFactory();
QueueChannel channel = new QueueChannel();
ApplicationEventListeningMessageProducer listenerMessageProducer = new ApplicationEventListeningMessageProducer();
listenerMessageProducer.setOutputChannel(channel);
listenerMessageProducer.setEventTypes(TestApplicationEvent2.class);
beanFactory.registerSingleton("testListenerMessageProducer", listenerMessageProducer);
AtomicInteger listenerCounter = new AtomicInteger();
beanFactory.registerSingleton("testListener", new TestApplicationListener(listenerCounter));
ctx.refresh();
ApplicationEventMulticaster multicaster = ctx.getBean(AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
Map<?, ?> retrieverCache = TestUtils.getPropertyValue(multicaster, "retrieverCache", Map.class);
ctx.publishEvent(new TestApplicationEvent1());
/*
* Previously, the retrieverCache grew unnecessarily; the adapter was added to the cache for each event type,
* event if not supported.
*/
assertEquals(2, retrieverCache.size());
for (Map.Entry<?, ?> entry : retrieverCache.entrySet()) {
Class<? extends ApplicationEvent> event = TestUtils.getPropertyValue(entry.getKey(), "eventType.resolved", Class.class);
assertThat(event, Matchers.is(Matchers.isOneOf(ContextRefreshedEvent.class, TestApplicationEvent1.class)));
Set<?> listeners = TestUtils.getPropertyValue(entry.getValue(), "applicationListenerBeans", Set.class);
assertEquals(1, listeners.size());
assertEquals("testListener", listeners.iterator().next());
}
TestApplicationEvent2 event2 = new TestApplicationEvent2();
ctx.publishEvent(event2);
assertEquals(3, retrieverCache.size());
for (Map.Entry<?, ?> entry : retrieverCache.entrySet()) {
Class<?> event = TestUtils.getPropertyValue(entry.getKey(), "eventType.resolved", Class.class);
if (TestApplicationEvent2.class.isAssignableFrom(event)) {
Set<?> listeners = TestUtils.getPropertyValue(entry.getValue(), "applicationListenerBeans", Set.class);
assertEquals(2, listeners.size());
for (Object listener : listeners) {
assertThat((String) listener, Matchers.is(Matchers.isOneOf("testListenerMessageProducer", "testListener")));
}
break;
}
}
ctx.publishEvent(new ApplicationEvent("Some event") {
});
assertEquals(4, listenerCounter.get());
final Message<?> receive = channel.receive(10);
assertNotNull(receive);
assertSame(event2, receive.getPayload());
assertNull(channel.receive(1));
ctx.close();
}
use of org.springframework.context.ApplicationEvent in project spring-data-commons by spring-projects.
the class ResourceReaderRepositoryInitializerUnitTests method emitsRepositoriesPopulatedEventIfPublisherConfigured.
// DATACMNS-224
@Test
public void emitsRepositoriesPopulatedEventIfPublisherConfigured() throws Exception {
RepositoryPopulator populator = setUpReferenceAndInititalize(new User(), publisher);
ApplicationEvent event = new RepositoriesPopulatedEvent(populator, repositories);
verify(publisher, times(1)).publishEvent(event);
}
use of org.springframework.context.ApplicationEvent in project spring-security by spring-projects.
the class DelegatingApplicationListenerTests method setup.
@BeforeEach
public void setup() {
this.event = new ApplicationEvent(this) {
};
this.listener = new DelegatingApplicationListener();
this.listener.addListener(this.delegate);
}
Aggregations