Search in sources :

Example 21 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisLockRegistryLeaderInitiatorTests method testDistributedLeaderElection.

@Test
@RedisAvailable
public void testDistributedLeaderElection() throws Exception {
    CountDownLatch granted = new CountDownLatch(1);
    CountingPublisher countingPublisher = new CountingPublisher(granted);
    List<LockRegistryLeaderInitiator> initiators = new ArrayList<>();
    for (int i = 0; i < 2; i++) {
        RedisLockRegistry registry = new RedisLockRegistry(getConnectionFactoryForTest(), "LeaderInitiator");
        LockRegistryLeaderInitiator initiator = new LockRegistryLeaderInitiator(registry, new DefaultCandidate("foo", "bar"));
        initiator.setLeaderEventPublisher(countingPublisher);
        initiators.add(initiator);
    }
    for (LockRegistryLeaderInitiator initiator : initiators) {
        initiator.start();
    }
    assertThat(granted.await(10, TimeUnit.SECONDS), is(true));
    LockRegistryLeaderInitiator initiator1 = countingPublisher.initiator;
    LockRegistryLeaderInitiator initiator2 = null;
    for (LockRegistryLeaderInitiator initiator : initiators) {
        if (initiator != initiator1) {
            initiator2 = initiator;
            break;
        }
    }
    assertNotNull(initiator2);
    assertThat(initiator1.getContext().isLeader(), is(true));
    assertThat(initiator2.getContext().isLeader(), is(false));
    final CountDownLatch granted1 = new CountDownLatch(1);
    final CountDownLatch granted2 = new CountDownLatch(1);
    CountDownLatch revoked1 = new CountDownLatch(1);
    CountDownLatch revoked2 = new CountDownLatch(1);
    CountDownLatch acquireLockFailed1 = new CountDownLatch(1);
    CountDownLatch acquireLockFailed2 = new CountDownLatch(1);
    initiator1.setLeaderEventPublisher(new CountingPublisher(granted1, revoked1, acquireLockFailed1) {

        @Override
        public void publishOnRevoked(Object source, Context context, String role) {
            try {
                // It's difficult to see round-robin election, so block one initiator until the second is elected.
                assertThat(granted2.await(10, TimeUnit.SECONDS), is(true));
            } catch (InterruptedException e) {
            // No op
            }
            super.publishOnRevoked(source, context, role);
        }
    });
    initiator2.setLeaderEventPublisher(new CountingPublisher(granted2, revoked2, acquireLockFailed2) {

        @Override
        public void publishOnRevoked(Object source, Context context, String role) {
            try {
                // It's difficult to see round-robin election, so block one initiator until the second is elected.
                assertThat(granted1.await(10, TimeUnit.SECONDS), is(true));
            } catch (InterruptedException e) {
            // No op
            }
            super.publishOnRevoked(source, context, role);
        }
    });
    initiator1.getContext().yield();
    assertThat(revoked1.await(10, TimeUnit.SECONDS), is(true));
    assertThat(initiator2.getContext().isLeader(), is(true));
    assertThat(initiator1.getContext().isLeader(), is(false));
    initiator2.getContext().yield();
    assertThat(revoked2.await(10, TimeUnit.SECONDS), is(true));
    assertThat(initiator1.getContext().isLeader(), is(true));
    assertThat(initiator2.getContext().isLeader(), is(false));
    initiator2.stop();
    CountDownLatch revoked11 = new CountDownLatch(1);
    initiator1.setLeaderEventPublisher(new CountingPublisher(new CountDownLatch(1), revoked11, new CountDownLatch(1)));
    initiator1.getContext().yield();
    assertThat(revoked11.await(10, TimeUnit.SECONDS), is(true));
    assertThat(initiator1.getContext().isLeader(), is(false));
    initiator1.stop();
}
Also used : Context(org.springframework.integration.leader.Context) ArrayList(java.util.ArrayList) DefaultCandidate(org.springframework.integration.leader.DefaultCandidate) CountDownLatch(java.util.concurrent.CountDownLatch) LockRegistryLeaderInitiator(org.springframework.integration.support.leader.LockRegistryLeaderInitiator) RedisLockRegistry(org.springframework.integration.redis.util.RedisLockRegistry) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 22 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisQueueOutboundChannelAdapterTests method testInt3932LeftPushFalse.

