Added 'autoStartup' to message-bus (INT-101), 'initialDelay' and 'fixedRate' attributes for @Polled (INT-100), <handler-chain> element (INT-58), <selector> sub-element (INT-102), and <interceptor> sub-element (INT-106).

This commit is contained in:
Mark Fisher
2008-02-13 08:08:26 +00:00
parent 405311d9b9
commit ecce093f6e
32 changed files with 696 additions and 37 deletions

View File

@@ -4,7 +4,9 @@
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
<bean id="bus" class="org.springframework.integration.bus.MessageBus">
<property name="autoStartup" value="false"/>
</bean>
<bean id="inputChannel" class="org.springframework.integration.channel.SimpleChannel"/>

View File

@@ -7,7 +7,7 @@
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<si:message-bus/>
<si:message-bus auto-startup="false"/>
<si:channel id="inputChannel"/>

View File

@@ -27,7 +27,9 @@ 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.MessagingConfigurationException;
import org.springframework.integration.adapter.PollableSource;
import org.springframework.integration.adapter.PollingSourceAdapter;
import org.springframework.integration.adapter.SourceAdapter;
@@ -175,6 +177,19 @@ public class MessageBusTests {
bus.stop();
}
@Test
public void testMultipleMessageBusBeans() {
boolean exceptionThrown = false;
try {
new ClassPathXmlApplicationContext("multipleMessageBusBeans.xml", this.getClass());
}
catch (BeanCreationException e) {
exceptionThrown = true;
assertEquals(MessagingConfigurationException.class, e.getCause().getClass());
}
assertTrue(exceptionThrown);
}
private static class FailingSource implements PollableSource<Object> {

View File

@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="bus1" class="org.springframework.integration.bus.MessageBus"/>
<bean id="bus2" class="org.springframework.integration.bus.MessageBus"/>
</beans>

View File

@@ -27,6 +27,7 @@ import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessageDeliveryException;
import org.springframework.integration.channel.MessageChannel;
@@ -186,6 +187,20 @@ public class ChannelParserTests {
channel.send(new GenericMessage<Boolean>(true));
}
@Test
public void testChannelInteceptors() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"channelInterceptorParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("channel");
TestChannelInterceptor interceptor = (TestChannelInterceptor) context.getBean("interceptor");
assertEquals(0, interceptor.getSendCount());
channel.send(new StringMessage("test"));
assertEquals(1, interceptor.getSendCount());
assertEquals(0, interceptor.getReceiveCount());
channel.receive();
assertEquals(1, interceptor.getReceiveCount());
}
private static class TestHandler implements MessageHandler {

View File

@@ -17,17 +17,24 @@
package org.springframework.integration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.springframework.context.Lifecycle;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.SimpleChannel;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.message.selector.MessageSelectorRejectedException;
/**
* @author Mark Fisher
@@ -47,6 +54,19 @@ public class EndpointParserTests {
assertEquals("test", handler.getMessageString());
}
@Test
public void testEndpointWithChildHandler() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithHandlerChildElement.xml", this.getClass());
context.start();
MessageChannel channel = (MessageChannel) context.getBean("testChannel");
TestHandler handler = (TestHandler) context.getBean("testHandler");
assertNull(handler.getMessageString());
channel.send(new GenericMessage<String>(1, "test"));
handler.getLatch().await(50, TimeUnit.MILLISECONDS);
assertEquals("test", handler.getMessageString());
}
@Test
public void testHandlerAdapterEndpoint() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
@@ -84,4 +104,28 @@ public class EndpointParserTests {
assertEquals(7777, concurrencyPolicy.getKeepAliveSeconds());
}
@Test
public void testEndpointWithSelectorAccepts() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithSelectors.xml", this.getClass());
MessageHandler endpoint = (MessageHandler) context.getBean("endpoint");
((Lifecycle) endpoint).start();
Message<?> message = new StringMessage("test");
MessageChannel replyChannel = new SimpleChannel();
message.getHeader().setReplyChannel(replyChannel);
endpoint.handle(message);
Message<?> reply = replyChannel.receive(500);
assertNotNull(reply);
assertEquals("foo", reply.getPayload());
}
@Test(expected=MessageSelectorRejectedException.class)
public void testEndpointWithSelectorRejects() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"endpointWithSelectors.xml", this.getClass());
MessageHandler endpoint = (MessageHandler) context.getBean("endpoint");
((Lifecycle) endpoint).start();
endpoint.handle(new GenericMessage<Integer>(123));
}
}

View File

@@ -0,0 +1,62 @@
/*
* Copyright 2002-2007 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.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
*/
public class HandlerParserTests {
@Test
public void testTopLevelHandlerAdapter() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"handlerAdapterParserTests.xml", HandlerParserTests.class);
MessageHandler adapter = (MessageHandler) context.getBean("handlerAdapter");
assertNotNull(adapter);
Message<?> reply = adapter.handle(new StringMessage("foo"));
assertNotNull(reply);
assertEquals("bar", reply.getPayload());
}
@Test
public void testHandlerChain() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"handlerChainParserTests.xml", HandlerParserTests.class);
TestBean testBean = (TestBean) context.getBean("testBean");
assertNull(testBean.getMessage());
MessageHandler handlerChain = (MessageHandler) context.getBean("handlerChain");
assertNotNull(handlerChain);
Message<?> reply = handlerChain.handle(new StringMessage("test"));
assertNotNull(reply);
assertEquals(0, testBean.getLatch().getCount());
assertEquals("foo", testBean.getMessage());
assertEquals("bar", reply.getPayload());
}
}

