Search in sources :

Example 1 with IntegrationEvent

use of org.springframework.integration.events.IntegrationEvent 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();
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) QueueChannel(org.springframework.integration.channel.QueueChannel) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) ApplicationEvent(org.springframework.context.ApplicationEvent) ArrayList(java.util.ArrayList) RedisExceptionEvent(org.springframework.integration.redis.event.RedisExceptionEvent) CountDownLatch(java.util.concurrent.CountDownLatch) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) DisposableBean(org.springframework.beans.factory.DisposableBean) BeanFactory(org.springframework.beans.factory.BeanFactory) IntegrationEvent(org.springframework.integration.events.IntegrationEvent) InitializingBean(org.springframework.beans.factory.InitializingBean) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Ignore (org.junit.Ignore)1 Test (org.junit.Test)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 DisposableBean (org.springframework.beans.factory.DisposableBean)1 InitializingBean (org.springframework.beans.factory.InitializingBean)1 ApplicationEvent (org.springframework.context.ApplicationEvent)1 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)1 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)1 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 IntegrationEvent (org.springframework.integration.events.IntegrationEvent)1 RedisExceptionEvent (org.springframework.integration.redis.event.RedisExceptionEvent)1 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)1