Major refactoring of endpoint subscription, scheduling, and activation. This includes significant changes to the dispatcher implementations and the addition of endpoint "triggers" to drive polling and dispatching of messages. Also introduces an explicit PublishSubscribeChannel implementation and thereby removes support for the "publish-subscribe" attribute from channel elements in the namespace.

This commit is contained in:
Mark Fisher
2008-07-04 18:48:33 +00:00
parent 218da1aacf
commit 0aa00b969b
34 changed files with 782 additions and 1222 deletions

View File

@@ -30,6 +30,7 @@ import org.springframework.beans.factory.BeanCreationException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.channel.DispatcherPolicy;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.endpoint.SourceEndpoint;
import org.springframework.integration.handler.MessageHandler;
@@ -129,8 +130,7 @@ public class MessageBusTests {
@Test
public void testBothHandlersReceivePublishSubscribeMessage() throws InterruptedException {
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy(true);
QueueChannel inputChannel = new QueueChannel(10, dispatcherPolicy);
PublishSubscribeChannel inputChannel = new PublishSubscribeChannel();
QueueChannel outputChannel1 = new QueueChannel();
QueueChannel outputChannel2 = new QueueChannel();
final CountDownLatch latch = new CountDownLatch(2);

View File

@@ -1,470 +0,0 @@
/*
* 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.Lifecycle;
import org.springframework.integration.channel.DispatcherPolicy;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.MessageEndpointBeanPostProcessor;
import org.springframework.integration.endpoint.AbstractEndpoint;
import org.springframework.integration.endpoint.ConcurrencyPolicy;
import org.springframework.integration.endpoint.HandlerEndpoint;
import org.springframework.integration.endpoint.interceptor.ConcurrencyInterceptor;
import org.springframework.integration.handler.MessageHandler;
import org.springframework.integration.handler.MessageHandlerRejectedExecutionException;
import org.springframework.integration.handler.TestHandlers;
import org.springframework.integration.message.ErrorMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.message.selector.PayloadTypeSelector;
import org.springframework.integration.scheduling.MessagePublishingErrorHandler;
import org.springframework.integration.scheduling.SimpleTaskScheduler;
/**
* @author Mark Fisher
*/
public class SubscriptionManagerTests {
private SimpleTaskScheduler scheduler = new SimpleTaskScheduler(new ScheduledThreadPoolExecutor(10));
@Test
public void testNonBroadcastingDispatcherSendsToExactlyOneEndpoint() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
QueueChannel channel = new QueueChannel();
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(handler1, true));
manager.addTarget(createEndpoint(handler2, true));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("exactly one handler should have received message", 1, counter1.get() + counter2.get());
}
@Test
public void testBroadcastingDispatcherSendsToAllEndpoints() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(2);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(true));
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(handler1, true));
manager.addTarget(createEndpoint(handler2, true));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("both handlers should have received message", 2, counter1.get() + counter2.get());
}
@Test
public void testNonBroadcastingDispatcherSkipsInactiveExecutor() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger counter3 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
QueueChannel channel = new QueueChannel();
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
MessageTarget inactiveEndpoint = createEndpoint(handler1, true);
manager.addTarget(inactiveEndpoint);
manager.addTarget(createEndpoint(handler2, true));
manager.addTarget(createEndpoint(handler3, true));
manager.start();
((Lifecycle) inactiveEndpoint).stop();
channel.send(new StringMessage(1, "test"));
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("inactive handler should not have received message", 0, counter1.get());
assertEquals("exactly one handler should have received message", 1, counter2.get() + counter3.get());
}
@Test
public void testBroadcastingDispatcherSkipsInactiveExecutor() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger counter3 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(2);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(true));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
MessageTarget inactiveEndpoint = createEndpoint(handler2, true);
manager.addTarget(createEndpoint(handler1, true));
manager.addTarget(inactiveEndpoint);
manager.addTarget(createEndpoint(handler3, true));
manager.start();
((Lifecycle) inactiveEndpoint).stop();
channel.send(new StringMessage(1, "test"));
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("inactive handler should not have received message", 0, counter2.get());
assertEquals("both active handlers should have received message", 2, counter1.get() + counter3.get());
}
@Test
public void testDispatcherWithNoExecutorsDoesNotFail() {
QueueChannel channel = new QueueChannel();
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.start();
}
@Test
public void testBroadcastingDispatcherReachesRejectionLimitAndShouldFail() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter3 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(2);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(true));
channel.getDispatcherPolicy().setRejectionLimit(2);
channel.getDispatcherPolicy().setRetryInterval(3);
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(handler1, true));
manager.addTarget(new MessageTarget() {
public boolean send(Message<?> message) {
throw new MessageHandlerRejectedExecutionException(message);
}
});
manager.addTarget(createEndpoint(handler3, true));
QueueChannel errorChannel = new QueueChannel();
scheduler.setErrorHandler(new MessagePublishingErrorHandler(errorChannel));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
Message<?> errorMessage = errorChannel.receive(1000);
assertNotNull(errorMessage);
assertTrue(errorMessage instanceof ErrorMessage);
assertEquals(MessageDeliveryException.class, ((ErrorMessage) errorMessage).getPayload().getClass());
}
@Test
public void testBroadcastingDispatcherReachesRejectionLimitAndShouldNotFail() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(3);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(true));
channel.getDispatcherPolicy().setRejectionLimit(2);
channel.getDispatcherPolicy().setRetryInterval(3);
channel.getDispatcherPolicy().setShouldFailOnRejectionLimit(false);
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(handler1, false));
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.addTarget(createEndpoint(handler2, false));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("both non-rejecting handlers should have received message", 2, counter1.get() + counter2.get());
}
@Test
public void testNonBroadcastingDispatcherReachesRejectionLimitAndShouldFail() throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(4);
MessageHandler handler1 = TestHandlers.rejectingCountDownHandler(latch);
MessageHandler handler2 = TestHandlers.rejectingCountDownHandler(latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(false));
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
channel.getDispatcherPolicy().setRejectionLimit(2);
channel.getDispatcherPolicy().setRetryInterval(3);
manager.addTarget(createEndpoint(handler1, false));
manager.addTarget(createEndpoint(handler2, false));
QueueChannel errorChannel = new QueueChannel();
scheduler.setErrorHandler(new MessagePublishingErrorHandler(errorChannel));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
Message<?> errorMessage = errorChannel.receive(500);
assertNotNull(errorMessage);
assertTrue(errorMessage instanceof ErrorMessage);
assertEquals(MessageDeliveryException.class, ((ErrorMessage) errorMessage).getPayload().getClass());
}
@Test
public void testNonBroadcastingDispatcherReachesRejectionLimitButShouldNotFail() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger rejectedCounter1 = new AtomicInteger();
final AtomicInteger rejectedCounter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(4);
QueueChannel channel = new QueueChannel();
channel.getDispatcherPolicy().setRejectionLimit(2);
channel.getDispatcherPolicy().setRetryInterval(3);
channel.getDispatcherPolicy().setShouldFailOnRejectionLimit(false);
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
rejectedCounter1.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
rejectedCounter2.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("latch should have counted down within allotted time", 0, latch.getCount());
assertEquals("rejecting handlers should not have received message", 0, counter1.get() + counter2.get());
assertEquals("handler1 should have rejected two times", 2, rejectedCounter1.get());
assertEquals("handler2 should have rejected two times", 2, rejectedCounter2.get());
}
@Test
public void testNonBroadcastingDispatcherWithOneEndpointSucceeding() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger counter3 = new AtomicInteger();
final AtomicInteger rejectedCounter1 = new AtomicInteger();
final AtomicInteger rejectedCounter2 = new AtomicInteger();
final AtomicInteger rejectedCounter3 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(5);
final MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
dispatcherPolicy.setRejectionLimit(2);
dispatcherPolicy.setRetryInterval(3);
dispatcherPolicy.setShouldFailOnRejectionLimit(false);
QueueChannel channel = new QueueChannel(25, dispatcherPolicy);
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
rejectedCounter1.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
if (rejectedCounter2.get() == 1) {
return handler2.handle(message);
}
rejectedCounter2.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
rejectedCounter3.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("handler1 should not have received message", 0, counter1.get());
assertEquals("handler2 should have received message the second time", 1, counter2.get());
assertEquals("handler3 should not have received message", 0, counter3.get());
assertEquals("handler1 should have rejected two times", 2, rejectedCounter1.get());
assertEquals("handler2 should have rejected one time", 1, rejectedCounter2.get());
assertEquals("handler3 should have rejected one time", 1, rejectedCounter3.get());
}
@Test
public void testBroadcastingDispatcherStillRetriesRejectedExecutorAfterOtherSucceeds() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger rejectedCounter1 = new AtomicInteger();
final AtomicInteger rejectedCounter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(8);
final MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
final MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy(true);
dispatcherPolicy.setRejectionLimit(5);
dispatcherPolicy.setRetryInterval(3);
dispatcherPolicy.setShouldFailOnRejectionLimit(false);
QueueChannel channel = new QueueChannel(25, dispatcherPolicy);
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
if (rejectedCounter1.get() == 2) {
return handler1.handle(message);
}
rejectedCounter1.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.addTarget(createEndpoint(new MessageHandler() {
public Message<?> handle(Message<?> message) {
if (rejectedCounter2.get() == 4) {
return handler2.handle(message);
}
rejectedCounter2.incrementAndGet();
latch.countDown();
throw new MessageHandlerRejectedExecutionException(message);
}
}, false));
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("handler1 should have received one message", 1, counter1.get());
assertEquals("handler2 should have received one message", 1, counter2.get());
assertEquals("handler1 should have rejected two times", 2, rejectedCounter1.get());
assertEquals("handler2 should have rejected four times", 4, rejectedCounter2.get());
}
@Test
public void testTwoExecutorsWithSelectorsAndOneAccepts() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
QueueChannel channel = new QueueChannel();
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1);
HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2);
endpoint1.setMessageSelector(new PayloadTypeSelector(Integer.class));
endpoint2.setMessageSelector(new PayloadTypeSelector(String.class));
manager.addTarget(endpoint1);
manager.addTarget(endpoint2);
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("handler1 should not have accepted the message", 0, counter1.get());
assertEquals("handler2 should have accepted the message", 1, counter2.get());
}
@Test
public void testTwoExecutorsWithSelectorsAndNeitherAccepts() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final AtomicInteger selectorCounter1 = new AtomicInteger();
final AtomicInteger selectorCounter2 = new AtomicInteger();
final CountDownLatch selectorLatch = new CountDownLatch(2);
final CountDownLatch handlerLatch = new CountDownLatch(1);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, handlerLatch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, handlerLatch);
QueueChannel channel = new QueueChannel();
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
final HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1);
final HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2);
endpoint1.setMessageSelector(new PayloadTypeSelector(Integer.class) {
@Override
public boolean accept(Message<?> message) {
selectorCounter1.incrementAndGet();
selectorLatch.countDown();
return super.accept(message);
}
});
endpoint2.setMessageSelector(new PayloadTypeSelector(Integer.class) {
@Override
public boolean accept(Message<?> message) {
selectorCounter2.incrementAndGet();
selectorLatch.countDown();
return super.accept(message);
}
});
manager.addTarget(endpoint1);
manager.addTarget(endpoint2);
manager.start();
selectorLatch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, selectorLatch.getCount());
assertEquals("handler1 should not have accepted the message", 0, counter1.get());
assertEquals("handler2 should not have accepted the message", 0, counter2.get());
assertEquals("executor1 should have had exactly one attempt", 1, selectorCounter1.get());
assertEquals("executor2 should have had exactly one attempt", 1, selectorCounter2.get());
assertEquals("handlerLatch should not have counted down", 1, handlerLatch.getCount());
}
@Test
public void testBroadcastingDispatcherWithSelectorsAndOneAccepts() throws InterruptedException {
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(1);
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
QueueChannel channel = new QueueChannel(5, new DispatcherPolicy(true));
channel.send(new StringMessage(1, "test"));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
HandlerEndpoint endpoint1 = new HandlerEndpoint(handler1);
HandlerEndpoint endpoint2 = new HandlerEndpoint(handler2);
endpoint1.setMessageSelector(new PayloadTypeSelector(Integer.class));
endpoint2.setMessageSelector(new PayloadTypeSelector(String.class));
manager.addTarget(endpoint1);
manager.addTarget(endpoint2);
manager.start();
latch.await(2000, TimeUnit.MILLISECONDS);
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
assertEquals("endpoint1 should not have accepted the message", 0, counter1.get());
assertEquals("endpoint2 should have accepted the message", 1, counter2.get());
}
private static MessageTarget createEndpoint(MessageHandler handler, boolean asynchronous) {
MessageTarget endpoint = new HandlerEndpoint(handler);
if (asynchronous) {
MessageEndpointBeanPostProcessor postProcessor = new MessageEndpointBeanPostProcessor();
((AbstractEndpoint) endpoint).addInterceptor(new ConcurrencyInterceptor(new ConcurrencyPolicy(1, 1)));
endpoint = (MessageTarget) postProcessor.postProcessAfterInitialization(endpoint, "test-endpoint");
}
try {
((InitializingBean) endpoint).afterPropertiesSet();
}
catch (Exception e) {
throw new RuntimeException("failed to initialize endpoint", e);
}
return endpoint;
}
}

