INT-845 added StartupMode enum since polling consumers must not start as eagerly as event-driven consumers subscribe to their input channels

This commit is contained in:
Mark Fisher
2009-10-13 00:39:30 +00:00
parent 78da00302e
commit 2064939c84
23 changed files with 259 additions and 41 deletions

View File

@@ -182,6 +182,7 @@ public class MessageChannelTemplateTests {
@Test
public void sendAndReceive() {
MessageChannelTemplate template = new MessageChannelTemplate();
template.setReceiveTimeout(3000);
Message<?> reply = template.sendAndReceive(new StringMessage("test"), this.requestChannel);
assertEquals("TEST", reply.getPayload());
}
@@ -189,6 +190,7 @@ public class MessageChannelTemplateTests {
@Test
public void sendAndReceiveWithDefaultChannel() {
MessageChannelTemplate template = new MessageChannelTemplate();
template.setReceiveTimeout(3000);
template.setDefaultChannel(this.requestChannel);
Message<?> reply = template.sendAndReceive(new StringMessage("test"));
assertEquals("TEST", reply.getPayload());
@@ -198,6 +200,7 @@ public class MessageChannelTemplateTests {
public void sendAndReceiveWithExplicitChannelTakesPrecedenceOverDefault() {
QueueChannel defaultChannel = new QueueChannel();
MessageChannelTemplate template = new MessageChannelTemplate(defaultChannel);
template.setReceiveTimeout(3000);
Message<?> message = new StringMessage("test");
Message<?> reply = template.sendAndReceive(message, this.requestChannel);
assertEquals("TEST", reply.getPayload());

View File

@@ -31,6 +31,7 @@ import org.junit.Test;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.core.Message;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.message.MessageSource;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.scheduling.PollerMetadata;
@@ -68,6 +69,7 @@ public class SourcePollingChannelAdapterFactoryBeanTests {
factoryBean.setPollerMetadata(pollerMetadata);
factoryBean.setAutoStartup(true);
factoryBean.afterPropertiesSet();
context.registerEndpoint("testPollingEndpoint", (AbstractEndpoint) factoryBean.getObject());
context.refresh();
Message<?> message = outputChannel.receive(30000);
assertEquals("test", message.getPayload());

View File

@@ -25,6 +25,7 @@ import org.junit.runner.RunWith;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.integration.endpoint.AbstractEndpoint.StartupMode;
import org.springframework.integration.handler.MethodInvokingMessageHandler;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -48,7 +49,7 @@ public class MethodInvokingOutboundChannelAdapterParserTests {
assertEquals(MethodInvokingMessageHandler.class, handler.getClass());
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(99, handlerAccessor.getPropertyValue("order"));
assertEquals(Boolean.FALSE, adapterAccessor.getPropertyValue("autoStartup"));
assertEquals(StartupMode.MANUAL, adapterAccessor.getPropertyValue("startupMode"));
}
@Test
@@ -59,7 +60,7 @@ public class MethodInvokingOutboundChannelAdapterParserTests {
assertEquals(MethodInvokingMessageHandler.class, handler.getClass());
DirectFieldAccessor handlerAccessor = new DirectFieldAccessor(handler);
assertEquals(99, handlerAccessor.getPropertyValue("order"));
assertEquals(Boolean.FALSE, adapterAccessor.getPropertyValue("autoStartup"));
assertEquals(StartupMode.MANUAL, adapterAccessor.getPropertyValue("startupMode"));
}

View File

@@ -0,0 +1,24 @@
<?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:si="http://www.springframework.org/schema/integration"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-2.0.xsd">
<si:inbound-channel-adapter id="inboundEndpoint" ref="counter" method="next" channel="testChannel">
<si:poller max-messages-per-poll="1">
<si:interval-trigger interval="50"/>
</si:poller>
</si:inbound-channel-adapter>
<si:channel id="testChannel"/>
<si:service-activator id="consumerEndpoint" input-channel="testChannel" ref="consumer"/>
<bean id="counter" class="org.springframework.integration.endpoint.ProducerAndConsumerAutoStartupTests$Counter"/>
<bean id="consumer" class="org.springframework.integration.endpoint.ProducerAndConsumerAutoStartupTests$Consumer"/>
</beans>

View File

@@ -0,0 +1,87 @@
/*
* Copyright 2002-2009 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.endpoint;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* @author Mark Fisher
* @since 2.0.0
*/
@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class ProducerAndConsumerAutoStartupTests {
@Autowired
private AbstractApplicationContext context;
@Autowired
private Consumer consumer;
@Test
public void test() throws Exception {
List<Integer> received = new ArrayList<Integer>();
for (int i = 0; i < 3; i++) {
received.add(consumer.poll(500));
}
context.stop();
assertEquals(new Integer(1), received.get(0));
assertEquals(new Integer(2), received.get(1));
assertEquals(new Integer(3), received.get(2));
}
static class Counter {
private final AtomicInteger count = new AtomicInteger();
public Integer next() {
return new Integer(count.incrementAndGet());
}
}
static class Consumer {
private final BlockingQueue<Integer> numbers = new LinkedBlockingQueue<Integer>();
public void receive(Integer number) {
numbers.add(number);
}
Integer poll(long timeoutInMillis) throws InterruptedException {
return numbers.poll(timeoutInMillis, TimeUnit.MILLISECONDS);
}
}
}