INT-3319 Fix BeanFactory Propagation
JIRA: https://jira.spring.io/browse/INT-3319 The `MessageBuilderFactory` abstraction relies on the bean factory being propagated to all classes that create messages. Some classes were missed in the initial PR.
This commit is contained in:
committed by
Artem Bilan
parent
1c9bcaccee
commit
75af72a77c
@@ -18,6 +18,7 @@ package org.springframework.integration.redis.channel;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.context.SmartLifecycle;
|
||||
import org.springframework.core.task.SimpleAsyncTaskExecutor;
|
||||
@@ -33,7 +34,6 @@ import org.springframework.integration.MessageDispatchingException;
|
||||
import org.springframework.integration.channel.AbstractMessageChannel;
|
||||
import org.springframework.integration.channel.MessagePublishingErrorHandler;
|
||||
import org.springframework.integration.context.IntegrationProperties;
|
||||
import org.springframework.integration.dispatcher.AbstractDispatcher;
|
||||
import org.springframework.integration.dispatcher.BroadcastingDispatcher;
|
||||
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
|
||||
import org.springframework.integration.support.converter.SimpleMessageConverter;
|
||||
@@ -60,7 +60,7 @@ public class SubscribableRedisChannel extends AbstractMessageChannel implements
|
||||
private final RedisTemplate redisTemplate;
|
||||
private final String topicName;
|
||||
|
||||
private final AbstractDispatcher dispatcher = new BroadcastingDispatcher(true);
|
||||
private final BroadcastingDispatcher dispatcher = new BroadcastingDispatcher(true);
|
||||
|
||||
private volatile Integer maxSubscribers;
|
||||
|
||||
@@ -84,6 +84,7 @@ public class SubscribableRedisChannel extends AbstractMessageChannel implements
|
||||
this.taskExecutor = taskExecutor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setMessageConverter(MessageConverter messageConverter) {
|
||||
Assert.notNull(messageConverter, "'messageConverter' must not be null");
|
||||
this.messageConverter = messageConverter;
|
||||
@@ -135,6 +136,9 @@ public class SubscribableRedisChannel extends AbstractMessageChannel implements
|
||||
if (this.messageConverter == null){
|
||||
this.messageConverter = new SimpleMessageConverter();
|
||||
}
|
||||
if (this.messageConverter instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
|
||||
}
|
||||
this.container.setConnectionFactory(connectionFactory);
|
||||
if (!(this.taskExecutor instanceof ErrorHandlingTaskExecutor)) {
|
||||
ErrorHandler errorHandler = new MessagePublishingErrorHandler(
|
||||
@@ -147,6 +151,7 @@ public class SubscribableRedisChannel extends AbstractMessageChannel implements
|
||||
adapter.afterPropertiesSet();
|
||||
this.container.addMessageListener(adapter, new ChannelTopic(topicName));
|
||||
this.container.afterPropertiesSet();
|
||||
this.dispatcher.setBeanFactory(this.getBeanFactory());
|
||||
this.initialized = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2007-2013 the original author or authors
|
||||
* Copyright 2007-2014 the original author or authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -19,6 +19,7 @@ package org.springframework.integration.redis.inbound;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.ChannelTopic;
|
||||
import org.springframework.data.redis.listener.PatternTopic;
|
||||
@@ -95,6 +96,9 @@ public class RedisInboundChannelAdapter extends MessageProducerSupport {
|
||||
}
|
||||
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);
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
|
||||
package org.springframework.integration.redis.outbound;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactoryAware;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
@@ -94,6 +95,9 @@ public class RedisPublishingMessageHandler extends AbstractMessageHandler implem
|
||||
@Override
|
||||
protected void onInit() throws Exception {
|
||||
Assert.notNull(topicExpression, "'topicExpression' must not be null.");
|
||||
if (this.messageConverter instanceof BeanFactoryAware) {
|
||||
((BeanFactoryAware) this.messageConverter).setBeanFactory(this.getBeanFactory());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -17,6 +17,7 @@
|
||||
package org.springframework.integration.redis.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
@@ -28,14 +29,15 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.data.redis.serializer.RedisSerializer;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.redis.rules.RedisAvailable;
|
||||
import org.springframework.integration.redis.rules.RedisAvailableTests;
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
import org.springframework.messaging.Message;
|
||||
import org.springframework.messaging.MessageHandler;
|
||||
import org.springframework.messaging.MessagingException;
|
||||
import org.springframework.messaging.SubscribableChannel;
|
||||
import org.springframework.messaging.support.GenericMessage;
|
||||
|
||||
/**
|
||||
* @author Oleg Zhurakousky
|
||||
@@ -59,9 +61,11 @@ public class RedisChannelParserTests extends RedisAvailableTests{
|
||||
assertEquals(Integer.MAX_VALUE, TestUtils.getPropertyValue(
|
||||
TestUtils.getPropertyValue(redisChannel, "dispatcher"), "maxSubscribers", Integer.class).intValue());
|
||||
redisChannel = context.getBean("redisChannelWithSubLimit", SubscribableChannel.class);
|
||||
assertEquals(1, TestUtils.getPropertyValue(
|
||||
TestUtils.getPropertyValue(redisChannel, "dispatcher"), "maxSubscribers", Integer.class).intValue());
|
||||
context.stop();
|
||||
assertEquals(1, TestUtils.getPropertyValue(redisChannel, "dispatcher.maxSubscribers", Integer.class).intValue());
|
||||
Object mbf = context.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME);
|
||||
assertSame(mbf, TestUtils.getPropertyValue(redisChannel, "dispatcher.messageBuilderFactory"));
|
||||
assertSame(mbf, TestUtils.getPropertyValue(redisChannel, "messageBuilderFactory"));
|
||||
context.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -76,6 +80,7 @@ public class RedisChannelParserTests extends RedisAvailableTests{
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
redisChannel.subscribe(new MessageHandler() {
|
||||
@Override
|
||||
public void handleMessage(Message<?> message) throws MessagingException {
|
||||
assertEquals(m.getPayload(), message.getPayload());
|
||||
latch.countDown();
|
||||
@@ -84,7 +89,7 @@ public class RedisChannelParserTests extends RedisAvailableTests{
|
||||
redisChannel.send(m);
|
||||
|
||||
assertTrue(latch.await(5, TimeUnit.SECONDS));
|
||||
context.stop();
|
||||
context.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -33,6 +33,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.connection.RedisConnectionFactory;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.redis.inbound.RedisInboundChannelAdapter;
|
||||
import org.springframework.integration.redis.rules.RedisAvailable;
|
||||
import org.springframework.integration.redis.rules.RedisAvailableTests;
|
||||
@@ -77,6 +78,8 @@ public class RedisInboundChannelAdapterParserTests extends RedisAvailableTests {
|
||||
Object bean = context.getBean("withoutSerializer.adapter");
|
||||
assertNotNull(bean);
|
||||
assertNull(TestUtils.getPropertyValue(bean, "serializer"));
|
||||
Object mbf = context.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME);
|
||||
assertSame(mbf, TestUtils.getPropertyValue(bean, "messageConverter.messageBuilderFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.integration.redis.config;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -28,6 +29,7 @@ import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
|
||||
import org.springframework.expression.Expression;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
import org.springframework.integration.context.IntegrationContextUtils;
|
||||
import org.springframework.integration.endpoint.EventDrivenConsumer;
|
||||
import org.springframework.integration.redis.inbound.RedisInboundChannelAdapter;
|
||||
import org.springframework.integration.redis.outbound.RedisPublishingMessageHandler;
|
||||
@@ -73,6 +75,8 @@ public class RedisOutboundChannelAdapterParserTests extends RedisAvailableTests
|
||||
Object converterBean = context.getBean("testConverter");
|
||||
assertEquals(converterBean, accessor.getPropertyValue("messageConverter"));
|
||||
assertEquals(context.getBean("serializer"), accessor.getPropertyValue("serializer"));
|
||||
Object mbf = context.getBean(IntegrationContextUtils.INTEGRATION_MESSAGE_BUILDER_FACTORY_BEAN_NAME);
|
||||
assertSame(mbf, TestUtils.getPropertyValue(handler, "messageConverter.messageBuilderFactory"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -22,6 +22,7 @@ import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.parsing.BeanDefinitionParsingException;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
@@ -70,7 +71,7 @@ public class RedisStoreInboundChannelAdapterParserTests {
|
||||
|
||||
@Test(expected=BeanDefinitionParsingException.class)
|
||||
public void testTemplateAndCfMutualExclusivity(){
|
||||
new ClassPathXmlApplicationContext("inbound-template-cf-fail.xml", this.getClass());
|
||||
new ClassPathXmlApplicationContext("inbound-template-cf-fail.xml", this.getClass()).close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user