Search in sources :

Example 1 with MessageListenerAdapter

use of org.springframework.data.redis.listener.adapter.MessageListenerAdapter 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 2 with MessageListenerAdapter

use of org.springframework.data.redis.listener.adapter.MessageListenerAdapter 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 3 with MessageListenerAdapter

use of org.springframework.data.redis.listener.adapter.MessageListenerAdapter 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)

Example 4 with MessageListenerAdapter

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

the class SubscribableRedisChannel method onInit.

@Override
public void onInit() throws Exception {
    if (this.initialized) {
        return;
    }
    super.onInit();
    if (this.maxSubscribers == null) {
        Integer maxSubscribers = getIntegrationProperty(IntegrationProperties.CHANNELS_MAX_BROADCAST_SUBSCRIBERS, Integer.class);
        this.setMaxSubscribers(maxSubscribers);
    }
    if (this.messageConverter == null) {
        this.messageConverter = new SimpleMessageConverter();
    }
    if (this.messageConverter instanceof BeanFactoryAware) {
        ((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
    }
    this.container.setConnectionFactory(this.connectionFactory);
    if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
        ErrorHandler errorHandler = new MessagePublishingErrorHandler(new BeanFactoryChannelResolver(this.getBeanFactory()));
        this.taskExecutor = new ErrorHandlingTaskExecutor(this.taskExecutor, errorHandler);
    }
    this.container.setTaskExecutor(this.taskExecutor);
    MessageListenerAdapter adapter = new MessageListenerAdapter(new MessageListenerDelegate());
    adapter.setSerializer(this.serializer);
    adapter.afterPropertiesSet();
    this.container.addMessageListener(adapter, new ChannelTopic(this.topicName));
    this.container.afterPropertiesSet();
    this.dispatcher.setBeanFactory(this.getBeanFactory());
    this.initialized = true;
}
Also used : MessageListenerAdapter(org.springframework.data.redis.listener.adapter.MessageListenerAdapter) BeanFactoryAware(org.springframework.beans.factory.BeanFactoryAware) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) ErrorHandler(org.springframework.util.ErrorHandler) MessagePublishingErrorHandler(org.springframework.integration.channel.MessagePublishingErrorHandler) ChannelTopic(org.springframework.data.redis.listener.ChannelTopic) BeanFactoryChannelResolver(org.springframework.integration.support.channel.BeanFactoryChannelResolver) SimpleMessageConverter(org.springframework.integration.support.converter.SimpleMessageConverter) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor)

Aggregations

MessageListenerAdapter (org.springframework.data.redis.listener.adapter.MessageListenerAdapter)4 ChannelTopic (org.springframework.data.redis.listener.ChannelTopic)3 Test (org.junit.Test)2 BeanFactoryAware (org.springframework.beans.factory.BeanFactoryAware)2 RedisConnectionFactory (org.springframework.data.redis.connection.RedisConnectionFactory)2 RedisMessageListenerContainer (org.springframework.data.redis.listener.RedisMessageListenerContainer)2 RedisAvailable (org.springframework.integration.redis.rules.RedisAvailable)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 PatternTopic (org.springframework.data.redis.listener.PatternTopic)1 Topic (org.springframework.data.redis.listener.Topic)1 StringRedisSerializer (org.springframework.data.redis.serializer.StringRedisSerializer)1 LiteralExpression (org.springframework.expression.common.LiteralExpression)1 MessagePublishingErrorHandler (org.springframework.integration.channel.MessagePublishingErrorHandler)1 BeanFactoryChannelResolver (org.springframework.integration.support.channel.BeanFactoryChannelResolver)1 SimpleMessageConverter (org.springframework.integration.support.converter.SimpleMessageConverter)1