Search in sources :

Example 1 with RedisMessageListenerContainer

use of org.springframework.data.redis.listener.RedisMessageListenerContainer in project camel by apache.

the class RedisConfiguration method createDefaultListenerContainer.

private RedisMessageListenerContainer createDefaultListenerContainer() {
    listenerContainer = new RedisMessageListenerContainer();
    managedListenerContainer = true;
    listenerContainer.setConnectionFactory(getConnectionFactory());
    listenerContainer.afterPropertiesSet();
    listenerContainer.start();
    return listenerContainer;
}
Also used : RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer)

Example 2 with RedisMessageListenerContainer

use of org.springframework.data.redis.listener.RedisMessageListenerContainer in project rdbcache by rdbcache.

the class RedisConfig method container.

@Bean
RedisMessageListenerContainer container() {
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(redisConnectionFactory());
    container.addMessageListener(listenerAdapter(), new PatternTopic("__key*__:expired"));
    return container;
}
Also used : PatternTopic(org.springframework.data.redis.listener.PatternTopic) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Bean(org.springframework.context.annotation.Bean)

Example 3 with RedisMessageListenerContainer

use of org.springframework.data.redis.listener.RedisMessageListenerContainer in project tutorials by eugenp.

the class RedisConfig method redisContainer.

@Bean
RedisMessageListenerContainer redisContainer() {
    final RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(jedisConnectionFactory());
    container.addMessageListener(messageListener(), topic());
    return container;
}
Also used : RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) Bean(org.springframework.context.annotation.Bean)

Example 4 with RedisMessageListenerContainer

use of org.springframework.data.redis.listener.RedisMessageListenerContainer 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 5 with RedisMessageListenerContainer

use of org.springframework.data.redis.listener.RedisMessageListenerContainer in project redisson by redisson.

the class RedissonSubscribeTest method testPatterTopic.

@Test
public void testPatterTopic() throws IOException, InterruptedException {
    RedisRunner.RedisProcess instance = new RedisRunner().nosave().randomPort().randomDir().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.K, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g, RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.$).run();
    Config config = new Config();
    config.useSingleServer().setAddress(instance.getRedisServerAddressAndPort()).setPingConnectionInterval(0);
    RedissonClient redisson = Redisson.create(config);
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(factory);
    AtomicInteger counterTest = new AtomicInteger();
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new PatternTopic("__keyspace@0__:mykey"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new PatternTopic("__keyevent@0__:del"));
    container.afterPropertiesSet();
    container.start();
    Assertions.assertThat(container.isRunning()).isTrue();
    RedisConnection c = factory.getConnection();
    c.set("mykey".getBytes(), "2".getBytes());
    c.del("mykey".getBytes());
    Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
        return counterTest.get() == 3;
    });
    container.stop();
    redisson.shutdown();
}
Also used : Message(org.springframework.data.redis.connection.Message) PatternTopic(org.springframework.data.redis.listener.PatternTopic) Config(org.redisson.config.Config) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RedisRunner(org.redisson.RedisRunner) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Aggregations

RedisMessageListenerContainer (org.springframework.data.redis.listener.RedisMessageListenerContainer)10 Bean (org.springframework.context.annotation.Bean)5 Test (org.junit.Test)4 PatternTopic (org.springframework.data.redis.listener.PatternTopic)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Message (org.springframework.data.redis.connection.Message)2 MessageListener (org.springframework.data.redis.connection.MessageListener)2 RedisConnection (org.springframework.data.redis.connection.RedisConnection)2 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)2 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)2 MessageListenerAdapter (org.springframework.data.redis.listener.adapter.MessageListenerAdapter)2 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 RedisRunner (org.redisson.RedisRunner)1 RedissonClient (org.redisson.api.RedissonClient)1 Config (org.redisson.config.Config)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1