Search in sources :

Example 1 with ChannelTopic

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

the class RedissonSubscribeTest method testMultipleSubscribers.

@Test
public void testMultipleSubscribers() {
    RedissonConnectionFactory factory = new RedissonConnectionFactory(redisson);
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(factory);
    AtomicInteger counterTest = new AtomicInteger();
    AtomicInteger counterTest2 = new AtomicInteger();
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new ChannelTopic("test"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest.incrementAndGet();
        }
    }, new ChannelTopic("test"));
    container.addMessageListener(new MessageListener() {

        @Override
        public void onMessage(Message message, byte[] pattern) {
            counterTest2.incrementAndGet();
        }
    }, new ChannelTopic("test2"));
    container.afterPropertiesSet();
    container.start();
    Assertions.assertThat(container.isRunning()).isTrue();
    RedisConnection c = factory.getConnection();
    c.publish("test".getBytes(), "sdfdsf".getBytes());
    Awaitility.await().atMost(Duration.FIVE_SECONDS).until(() -> {
        return counterTest.get() == 2;
    });
    Assertions.assertThat(counterTest2.get()).isZero();
    container.stop();
}
Also used : Message(org.springframework.data.redis.connection.Message) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageListener(org.springframework.data.redis.connection.MessageListener) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) RedisConnection(org.springframework.data.redis.connection.RedisConnection) Test(org.junit.Test)

Example 2 with ChannelTopic

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

the class RedisConsumerTest method registerConsumerForTwoChannelTopics.

@Test
public void registerConsumerForTwoChannelTopics() throws Exception {
    ArgumentCaptor<Collection> collectionCaptor = ArgumentCaptor.forClass(Collection.class);
    verify(listenerContainer).addMessageListener(any(MessageListener.class), collectionCaptor.capture());
    Collection<ChannelTopic> topics = collectionCaptor.getValue();
    Iterator<ChannelTopic> topicIterator = topics.iterator();
    Topic firstTopic = topicIterator.next();
    Topic twoTopic = topicIterator.next();
    assertEquals("one", firstTopic.getTopic());
    assertEquals("two", twoTopic.getTopic());
}
Also used : ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) MessageListener(org.springframework.data.redis.connection.MessageListener) Collection(java.util.Collection) Topic(org.springframework.data.redis.listener.Topic) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) Test(org.junit.Test)

Example 3 with ChannelTopic

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

the class RedisConsumer method toTopics.

private Collection<Topic> toTopics(String channels) {
    String[] channelsArrays = channels.split(",");
    List<Topic> topics = new ArrayList<>();
    for (String channel : channelsArrays) {
        String name = channel.trim();
        if (Command.PSUBSCRIBE.equals(redisConfiguration.getCommand())) {
            topics.add(new PatternTopic(name));
        } else if (Command.SUBSCRIBE.equals(redisConfiguration.getCommand())) {
            topics.add(new ChannelTopic(name));
        } else {
            throw new IllegalArgumentException("Unsupported Command " + redisConfiguration.getCommand());
        }
    }
    return topics;
}
Also used : ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) PatternTopic(org.springframework.data.redis.listener.PatternTopic) ArrayList(java.util.ArrayList) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) Topic(org.springframework.data.redis.listener.Topic) PatternTopic(org.springframework.data.redis.listener.PatternTopic)

Example 4 with ChannelTopic

use of org.springframework.data.redis.listener.ChannelTopic in project spring-integration by spring-projects.

the class RedisPublishingMessageHandlerTests method testRedisPublishingMessageHandler.

