Search in sources :

Example 1 with RedisZSet

use of org.springframework.data.redis.support.collections.RedisZSet in project spring-integration by spring-projects.

the class RedisStoreInboundChannelAdapterIntegrationTests method testZsetInboundAdapter.

@Test
@RedisAvailable
@SuppressWarnings("unchecked")
public void testZsetInboundAdapter() throws InterruptedException {
    RedisConnectionFactory jcf = this.getConnectionFactoryForTest();
    this.prepareZset(jcf);
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("zset-inbound-adapter.xml", this.getClass());
    // No Score test
    SourcePollingChannelAdapter zsetAdapterNoScore = context.getBean("zsetAdapterNoScore", SourcePollingChannelAdapter.class);
    zsetAdapterNoScore.start();
    QueueChannel redisChannel = context.getBean("redisChannel", QueueChannel.class);
    Message<RedisZSet<Object>> message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(13, message.getPayload().size());
    // poll again, should get the same stuff
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(13, message.getPayload().size());
    zsetAdapterNoScore.stop();
    // ScoreRange test
    SourcePollingChannelAdapter zsetAdapterWithScoreRange = context.getBean("zsetAdapterWithScoreRange", SourcePollingChannelAdapter.class);
    zsetAdapterWithScoreRange.start();
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(11, message.getPayload().rangeByScore(18, 20).size());
    // poll again, should get the same stuff
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(11, message.getPayload().rangeByScore(18, 20).size());
    zsetAdapterWithScoreRange.stop();
    // SingleScore test
    SourcePollingChannelAdapter zsetAdapterWithSingleScore = context.getBean("zsetAdapterWithSingleScore", SourcePollingChannelAdapter.class);
    zsetAdapterWithSingleScore.start();
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(2, message.getPayload().rangeByScore(18, 18).size());
    // poll again, should get the same stuff
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(2, message.getPayload().rangeByScore(18, 18).size());
    zsetAdapterWithSingleScore.stop();
    // SingleScoreAndSynchronization test
    SourcePollingChannelAdapter zsetAdapterWithSingleScoreAndSynchronization = context.getBean("zsetAdapterWithSingleScoreAndSynchronization", SourcePollingChannelAdapter.class);
    QueueChannel otherRedisChannel = context.getBean("otherRedisChannel", QueueChannel.class);
    // get all 13 presidents
    zsetAdapterNoScore.start();
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    assertEquals(13, message.getPayload().size());
    zsetAdapterNoScore.stop();
    // get only presidents for 18th century
    zsetAdapterWithSingleScoreAndSynchronization.start();
    Message<Integer> sizeMessage = (Message<Integer>) otherRedisChannel.receive(10000);
    assertNotNull(sizeMessage);
    assertEquals(Integer.valueOf(2), sizeMessage.getPayload());
    // ... however other elements are still available 13-2=11
    zsetAdapterNoScore.start();
    message = (Message<RedisZSet<Object>>) redisChannel.receive(10000);
    assertNotNull(message);
    int n = 0;
    while (n++ < 100 && message.getPayload().size() != 11) {
        Thread.sleep(100);
    }
    assertTrue(n < 100);
    zsetAdapterNoScore.stop();
    zsetAdapterWithSingleScoreAndSynchronization.stop();
    this.deletePresidents(jcf);
    context.close();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SourcePollingChannelAdapter(org.springframework.integration.endpoint.SourcePollingChannelAdapter) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisZSet(org.springframework.data.redis.support.collections.RedisZSet) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)1 RedisZSet (org.springframework.data.redis.support.collections.RedisZSet)1 QueueChannel (org.springframework.integration.channel.QueueChannel)1 SourcePollingChannelAdapter (org.springframework.integration.endpoint.SourcePollingChannelAdapter)1 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)1 Message (org.springframework.messaging.Message)1