Search in sources :

Example 36 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisMessageGroupStoreTests method testRemoveNonExistingMessageFromTheGroup.

@Test
@RedisAvailable
public void testRemoveNonExistingMessageFromTheGroup() {
    RedisConnectionFactory jcf = getConnectionFactoryForTest();
    RedisMessageStore store = new RedisMessageStore(jcf);
    MessageGroup messageGroup = store.getMessageGroup(this.groupId);
    store.addMessagesToGroup(messageGroup.getGroupId(), new GenericMessage<>("1"));
    store.removeMessagesFromGroup(this.groupId, new GenericMessage<>("2"));
}
Also used : 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 37 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisLockRegistryTests method testEquals.

@Test
@RedisAvailable
public void testEquals() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    RedisLockRegistry registry1 = new RedisLockRegistry(connectionFactory, this.registryKey);
    RedisLockRegistry registry2 = new RedisLockRegistry(connectionFactory, this.registryKey);
    RedisLockRegistry registry3 = new RedisLockRegistry(connectionFactory, this.registryKey2);
    Lock lock1 = registry1.obtain("foo");
    Lock lock2 = registry1.obtain("foo");
    assertEquals(lock1, lock2);
    lock1.lock();
    lock2.lock();
    assertEquals(lock1, lock2);
    lock1.unlock();
    lock2.unlock();
    assertEquals(lock1, lock2);
    lock1 = registry1.obtain("foo");
    lock2 = registry2.obtain("foo");
    assertNotEquals(lock1, lock2);
    lock1.lock();
    assertFalse(lock2.tryLock());
    lock1.unlock();
    lock1 = registry1.obtain("foo");
    lock2 = registry3.obtain("foo");
    assertNotEquals(lock1, lock2);
    lock1.lock();
    lock2.lock();
    lock1.unlock();
    lock2.unlock();
}
Also used : RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) Lock(java.util.concurrent.locks.Lock) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 38 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class SubscribableRedisChannelTests method dispatcherHasNoSubscribersTest.

@Test
@RedisAvailable
public void dispatcherHasNoSubscribersTest() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    SubscribableRedisChannel channel = new SubscribableRedisChannel(connectionFactory, "si.test.channel.no.subs");
    channel.setBeanName("dhnsChannel");
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    RedisMessageListenerContainer container = TestUtils.getPropertyValue(channel, "container", RedisMessageListenerContainer.class);
    @SuppressWarnings("unchecked") Map<?, Set<MessageListenerAdapter>> channelMapping = (Map<?, Set<MessageListenerAdapter>>) TestUtils.getPropertyValue(container, "channelMapping");
    MessageListenerAdapter listener = channelMapping.entrySet().iterator().next().getValue().iterator().next();
    Object delegate = TestUtils.getPropertyValue(listener, "delegate");
    try {
        ReflectionUtils.findMethod(delegate.getClass(), "handleMessage", Object.class).invoke(delegate, "Hello, world!");
        fail("Exception expected");
    } catch (InvocationTargetException e) {
        Throwable cause = e.getCause();
        assertNotNull(cause);
        assertThat(cause.getMessage(), containsString("Dispatcher has no subscribers for redis-channel 'si.test.channel.no.subs' (dhnsChannel)."));
    }
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) Set(java.util.Set) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Map(java.util.Map) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) InvocationTargetException(java.lang.reflect.InvocationTargetException) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 39 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class SubscribableRedisChannelTests method pubSubChannelTest.

@Test
@RedisAvailable
public void pubSubChannelTest() throws Exception {
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    SubscribableRedisChannel channel = new SubscribableRedisChannel(connectionFactory, "si.test.channel");
    channel.setBeanFactory(mock(BeanFactory.class));
    channel.afterPropertiesSet();
    channel.start();
    this.awaitContainerSubscribed(TestUtils.getPropertyValue(channel, "container", RedisMessageListenerContainer.class));
    final CountDownLatch latch = new CountDownLatch(3);
    MessageHandler handler = message -> latch.countDown();
    channel.subscribe(handler);
    channel.send(new GenericMessage<String>("1"));
    channel.send(new GenericMessage<String>("2"));
    channel.send(new GenericMessage<String>("3"));
    assertTrue(latch.await(20, TimeUnit.SECONDS));
}
Also used : Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Set(java.util.Set) Test(org.junit.Test) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) TestUtils(org.springframework.integration.test.util.TestUtils) InvocationTargetException(java.lang.reflect.InvocationTargetException) Assert.assertThat(org.junit.Assert.assertThat) TimeUnit(java.util.concurrent.TimeUnit) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) MessageHandler(org.springframework.messaging.MessageHandler) RedisAvailableTests(org.springframework.integration.redis.rules.RedisAvailableTests) ReflectionUtils(org.springframework.util.ReflectionUtils) Map(java.util.Map) BeanFactory(org.springframework.beans.factory.BeanFactory) Assert.fail(org.junit.Assert.fail) GenericMessage(org.springframework.messaging.support.GenericMessage) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MessageHandler(org.springframework.messaging.MessageHandler) BeanFactory(org.springframework.beans.factory.BeanFactory) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 40 with RedisConnectionFactory

use of org.springframework.data.redis.connection.RedisConnectionFactory in project spring-integration by spring-projects.

the class RedisInboundChannelAdapterParserTests method testInboundChannelAdapterMessaging.

@Test
@RedisAvailable
public void testInboundChannelAdapterMessaging() throws Exception {
    RedisInboundChannelAdapter adapter = context.getBean("adapter", RedisInboundChannelAdapter.class);
    this.awaitContainerSubscribedWithPatterns(TestUtils.getPropertyValue(adapter, "container", RedisMessageListenerContainer.class));
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    connectionFactory.getConnection().publish("foo".getBytes(), "Hello Redis from foo".getBytes());
    connectionFactory.getConnection().publish("bar".getBytes(), "Hello Redis from bar".getBytes());
    QueueChannel receiveChannel = context.getBean("receiveChannel", QueueChannel.class);
    for (int i = 0; i < 3; i++) {
        Message<?> receive = receiveChannel.receive(10000);
        assertNotNull(receive);
        assertThat(receive.getPayload(), Matchers.<Object>isOneOf("Hello Redis from foo", "Hello Redis from bar"));
    }
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedisInboundChannelAdapter(org.springframework.integration.redis.inbound.RedisInboundChannelAdapter) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Aggregations

RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)100 Test (org.junit.Test)83 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)62 GenericMessage (org.springframework.messaging.support.GenericMessage)22 BeanFactory (org.springframework.beans.factory.BeanFactory)19 StringRedisTemplate (org.springframework.data.redis.core.StringRedisTemplate)17 MessageGroup (org.springframework.integration.store.MessageGroup)14 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)13 ArrayList (java.util.ArrayList)12 Properties (java.util.Properties)10 Message (org.springframework.messaging.Message)10 ApplicationContext (org.springframework.context.ApplicationContext)9 List (java.util.List)8 DefaultRedisList (org.springframework.data.redis.support.collections.DefaultRedisList)8 RedisList (org.springframework.data.redis.support.collections.RedisList)8 RedisConnection (org.springframework.data.redis.connection.RedisConnection)7 DefaultRedisZSet (org.springframework.data.redis.support.collections.DefaultRedisZSet)7 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)6 TypedTuple (org.springframework.data.redis.core.ZSetOperations.TypedTuple)6 LiteralExpression (org.springframework.expression.common.LiteralExpression)6