Added the MessageBusAware interface, as well as the MessageBusAwareBeanPostProcessor for injecting the MessageBus in the implementing beans.
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.bus;
|
||||
|
||||
/**
|
||||
* Interface to be implemented by classes which need access to the {@link MessageBus}.
|
||||
* @author Marius Bogoevici
|
||||
*
|
||||
*/
|
||||
public interface MessageBusAware {
|
||||
|
||||
public void setMessageBus(MessageBus messageBus);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2002-2008 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.integration.bus;
|
||||
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A bean post processor which injects all {@link MessageBusAware} beans with a
|
||||
* reference to the {@link MessageBus}.
|
||||
*
|
||||
* @author Marius Bogoevici
|
||||
*
|
||||
*/
|
||||
public class MessageBusAwareBeanPostProcessor implements BeanPostProcessor {
|
||||
|
||||
private final MessageBus messageBus;
|
||||
|
||||
public MessageBusAwareBeanPostProcessor(MessageBus messageBus) {
|
||||
Assert.notNull(messageBus, "'messageBus' must not be null");
|
||||
this.messageBus = messageBus;
|
||||
}
|
||||
|
||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||
if (bean instanceof MessageBusAware) {
|
||||
((MessageBusAware) bean).setMessageBus(messageBus);
|
||||
}
|
||||
return bean;
|
||||
}
|
||||
|
||||
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
|
||||
return bean;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -25,10 +25,8 @@ import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import org.springframework.integration.ConfigurationException;
|
||||
import org.springframework.integration.channel.DispatcherPolicy;
|
||||
import org.springframework.integration.channel.MessageChannel;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
@@ -219,17 +217,11 @@ public class MessageBusTests {
|
||||
assertEquals(MessageDeliveryException.class, errorMessage.getPayload().getClass());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test(expected = BeanCreationException.class)
|
||||
public void testMultipleMessageBusBeans() {
|
||||
boolean exceptionThrown = false;
|
||||
try {
|
||||
new ClassPathXmlApplicationContext("multipleMessageBusBeans.xml", this.getClass());
|
||||
}
|
||||
catch (BeanCreationException e) {
|
||||
exceptionThrown = true;
|
||||
assertEquals(ConfigurationException.class, e.getCause().getClass());
|
||||
}
|
||||
assertTrue(exceptionThrown);
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("multipleMessageBusBeans.xml",
|
||||
this.getClass());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -259,6 +251,12 @@ public class MessageBusTests {
|
||||
assertEquals("handler should have received error message", 0, latch.getCount());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMessageBusAwareImpl() {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("messageBusTests.xml", this.getClass());
|
||||
TestMessageBusAwareImpl messageBusAwareBean = (TestMessageBusAwareImpl) context.getBean("messageBusAwareBean");
|
||||
assertTrue(messageBusAwareBean.getMessageBus() == context.getBean("bus"));
|
||||
}
|
||||
|
||||
private static class FailingSource implements PollableSource<Object> {
|
||||
|
||||
|
||||
@@ -21,5 +21,10 @@
|
||||
</bean>
|
||||
|
||||
<bean id="handler" class="org.springframework.integration.handler.TestHandlers" factory-method="echoHandler"/>
|
||||
|
||||
|
||||
<bean class="org.springframework.integration.bus.MessageBusAwareBeanPostProcessor">
|
||||
<constructor-arg ref="bus"/>
|
||||
</bean>
|
||||
|
||||
<bean id= "messageBusAwareBean" class="org.springframework.integration.bus.TestMessageBusAwareImpl"/>
|
||||
</beans>
|
||||
|
||||
Reference in New Issue
Block a user