Search in sources :

Example 86 with RedisAvailable

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

the class RedisStoreWritingMessageHandlerTests method testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement.

@Test
@RedisAvailable
public void testZsetWithListPayloadParsedAndProvidedKeyScoreIncrement() {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.deleteKey(jcf, "foo");
    String key = "foo";
    RedisZSet<String> redisZset = new DefaultRedisZSet<String>(key, this.initTemplate(jcf, new StringRedisTemplate()));
    assertEquals(0, redisZset.size());
    RedisStoreWritingMessageHandler handler = new RedisStoreWritingMessageHandler(jcf);
    handler.setKey(key);
    handler.setCollectionType(CollectionType.ZSET);
    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).setHeader(RedisHeaders.ZSET_INCREMENT_SCORE, Boolean.TRUE).build();
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    Set<TypedTuple<String>> pepboys = redisZset.rangeByScoreWithScores(1, 1);
    for (TypedTuple<String> pepboy : pepboys) {
        assertTrue(pepboy.getScore() == 1);
    }
    handler.handleMessage(message);
    assertEquals(3, redisZset.size());
    pepboys = redisZset.rangeByScoreWithScores(1, 2);
    // should have incremented
    for (TypedTuple<String> pepboy : pepboys) {
        assertTrue(pepboy.getScore() == 2);
    }
    this.deleteKey(jcf, "foo");
}
Also used : DefaultRedisZSet(org.springframework.data.redis.support.collections.DefaultRedisZSet) ArrayList(java.util.ArrayList) StringRedisTemplate(org.springframework.data.redis.core.StringRedisTemplate) BeanFactory(org.springframework.beans.factory.BeanFactory) ArrayList(java.util.ArrayList) DefaultRedisList(org.springframework.data.redis.support.collections.DefaultRedisList) List(java.util.List) RedisList(org.springframework.data.redis.support.collections.RedisList) TypedTuple(org.springframework.data.redis.core.ZSetOperations.TypedTuple) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 87 with RedisAvailable

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

the class DelayerHandlerRescheduleIntegrationTests method testDelayerHandlerRescheduleWithRedisMessageStore.

@Test
@RedisAvailable
public void testDelayerHandlerRescheduleWithRedisMessageStore() throws Exception {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("DelayerHandlerRescheduleIntegrationTests-context.xml", this.getClass());
    MessageChannel input = context.getBean("input", MessageChannel.class);
    MessageGroupStore messageStore = context.getBean("messageStore", MessageGroupStore.class);
    String delayerMessageGroupId = DELAYER_ID + ".messageGroupId";
    messageStore.removeMessageGroup(delayerMessageGroupId);
    Message<String> message1 = MessageBuilder.withPayload("test1").build();
    input.send(message1);
    Thread.sleep(10);
    input.send(MessageBuilder.withPayload("test2").build());
    // Emulate restart and check DB state before next start
    // Interrupt taskScheduler as quickly as possible
    ThreadPoolTaskScheduler taskScheduler = (ThreadPoolTaskScheduler) IntegrationContextUtils.getTaskScheduler(context);
    taskScheduler.shutdown();
    taskScheduler.getScheduledExecutor().awaitTermination(10, TimeUnit.SECONDS);
    context.close();
    try {
        context.getBean("input", MessageChannel.class);
        fail("IllegalStateException expected");
    } catch (Exception e) {
        assertTrue(e instanceof IllegalStateException);
        assertTrue(e.getMessage().contains("BeanFactory not initialized or already closed - call 'refresh'"));
    }
    assertEquals(1, messageStore.getMessageGroupCount());
    assertEquals(delayerMessageGroupId, messageStore.iterator().next().getGroupId());
    assertEquals(2, messageStore.messageGroupSize(delayerMessageGroupId));
    assertEquals(2, messageStore.getMessageCountForAllMessageGroups());
    MessageGroup messageGroup = messageStore.getMessageGroup(delayerMessageGroupId);
    Message<?> messageInStore = messageGroup.getMessages().iterator().next();
    Object payload = messageInStore.getPayload();
    // INT-3049
    assertTrue(payload instanceof DelayHandler.DelayedMessageWrapper);
    assertEquals(message1, ((DelayHandler.DelayedMessageWrapper) payload).getOriginal());
    context.refresh();
    PollableChannel output = context.getBean("output", PollableChannel.class);
    Message<?> message = output.receive(20000);
    assertNotNull(message);
    Object payload1 = message.getPayload();
    message = output.receive(20000);
    assertNotNull(message);
    Object payload2 = message.getPayload();
    assertNotSame(payload1, payload2);
    assertEquals(1, messageStore.getMessageGroupCount());
    int n = 0;
    while (n++ < 200 && messageStore.messageGroupSize(delayerMessageGroupId) > 0) {
        Thread.sleep(100);
    }
    assertEquals(0, messageStore.messageGroupSize(delayerMessageGroupId));
    messageStore.removeMessageGroup(delayerMessageGroupId);
    context.close();
}
Also used : DelayHandler(org.springframework.integration.handler.DelayHandler) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageGroup(org.springframework.integration.store.MessageGroup) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) AbstractApplicationContext(org.springframework.context.support.AbstractApplicationContext) MessageChannel(org.springframework.messaging.MessageChannel) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) PollableChannel(org.springframework.messaging.PollableChannel) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 88 with RedisAvailable

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