View File

@@ -18,9 +18,12 @@ package org.springframework.integration.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.MessagingConfigurationException;
@@ -58,7 +61,6 @@ public class MessageBusParserTests {
MessageBus bus = (MessageBus) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
Subscription subscription = new Subscription("unknownChannel");
bus.registerHandler("handler", TestHandlers.nullHandler(), subscription);
bus.start();
}
@Test
@@ -73,4 +75,39 @@ public class MessageBusParserTests {
bus.stop();
}
@Test
public void testMultipleMessageBusElements() {
boolean exceptionThrown = false;
try {
new ClassPathXmlApplicationContext("multipleMessageBusElements.xml", this.getClass());
}
catch (BeanDefinitionStoreException e) {
exceptionThrown = true;
assertEquals(MessagingConfigurationException.class, e.getCause().getClass());
}
assertTrue(exceptionThrown);
}
@Test
public void testMessageBusElementAndBean() {
boolean exceptionThrown = false;
try {
new ClassPathXmlApplicationContext("messageBusElementAndBean.xml", this.getClass());
}
catch (BeanCreationException e) {
exceptionThrown = true;
assertEquals(MessagingConfigurationException.class, e.getCause().getClass());
}
assertTrue(exceptionThrown);
}
@Test
public void testAutoStartup() {
ApplicationContext context = new ClassPathXmlApplicationContext(
"messageBusWithAutoStartup.xml", this.getClass());
MessageBus bus = (MessageBus) context.getBean(MessageBusParser.MESSAGE_BUS_BEAN_NAME);
assertTrue(bus.isRunning());
bus.stop();
}
}

View File

@@ -27,21 +27,30 @@ public class TestBean {
private CountDownLatch latch;
private String replyMessageText = null;
public TestBean(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public void setReplyMessageText(String replyMessageText) {
this.replyMessageText = replyMessageText;
}
public CountDownLatch getLatch() {
return this.latch;
}
public void store(String message) {
public String store(String message) {
this.message = message;
latch.countDown();
return this.replyMessageText;
}
public String getMessage() {
return this.message;
}
}

View File

@@ -0,0 +1,54 @@
/*
* Copyright 2002-2007 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.config;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.interceptor.ChannelInterceptorAdapter;
import org.springframework.integration.message.Message;
/**
* @author Mark Fisher
*/
public class TestChannelInterceptor extends ChannelInterceptorAdapter {
private final AtomicInteger sendCount = new AtomicInteger();
private final AtomicInteger receiveCount = new AtomicInteger();
@Override
public boolean preSend(Message<?> message, MessageChannel channel) {
sendCount.incrementAndGet();
return true;
}
@Override
public void postReceive(Message<?> message, MessageChannel channel) {
receiveCount.incrementAndGet();
}
public int getSendCount() {
return this.sendCount.get();
}
public int getReceiveCount() {
return this.receiveCount.get();
}
}

View File

@@ -20,6 +20,7 @@ import java.util.concurrent.CountDownLatch;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.StringMessage;
/**
* @author Mark Fisher
@@ -30,15 +31,22 @@ public class TestHandler implements MessageHandler {
private CountDownLatch latch;
private String replyMessageText = null;
public TestHandler(int countdown) {
this.latch = new CountDownLatch(countdown);
}
public void setReplyMessageText(String replyMessageText) {
this.replyMessageText = replyMessageText;
}
public Message handle(Message message) {
this.messageString = (String) message.getPayload();
this.latch.countDown();
return null;
return (this.replyMessageText != null) ? new StringMessage(this.replyMessageText) : null;
}
public String getMessageString() {

View File

@@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<channel id="channel">
<interceptor ref="interceptor"/>
</channel>
<beans:bean id="interceptor" class="org.springframework.integration.config.TestChannelInterceptor"/>
</beans:beans>

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<channel id="testChannel" capacity="50"/>
<endpoint input-channel="testChannel">
<schedule period="100"/>
<handler ref="testHandler"/>
</endpoint>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<channel id="testChannel" capacity="50"/>
<endpoint id="endpoint" input-channel="testChannel">
<schedule period="100"/>
<selector ref="typeSelector"/>
<handler ref="testHandler"/>
</endpoint>
<beans:bean id="typeSelector" class="org.springframework.integration.message.selector.PayloadTypeSelector">
<beans:constructor-arg value="java.lang.String"/>
</beans:bean>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="foo"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<handler id="handlerAdapter" ref="testBean" method="store"/>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="bar"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<handler-chain id="handlerChain">
<handler ref="handler1"/>
<handler ref="handler2"/>
<handler ref="testBean" method="store"/>
</handler-chain>
<handler id="handler1" ref="testBean" method="store"/>
<beans:bean id="handler2" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="foo"/>
</beans:bean>
<beans:bean id="testBean" class="org.springframework.integration.config.TestBean">
<beans:constructor-arg value="1"/>
<beans:property name="replyMessageText" value="bar"/>
</beans:bean>
</beans:beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:integration="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<integration:message-bus/>
<bean id="bus" class="org.springframework.integration.bus.MessageBus"/>
</beans>

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus auto-startup="true"/>
</beans:beans>

View File

@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-1.0.xsd">
<message-bus/>
<message-bus/>
</beans:beans>

View File

@@ -14,7 +14,7 @@
<endpoint input-channel="testChannel" handler-ref="testHandler">
<schedule period="100"/>
</endpoint>
<beans:bean id="testHandler" class="org.springframework.integration.config.TestHandler">
<beans:constructor-arg value="1"/>
</beans:bean>