View File

@@ -20,27 +20,22 @@ import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.beans.FatalBeanException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.integration.bus.SubscriptionManager;
import org.springframework.integration.channel.DispatcherPolicy;
import org.springframework.integration.channel.MessageChannel;
import org.springframework.integration.channel.PublishSubscribeChannel;
import org.springframework.integration.channel.QueueChannel;
import org.springframework.integration.config.TestChannelInterceptor;
import org.springframework.integration.message.GenericMessage;
import org.springframework.integration.message.Message;
import org.springframework.integration.message.MessageDeliveryException;
import org.springframework.integration.message.MessagePriority;
import org.springframework.integration.message.StringMessage;
import org.springframework.integration.message.MessageTarget;
import org.springframework.integration.scheduling.SimpleTaskScheduler;
/**
* @author Mark Fisher
@@ -65,43 +60,11 @@ public class ChannelParserTests {
}
@Test
public void testPointToPointChannelByDefault() throws InterruptedException {
public void testQueueChannelByDefault() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("pointToPointChannelByDefault");
channel.send(new StringMessage("test"));
SimpleTaskScheduler scheduler = new SimpleTaskScheduler(new ScheduledThreadPoolExecutor(1));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
AtomicInteger counter = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(1);
TestTarget target1 = new TestTarget(counter, latch);
TestTarget target2 = new TestTarget(counter, latch);
manager.addTarget(target1);
manager.addTarget(target2);
manager.start();
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertEquals(1, counter.get());
}
@Test
public void testPointToPointChannelExplicitlyConfigured() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("pointToPointChannelExplicitlyConfigured");
channel.send(new StringMessage("test"));
SimpleTaskScheduler scheduler = new SimpleTaskScheduler(new ScheduledThreadPoolExecutor(1));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
AtomicInteger counter = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(1);
TestTarget target1 = new TestTarget(counter, latch);
TestTarget target2 = new TestTarget(counter, latch);
manager.addTarget(target1);
manager.addTarget(target2);
manager.start();
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertEquals(1, counter.get());
MessageChannel channel = (MessageChannel) context.getBean("queueChannelByDefault");
assertEquals(QueueChannel.class, channel.getClass());
}
@Test
@@ -109,26 +72,27 @@ public class ChannelParserTests {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("publishSubscribeChannel");
channel.send(new StringMessage("test"));
SimpleTaskScheduler scheduler = new SimpleTaskScheduler(new ScheduledThreadPoolExecutor(1));
SubscriptionManager manager = new SubscriptionManager(channel, scheduler);
AtomicInteger counter = new AtomicInteger();
CountDownLatch latch = new CountDownLatch(2);
TestTarget target1 = new TestTarget(counter, latch);
TestTarget target2 = new TestTarget(counter, latch);
manager.addTarget(target1);
manager.addTarget(target2);
manager.start();
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
assertEquals(2, counter.get());
assertEquals(PublishSubscribeChannel.class, channel.getClass());
}
@Test
public void testPublishSubscribeChannelWithTaskExecutorReference() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("publishSubscribeChannelWithTaskExecutorRef");
assertEquals(PublishSubscribeChannel.class, channel.getClass());
DirectFieldAccessor accessor = new DirectFieldAccessor(channel);
accessor = new DirectFieldAccessor(accessor.getPropertyValue("dispatcher"));
Object taskExecutorProperty = accessor.getPropertyValue("taskExecutor");
Object taskExecutorBean = context.getBean("taskExecutor");
assertEquals(taskExecutorBean, taskExecutorProperty);
}
@Test
public void testDefaultDispatcherPolicy() throws InterruptedException {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("pointToPointChannelByDefault");
MessageChannel channel = (MessageChannel) context.getBean("queueChannelByDefault");
DispatcherPolicy dispatcherPolicy = channel.getDispatcherPolicy();
assertFalse(dispatcherPolicy.isPublishSubscribe());
assertEquals(DispatcherPolicy.DEFAULT_REJECTION_LIMIT, dispatcherPolicy.getRejectionLimit());
@@ -142,7 +106,6 @@ public class ChannelParserTests {
"channelParserTests.xml", this.getClass());
MessageChannel channel = (MessageChannel) context.getBean("channelWithDispatcherPolicy");
DispatcherPolicy dispatcherPolicy = channel.getDispatcherPolicy();
assertTrue(dispatcherPolicy.isPublishSubscribe());
assertEquals(7, dispatcherPolicy.getRejectionLimit());
assertEquals(77, dispatcherPolicy.getRetryInterval());
assertFalse(dispatcherPolicy.getShouldFailOnRejectionLimit());
@@ -267,23 +230,4 @@ public class ChannelParserTests {
assertTrue(threwException);
}
private static class TestTarget implements MessageTarget {
private AtomicInteger counter;
private CountDownLatch latch;
TestTarget(AtomicInteger counter, CountDownLatch latch) {
this.counter = counter;
this.latch = latch;
}
public boolean send(Message<?> message) {
this.counter.incrementAndGet();
this.latch.countDown();
return true;
}
}
}

View File

@@ -9,13 +9,14 @@
<queue-channel id="capacityChannel" capacity="10"/>
<channel id="pointToPointChannelByDefault"/>
<channel id="queueChannelByDefault"/>
<channel id="pointToPointChannelExplicitlyConfigured" publish-subscribe="false"/>
<publish-subscribe-channel id="publishSubscribeChannel"/>
<channel id="publishSubscribeChannel" publish-subscribe="true"/>
<publish-subscribe-channel id="publishSubscribeChannelWithTaskExecutorRef"
task-executor="taskExecutor"/>
<channel id="channelWithDispatcherPolicy" publish-subscribe="true">
<channel id="channelWithDispatcherPolicy">
<dispatcher-policy rejection-limit="7"
retry-interval="77"
should-fail-on-rejection-limit="false"/>
@@ -27,4 +28,6 @@
<channel id="stringOrNumberChannel" datatype="java.lang.String,java.lang.Number"/>
<beans:bean id="taskExecutor" class="org.springframework.core.task.SimpleAsyncTaskExecutor"/>
</beans:beans>

View File

@@ -47,14 +47,13 @@ public class MessageEndpointBeanPostProcessorTests {
public void testHandlerEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
context.start();
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("handlerEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();
@@ -72,14 +71,13 @@ public class MessageEndpointBeanPostProcessorTests {
public void testTargetEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
context.start();
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("targetEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();
@@ -97,14 +95,13 @@ public class MessageEndpointBeanPostProcessorTests {
public void testSourceEndpointWithAdviceChain() {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"messageEndpointBeanPostProcessorTests.xml", this.getClass());
context.start();
MessageEndpoint endpoint = (MessageEndpoint) context.getBean("sourceEndpointWithAdvice");
assertTrue(AopUtils.isAopProxy(endpoint));
TestBeforeAdvice beforeAdvice = (TestBeforeAdvice) context.getBean("simpleAdvice");
TestEndpointInterceptor interceptor = (TestEndpointInterceptor) context.getBean("interceptor");
assertEquals(0, beforeAdvice.getCount());
assertEquals(0, interceptor.getCount());
endpoint.invoke(new CommandMessage(new PollCommand()));
endpoint.send(new CommandMessage(new PollCommand()));
assertEquals(1, beforeAdvice.getCount());
assertEquals(2, interceptor.getCount());
context.stop();

View File

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

View File

@@ -40,7 +40,7 @@ public class SimpleDispatcherTests {
public void testSingleMessage() throws InterruptedException {
SimpleDispatcher dispatcher = new SimpleDispatcher(new DispatcherPolicy());
final CountDownLatch latch = new CountDownLatch(1);
dispatcher.subscribe(createEndpoint(TestHandlers.countDownHandler(latch)));
dispatcher.addTarget(createEndpoint(TestHandlers.countDownHandler(latch)));
dispatcher.send(new StringMessage("test"));
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
@@ -52,8 +52,8 @@ public class SimpleDispatcherTests {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
dispatcher.subscribe(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch)));
dispatcher.subscribe(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch)));
dispatcher.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch)));
dispatcher.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch)));
dispatcher.send(new StringMessage("test"));
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());
@@ -66,8 +66,8 @@ public class SimpleDispatcherTests {
final CountDownLatch latch = new CountDownLatch(2);
final AtomicInteger counter1 = new AtomicInteger();
final AtomicInteger counter2 = new AtomicInteger();
dispatcher.subscribe(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch)));
dispatcher.subscribe(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch)));
dispatcher.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter1, latch)));
dispatcher.addTarget(createEndpoint(TestHandlers.countingCountDownHandler(counter2, latch)));
dispatcher.send(new StringMessage("test"));
latch.await(500, TimeUnit.MILLISECONDS);
assertEquals(0, latch.getCount());

View File

@@ -42,7 +42,7 @@ public class SourceEndpointTests {
QueueChannel channel = new QueueChannel();
SourceEndpoint endpoint = new SourceEndpoint(source, channel);
endpoint.afterPropertiesSet();
endpoint.invoke(new CommandMessage(new PollCommand()));
endpoint.send(new CommandMessage(new PollCommand()));
Message<?> message = channel.receive(1000);
assertNotNull("message should not be null", message);
assertEquals("testing.1", message.getPayload());
@@ -55,7 +55,7 @@ public class SourceEndpointTests {
SourceEndpoint endpoint = new SourceEndpoint(source, channel);
endpoint.setAutoStartup(false);
endpoint.afterPropertiesSet();
endpoint.invoke(new CommandMessage(new PollCommand()));
endpoint.send(new CommandMessage(new PollCommand()));
}
private static class TestSource implements MessageSource<String> {

View File

@@ -79,13 +79,13 @@ public class TransactionInterceptorTests {
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("required");
assertEquals(0, txManager.getCommitCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(1, txManager.getCommitCount());
TestTransactionManager outerTxManager = new TestTransactionManager();
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return endpoint.invoke(new StringMessage("test"));
return endpoint.send(new StringMessage("test"));
}
});
assertEquals(1, outerTxManager.getCommitCount());
@@ -100,13 +100,13 @@ public class TransactionInterceptorTests {
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("requiresNew");
assertEquals(0, txManager.getCommitCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(1, txManager.getCommitCount());
TestTransactionManager outerTxManager = new TestTransactionManager();
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return endpoint.invoke(new StringMessage("test"));
return endpoint.send(new StringMessage("test"));
}
});
assertEquals(1, outerTxManager.getCommitCount());
@@ -121,13 +121,13 @@ public class TransactionInterceptorTests {
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("supports");
assertEquals(0, txManager.getCommitCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(0, txManager.getCommitCount());
TestTransactionManager outerTxManager = new TestTransactionManager();
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return endpoint.invoke(new StringMessage("test"));
return endpoint.send(new StringMessage("test"));
}
});
assertEquals(0, txManager.getCommitCount());
@@ -141,13 +141,13 @@ public class TransactionInterceptorTests {
TestTransactionManager txManager = (TestTransactionManager) context.getBean("txManager");
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("notSupported");
assertEquals(0, txManager.getCommitCount());
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
assertEquals(0, txManager.getCommitCount());
TestTransactionManager outerTxManager = new TestTransactionManager();
TransactionTemplate txTemplate = new TransactionTemplate(outerTxManager);
txTemplate.execute(new TransactionCallback() {
public Object doInTransaction(TransactionStatus status) {
return endpoint.invoke(new StringMessage("test"));
return endpoint.send(new StringMessage("test"));
}
});
assertEquals(0, txManager.getCommitCount());
@@ -159,7 +159,7 @@ public class TransactionInterceptorTests {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
"transactionInterceptorPropagationTests.xml", this.getClass());
final MessageEndpoint endpoint = (MessageEndpoint) context.getBean("mandatory");
endpoint.invoke(new StringMessage("test"));
endpoint.send(new StringMessage("test"));
}
}