the class RedisMessageGroupStoreTests method testRemoveMessageGroup.

@Test
@RedisAvailable
public void testRemoveMessageGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    Message<?> message = new GenericMessage<>("Hello");
    messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), message);
    assertEquals(1, messageGroup.size());
    store.removeMessageGroup(this.groupId);
    MessageGroup messageGroupA = store.getMessageGroup(this.groupId);
    assertNotSame(messageGroup, messageGroupA);
    // assertEquals(0, messageGroupA.getMarked().size());
    assertEquals(0, messageGroupA.getMessages().size());
    assertEquals(0, messageGroupA.size());
    // make sure the store is properly rebuild from Redis
    store = new RedisMessageStore(jcf);
    messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(0, messageGroup.getMessages().size());
    assertEquals(0, messageGroup.size());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 89 with RedisAvailable

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

the class RedisMessageGroupStoreTests method testConcurrentModifications.

@Test
@RedisAvailable
@Ignore
public void testConcurrentModifications() throws Exception {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    final RedisMessageStore store1 = new RedisMessageStore(jcf);
    final RedisMessageStore store2 = new RedisMessageStore(jcf);
    store1.removeMessageGroup(this.groupId);
    final Message<?> message = new GenericMessage<>("1");
    ExecutorService executor = null;
    final List<Object> failures = new ArrayList<>();
    for (int i = 0; i < 100; i++) {
        executor = Executors.newCachedThreadPool();
        executor.execute(() -> {
            MessageGroup group = store1.addMessageToGroup(this.groupId, message);
            if (group.getMessages().size() != 1) {
                failures.add("ADD");
                throw new AssertionFailedError("Failed on ADD");
            }
        });
        executor.execute(() -> {
            store2.removeMessagesFromGroup(this.groupId, message);
            MessageGroup group = store2.getMessageGroup(this.groupId);
            if (group.getMessages().size() != 0) {
                failures.add("REMOVE");
                throw new AssertionFailedError("Failed on Remove");
            }
        });
        executor.shutdown();
        executor.awaitTermination(10, TimeUnit.SECONDS);
        // ensures that if ADD thread executed after REMOVE, the store is empty for the next cycle
        store2.removeMessagesFromGroup(1, message);
    }
    assertTrue(failures.size() == 0);
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) AssertionFailedError(junit.framework.AssertionFailedError) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 90 with RedisAvailable

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

the class RedisMessageGroupStoreTests method testLastReleasedSequenceNumber.

@Test
@RedisAvailable
public void testLastReleasedSequenceNumber() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    Message<?> message = new GenericMessage<>("Hello");
    messageGroup = store.addMessageToGroup(messageGroup.getGroupId(), message);
    store.setLastReleasedSequenceNumberForGroup(messageGroup.getGroupId(), 5);
    messageGroup = store.getMessageGroup(this.groupId);
    assertEquals(5, messageGroup.getLastReleasedMessageSequenceNumber());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) MessageGroup(org.springframework.integration.store.MessageGroup) SimpleMessageGroup(org.springframework.integration.store.SimpleMessageGroup) 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