@Test
@RedisAvailable
public void testInt3932LeftPushFalse() throws Exception {
    final String queueName = "si.test.Int3932LeftPushFalse";
    final RedisQueueOutboundChannelAdapter handler = new RedisQueueOutboundChannelAdapter(queueName, this.connectionFactory);
    handler.setLeftPush(false);
    String payload = "testing";
    handler.handleMessage(MessageBuilder.withPayload(payload).build());
    Date payload2 = new Date();
    handler.handleMessage(MessageBuilder.withPayload(payload2).build());
    RedisTemplate<String, ?> redisTemplate = new StringRedisTemplate();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.afterPropertiesSet();
    Object result = redisTemplate.boundListOps(queueName).leftPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result);
    assertEquals(payload, result);
    RedisTemplate<String, ?> redisTemplate2 = new RedisTemplate<String, Object>();
    redisTemplate2.setConnectionFactory(this.connectionFactory);
    redisTemplate2.setEnableDefaultSerializer(false);
    redisTemplate2.setKeySerializer(new StringRedisSerializer());
    redisTemplate2.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate2.afterPropertiesSet();
    Object result2 = redisTemplate2.boundListOps(queueName).leftPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result2);
    assertEquals(payload2, result2);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) Date(java.util.Date) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 23 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisQueueOutboundChannelAdapterTests method testInt3015ExtractPayloadFalse.

@Test
@RedisAvailable
public void testInt3015ExtractPayloadFalse() throws Exception {
    final String queueName = "si.test.testRedisQueueOutboundChannelAdapter2";
    final RedisQueueOutboundChannelAdapter handler = new RedisQueueOutboundChannelAdapter(queueName, this.connectionFactory);
    handler.setExtractPayload(false);
    Message<String> message = MessageBuilder.withPayload("testing").build();
    handler.handleMessage(message);
    RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.setEnableDefaultSerializer(false);
    redisTemplate.setKeySerializer(new StringRedisSerializer());
    redisTemplate.setValueSerializer(new JdkSerializationRedisSerializer());
    redisTemplate.afterPropertiesSet();
    Object result = redisTemplate.boundListOps(queueName).rightPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result);
    assertEquals(message, result);
}
Also used : StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) JdkSerializationRedisSerializer(org.springframework.data.redis.serializer.JdkSerializationRedisSerializer) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 24 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisQueueOutboundChannelAdapterTests method testInt3017IntegrationOutbound.

@Test
@RedisAvailable
public void testInt3017IntegrationOutbound() throws Exception {
    final String queueName = "si.test.Int3017IntegrationOutbound";
    GenericMessage<Object> message = new GenericMessage<Object>(queueName);
    this.sendChannel.send(message);
    RedisTemplate<String, String> redisTemplate = new StringRedisTemplate();
    redisTemplate.setConnectionFactory(this.connectionFactory);
    redisTemplate.afterPropertiesSet();
    String result = redisTemplate.boundListOps(queueName).rightPop(5000, TimeUnit.MILLISECONDS);
    assertNotNull(result);
    InboundMessageMapper<String> mapper = new JsonInboundMessageMapper(String.class, new Jackson2JsonMessageParser());
    Message<?> resultMessage = mapper.toMessage(result);
    assertEquals(message.getPayload(), resultMessage.getPayload());
}
Also used : JsonInboundMessageMapper(org.springframework.integration.support.json.JsonInboundMessageMapper) GenericMessage(org.springframework.messaging.support.GenericMessage) Jackson2JsonMessageParser(org.springframework.integration.support.json.Jackson2JsonMessageParser) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 25 with RedisAvailable

use of org.springframework.integration.redis.rules.RedisAvailable in project spring-integration by spring-projects.

the class RedisStoreWritingMessageHandlerTests method testListWithListPayloadParsedAndNoKey.

@RedisAvailable
@Test(expected = MessageHandlingException.class)
public void testListWithListPayloadParsedAndNoKey() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deleteKey(jcf, "foo");
    String key = "foo";
    RedisList<String> redisList = new DefaultRedisList<String>(key, this.initTemplate(jcf, new RedisTemplate<String, String>()));
    assertEquals(0, redisList.size());
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    List<String> list = new ArrayList<String>();
    list.add("Manny");
    list.add("Moe");
    list.add("Jack");
    Message<List<String>> message = MessageBuilder.withPayload(list).build();
    handler.handleMessage(message);
    this.deleteKey(jcf, "foo");
}
Also used : StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) RedisTemplate(org.springframework.data.redis.core.RedisTemplate) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) List(java.util.List) RedisList(org.springframework.data.redis.support.collections.RedisList) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)99 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)99 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)62 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)28 GenericMessage (org.springframework.messaging.support.GenericMessage)24 BeanFactory (org.springframework.beans.factory.BeanFactory)23 RedisTemplate (org.springframework.data.redis.core.RedisTemplate)16 MessageGroup (org.springframework.integration.store.MessageGroup)15 ArrayList (java.util.ArrayList)14 Lock (java.util.concurrent.locks.Lock)13 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)13 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)13 Message (org.springframework.messaging.Message)13 QueueChannel (org.springframework.integration.channel.QueueChannel)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)9 JdkSerializationRedisSerializer (org.springframework.data.redis.serializer.JdkSerializationRedisSerializer)9 List (java.util.List)8 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)8 RedisList (org.springframework.data.redis.support.collections.RedisList)8