@Test
@RedisAvailable
public void testRedisPublishingMessageHandler() throws Exception {
    int numToTest = 10;
    String topic = "si.test.channel";
    final CountDownLatch latch = new CountDownLatch(numToTest * 2);
    RedisConnectionFactory connectionFactory = this.getConnectionFactoryForTest();
    MessageListenerAdapter listener = new MessageListenerAdapter();
    listener.setDelegate(new Listener(latch));
    listener.setSerializer(new StringRedisSerializer());
    listener.afterPropertiesSet();
    RedisMessageListenerContainer container = new RedisMessageListenerContainer();
    container.setConnectionFactory(connectionFactory);
    container.afterPropertiesSet();
    container.addMessageListener(listener, Collections.<Topic>singletonList(new ChannelTopic(topic)));
    container.start();
    this.awaitContainerSubscribed(container);
    final RedisPublishingMessageHandler handler = new RedisPublishingMessageHandler(connectionFactory);
    handler.setTopicExpression(new LiteralExpression(topic));
    for (int i = 0; i < numToTest; i++) {
        handler.handleMessage(MessageBuilder.withPayload("test-" + i).build());
    }
    for (int i = 0; i < numToTest; i++) {
        handler.handleMessage(MessageBuilder.withPayload(("test-" + i).getBytes()).build());
    }
    assertTrue(latch.await(10, TimeUnit.SECONDS));
    container.stop();
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) StringRedisSerializer(org.springframework.data.redis.serializer.StringRedisSerializer) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) LiteralExpression(org.springframework.expression.common.LiteralExpression) RedisMessageListenerContainer(org.springframework.data.redis.listener.RedisMessageListenerContainer) CountDownLatch(java.util.concurrent.CountDownLatch) RedisConnectionFactory(org.springframework.data.redis.connection.RedisConnectionFactory) RedisAvailable(org.springframework.integration.redis.rules.RedisAvailable) Test(org.junit.Test)

Example 5 with ChannelTopic

use of org.springframework.data.redis.listener.ChannelTopic in project spring-integration by spring-projects.

the class RedisInboundChannelAdapter method onInit.

@Override
protected void onInit() {
    super.onInit();
    boolean hasTopics = false;
    if (this.topics != null) {
        Assert.noNullElements(this.topics, "'topics' may not contain null elements.");
        hasTopics = true;
    }
    boolean hasPatterns = false;
    if (this.topicPatterns != null) {
        Assert.noNullElements(this.topicPatterns, "'topicPatterns' may not contain null elements.");
        hasPatterns = true;
    }
    Assert.state(hasTopics || hasPatterns, "at least one topic or topic pattern is required for subscription.");
    if (this.messageConverter instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
    }
    MessageListenerDelegate delegate = new MessageListenerDelegate();
    MessageListenerAdapter adapter = new MessageListenerAdapter(delegate);
    adapter.setSerializer(this.serializer);
    List<Topic> topicList = new ArrayList<Topic>();
    if (hasTopics) {
        for (String topic : this.topics) {
            topicList.add(new ChannelTopic(topic));
        }
    }
    if (hasPatterns) {
        for (String pattern : this.topicPatterns) {
            topicList.add(new PatternTopic(pattern));
        }
    }
    adapter.afterPropertiesSet();
    this.container.addMessageListener(adapter, topicList);
    this.container.afterPropertiesSet();
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) PatternTopic(org.springframework.data.redis.listener.PatternTopic) ArrayList(java.util.ArrayList) Topic(org.springframework.data.redis.listener.Topic) PatternTopic(org.springframework.data.redis.listener.PatternTopic) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic)

Aggregations

ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)6 Test (org.junit.Test)3 Topic (org.springframework.data.redis.listener.Topic)3 MessageListenerAdapter (org.springframework.data.redis.listener.adapter.MessageListenerAdapter)3 ArrayList (java.util.ArrayList)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2 MessageListener (org.springframework.data.redis.connection.MessageListener)2 PatternTopic (org.springframework.data.redis.listener.PatternTopic)2 RedisMessageListenerContainer (org.springframework.data.redis.listener.RedisMessageListenerContainer)2 Collection (java.util.Collection)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Message (org.springframework.data.redis.connection.Message)1 RedisConnection (org.springframework.data.redis.connection.RedisConnection)1 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 MessagePublishingErrorHandler (org.springframework.integration.channel.MessagePublishingErrorHandler)1 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)1 BeanFactoryChannelResolver (org.springframework.integration.support.channel.BeanFactoryChannelResolver)1