Refactored so that endpoint is reponsible for managing ConcurrentHandlers rather than dispatchers having that responsibility. Also providing errorHandler for endpoints. That errorHandler is also used for asynchronous handler task execution.
This commit is contained in:
@@ -30,8 +30,12 @@ import org.springframework.integration.MessageDeliveryException;
|
||||
import org.springframework.integration.channel.SimpleChannel;
|
||||
import org.springframework.integration.dispatcher.DefaultMessageDispatcher;
|
||||
import org.springframework.integration.dispatcher.MessageHandlerRejectedExecutionException;
|
||||
import org.springframework.integration.endpoint.ConcurrencyPolicy;
|
||||
import org.springframework.integration.endpoint.DefaultMessageEndpoint;
|
||||
import org.springframework.integration.handler.ConcurrentHandler;
|
||||
import org.springframework.integration.handler.InterceptingMessageHandler;
|
||||
import org.springframework.integration.handler.MessageHandler;
|
||||
import org.springframework.integration.handler.PooledMessageHandler;
|
||||
import org.springframework.integration.handler.TestHandlers;
|
||||
import org.springframework.integration.message.ErrorMessage;
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
@@ -49,20 +53,20 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter1 = new AtomicInteger();
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1));
|
||||
SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler();
|
||||
scheduler.start();
|
||||
dispatcher.setMessagingTaskScheduler(scheduler);
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("exactly one endpoint should have received message", 1, counter1.get() + counter2.get());
|
||||
assertEquals("exactly one handler should have received message", 1, counter1.get() + counter2.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -70,18 +74,18 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter1 = new AtomicInteger();
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1));
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("both endpoints should have received message", 2, counter1.get() + counter2.get());
|
||||
assertEquals("both handlers should have received message", 2, counter1.get() + counter2.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -90,24 +94,24 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final AtomicInteger counter3 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
TestEndpoint endpoint3 = new TestEndpoint(counter3, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1) {
|
||||
@Override
|
||||
public void start() {
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint3, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler3, 1, 1));
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("inactive endpoint should not have received message", 0, counter1.get());
|
||||
assertEquals("exactly one endpoint should have received message", 1, counter2.get() + counter3.get());
|
||||
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
|
||||
@@ -116,25 +120,25 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final AtomicInteger counter3 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
TestEndpoint endpoint3 = new TestEndpoint(counter3, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public void start() {
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint3, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler3, 1, 1));
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("inactive endpoint should not have received message", 0, counter2.get());
|
||||
assertEquals("both active endpoints should have received message", 2, counter1.get() + counter3.get());
|
||||
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
|
||||
@@ -151,23 +155,23 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final AtomicInteger counter3 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
TestEndpoint endpoint3 = new TestEndpoint(counter3, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.setRejectionLimit(2);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint3, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler3, 1, 1));
|
||||
SimpleChannel errorChannel = new SimpleChannel();
|
||||
SimpleMessagingTaskScheduler scheduler = new SimpleMessagingTaskScheduler();
|
||||
scheduler.setErrorHandler(new MessagePublishingErrorHandler(errorChannel));
|
||||
@@ -187,9 +191,9 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final AtomicInteger counter3 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
TestEndpoint endpoint3 = new TestEndpoint(counter3, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
@@ -197,19 +201,19 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcher.setRejectionLimit(2);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.setShouldFailOnRejectionLimit(false);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1));
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint3, 1, 1));
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler3, 1, 1));
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("rejecting endpoint should not have received message", 0, counter2.get());
|
||||
assertEquals("both non-rejecting endpoints should have received message", 2, counter1.get() + counter3.get());
|
||||
assertEquals("rejecting handler should not have received message", 0, counter2.get());
|
||||
assertEquals("both non-rejecting handlers should have received message", 2, counter1.get() + counter3.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -217,21 +221,21 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter1 = new AtomicInteger();
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(4);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.setRejectionLimit(2);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
latch.countDown();
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
latch.countDown();
|
||||
@@ -258,15 +262,15 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger rejectedCounter1 = new AtomicInteger();
|
||||
final AtomicInteger rejectedCounter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(4);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.setRejectionLimit(2);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.setShouldFailOnRejectionLimit(false);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
rejectedCounter1.incrementAndGet();
|
||||
@@ -274,7 +278,7 @@ public class DefaultMessageDispatcherTests {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
rejectedCounter2.incrementAndGet();
|
||||
@@ -286,9 +290,9 @@ public class DefaultMessageDispatcherTests {
|
||||
latch.await(500, 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 endpoints should not have received message", 0, counter1.get() + counter2.get());
|
||||
assertEquals("endpoint1 should have rejected two times", 2, rejectedCounter1.get());
|
||||
assertEquals("endpoint2 should have rejected two times", 2, rejectedCounter2.get());
|
||||
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
|
||||
@@ -300,16 +304,16 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger rejectedCounter2 = new AtomicInteger();
|
||||
final AtomicInteger rejectedCounter3 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(5);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
TestEndpoint endpoint3 = new TestEndpoint(counter3, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
dispatcher.setRejectionLimit(2);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.setShouldFailOnRejectionLimit(false);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
rejectedCounter1.incrementAndGet();
|
||||
@@ -317,7 +321,7 @@ public class DefaultMessageDispatcherTests {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
if (rejectedCounter2.get() == 1) {
|
||||
@@ -328,7 +332,7 @@ public class DefaultMessageDispatcherTests {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint3, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler3, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
rejectedCounter3.incrementAndGet();
|
||||
@@ -339,12 +343,12 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("endpoint1 should not have received message", 0, counter1.get());
|
||||
assertEquals("endpoint2 should have received message the second time", 1, counter2.get());
|
||||
assertEquals("endpoint3 should not have received message", 0, counter3.get());
|
||||
assertEquals("endpoint1 should have rejected two times", 2, rejectedCounter1.get());
|
||||
assertEquals("endpoint2 should have rejected one time", 1, rejectedCounter2.get());
|
||||
assertEquals("endpoint3 should have rejected one time", 1, rejectedCounter3.get());
|
||||
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
|
||||
@@ -354,8 +358,8 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger rejectedCounter1 = new AtomicInteger();
|
||||
final AtomicInteger rejectedCounter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(8);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
@@ -363,7 +367,7 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcher.setRejectionLimit(5);
|
||||
dispatcher.setRetryInterval(3);
|
||||
dispatcher.setShouldFailOnRejectionLimit(false);
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
if (rejectedCounter1.get() == 2) {
|
||||
@@ -374,7 +378,7 @@ public class DefaultMessageDispatcherTests {
|
||||
throw new MessageHandlerRejectedExecutionException();
|
||||
}
|
||||
});
|
||||
dispatcher.addHandler(new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler2, 1, 1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
if (rejectedCounter2.get() == 4) {
|
||||
@@ -388,10 +392,10 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
assertEquals("endpoint1 should have received one message", 1, counter1.get());
|
||||
assertEquals("endpoint2 should have received one message", 1, counter2.get());
|
||||
assertEquals("endpoint1 should have rejected two times", 2, rejectedCounter1.get());
|
||||
assertEquals("endpoint2 should have rejected four times", 4, rejectedCounter2.get());
|
||||
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
|
||||
@@ -399,22 +403,22 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter1 = new AtomicInteger();
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
PooledMessageHandler executor1 = new PooledMessageHandler(endpoint1, 1, 1);
|
||||
PooledMessageHandler executor2 = new PooledMessageHandler(endpoint2, 1, 1);
|
||||
executor1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
executor2.addMessageSelector(new PayloadTypeSelector(String.class));
|
||||
dispatcher.addHandler(executor1);
|
||||
dispatcher.addHandler(executor2);
|
||||
DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1);
|
||||
DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint(handler2);
|
||||
endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
endpoint2.addMessageSelector(new PayloadTypeSelector(String.class));
|
||||
dispatcher.addHandler(endpoint1);
|
||||
dispatcher.addHandler(endpoint2);
|
||||
dispatcher.start();
|
||||
latch.await(500, 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());
|
||||
assertEquals("handler1 should not have accepted the message", 0, counter1.get());
|
||||
assertEquals("handler2 should have accepted the message", 1, counter2.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -424,40 +428,42 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger attemptedCounter1 = new AtomicInteger();
|
||||
final AtomicInteger attemptedCounter2 = new AtomicInteger();
|
||||
final CountDownLatch attemptedLatch = new CountDownLatch(2);
|
||||
final CountDownLatch endpointLatch = new CountDownLatch(1);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, endpointLatch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, endpointLatch);
|
||||
final CountDownLatch handlerLatch = new CountDownLatch(1);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, attemptedLatch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, attemptedLatch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
PooledMessageHandler executor1 = new PooledMessageHandler(endpoint1, 1, 1) {
|
||||
DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1);
|
||||
DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint(handler2);
|
||||
endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
endpoint2.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
MessageHandler interceptor1 = new InterceptingMessageHandler(endpoint1) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
public Message<?> handle(Message<?> message, MessageHandler target) {
|
||||
attemptedCounter1.incrementAndGet();
|
||||
attemptedLatch.countDown();
|
||||
return super.handle(message);
|
||||
return target.handle(message);
|
||||
}
|
||||
};
|
||||
PooledMessageHandler executor2 = new PooledMessageHandler(endpoint2, 1, 1) {
|
||||
MessageHandler interceptor2 = new InterceptingMessageHandler(endpoint2) {
|
||||
@Override
|
||||
public Message<?> handle(Message<?> message) {
|
||||
public Message<?> handle(Message<?> message, MessageHandler target) {
|
||||
attemptedCounter2.incrementAndGet();
|
||||
attemptedLatch.countDown();
|
||||
return super.handle(message);
|
||||
return target.handle(message);
|
||||
}
|
||||
};
|
||||
executor1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
executor2.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
dispatcher.addHandler(executor1);
|
||||
dispatcher.addHandler(executor2);
|
||||
dispatcher.addHandler(interceptor1);
|
||||
dispatcher.addHandler(interceptor2);
|
||||
dispatcher.start();
|
||||
attemptedLatch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, attemptedLatch.getCount());
|
||||
assertEquals("endpoint1 should not have accepted the message", 0, counter1.get());
|
||||
assertEquals("endpoint2 should not have accepted the message", 0, counter2.get());
|
||||
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, attemptedCounter1.get());
|
||||
assertEquals("executor2 should have had exactly one attempt", 1, attemptedCounter2.get());
|
||||
assertEquals("endpointLatch should not have counted down", 1, endpointLatch.getCount());
|
||||
assertEquals("handlerLatch should not have counted down", 1, handlerLatch.getCount());
|
||||
assertEquals("attemptedLatch should have counted down", 0, attemptedLatch.getCount());
|
||||
}
|
||||
|
||||
@@ -466,18 +472,22 @@ public class DefaultMessageDispatcherTests {
|
||||
final AtomicInteger counter1 = new AtomicInteger();
|
||||
final AtomicInteger counter2 = new AtomicInteger();
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
TestEndpoint endpoint1 = new TestEndpoint(counter1, latch);
|
||||
TestEndpoint endpoint2 = new TestEndpoint(counter2, latch);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel();
|
||||
channel.setPublishSubscribe(true);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel);
|
||||
PooledMessageHandler executor1 = new PooledMessageHandler(endpoint1, 1, 1);
|
||||
PooledMessageHandler executor2 = new PooledMessageHandler(endpoint2, 1, 1);
|
||||
executor1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
executor2.addMessageSelector(new PayloadTypeSelector(String.class));
|
||||
dispatcher.addHandler(executor1);
|
||||
dispatcher.addHandler(executor2);
|
||||
DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint();
|
||||
endpoint1.setHandler(handler1);
|
||||
endpoint1.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1));
|
||||
DefaultMessageEndpoint endpoint2 = new DefaultMessageEndpoint();
|
||||
endpoint2.setHandler(handler2);
|
||||
endpoint2.setConcurrencyPolicy(new ConcurrencyPolicy(1, 1));
|
||||
endpoint1.addMessageSelector(new PayloadTypeSelector(Integer.class));
|
||||
endpoint2.addMessageSelector(new PayloadTypeSelector(String.class));
|
||||
dispatcher.addHandler(endpoint1);
|
||||
dispatcher.addHandler(endpoint2);
|
||||
dispatcher.start();
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
assertEquals("messages should have been dispatched within allotted time", 0, latch.getCount());
|
||||
@@ -485,23 +495,4 @@ public class DefaultMessageDispatcherTests {
|
||||
assertEquals("endpoint2 should have accepted the message", 1, counter2.get());
|
||||
}
|
||||
|
||||
|
||||
private static class TestEndpoint implements MessageHandler {
|
||||
|
||||
private AtomicInteger counter;
|
||||
|
||||
private CountDownLatch latch;
|
||||
|
||||
public TestEndpoint(AtomicInteger counter, CountDownLatch latch) {
|
||||
this.counter = counter;
|
||||
this.latch = latch;
|
||||
}
|
||||
|
||||
public Message<?> handle(Message<?> message) {
|
||||
counter.incrementAndGet();
|
||||
latch.countDown();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,9 @@ public class DefaultMessageEndpointTests {
|
||||
endpoint.setChannelRegistry(channelRegistry);
|
||||
endpoint.setHandler(handler);
|
||||
endpoint.setDefaultOutputChannelName("replyChannel");
|
||||
endpoint.start();
|
||||
endpoint.handle(new StringMessage(1, "test"));
|
||||
endpoint.stop();
|
||||
Message<?> reply = replyChannel.receive(50);
|
||||
assertNotNull(reply);
|
||||
assertEquals("hello test", reply.getPayload());
|
||||
@@ -67,9 +69,11 @@ public class DefaultMessageEndpointTests {
|
||||
DefaultMessageEndpoint endpoint = new DefaultMessageEndpoint();
|
||||
endpoint.setChannelRegistry(channelRegistry);
|
||||
endpoint.setHandler(handler);
|
||||
endpoint.start();
|
||||
StringMessage testMessage = new StringMessage(1, "test");
|
||||
testMessage.getHeader().setReplyChannelName("replyChannel");
|
||||
endpoint.handle(testMessage);
|
||||
endpoint.stop();
|
||||
Message<?> reply = replyChannel.receive(50);
|
||||
assertNotNull(reply);
|
||||
assertEquals("hello test", reply.getPayload());
|
||||
|
||||
@@ -17,20 +17,22 @@
|
||||
package org.springframework.integration.handler;
|
||||
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
|
||||
/**
|
||||
* Factory for {@link MessageHandler} implementations that are useful for testing.
|
||||
* Factory for {@link MessageHandler} implementations that are useful for
|
||||
* testing.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class TestHandlers {
|
||||
public abstract class TestHandlers {
|
||||
|
||||
/**
|
||||
* Create a {@link MessageHandler} that always returns null.
|
||||
*/
|
||||
public static MessageHandler nullHandler() {
|
||||
public final static MessageHandler nullHandler() {
|
||||
return new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
return null;
|
||||
@@ -38,10 +40,22 @@ public class TestHandlers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MessageHandler} that increments the provided counter.
|
||||
*/
|
||||
public final static MessageHandler countingHandler(final AtomicInteger counter) {
|
||||
return new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
counter.incrementAndGet();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MessageHandler} that counts down on the provided latch.
|
||||
*/
|
||||
public static MessageHandler countDownHandler(final CountDownLatch latch) {
|
||||
public final static MessageHandler countDownHandler(final CountDownLatch latch) {
|
||||
return new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
latch.countDown();
|
||||
@@ -50,4 +64,18 @@ public class TestHandlers {
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link MessageHandler} that counts down on the provided latch
|
||||
* and also increments the provided counter.
|
||||
*/
|
||||
public final static MessageHandler countingCountDownHandler(final AtomicInteger counter, final CountDownLatch latch) {
|
||||
return new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
counter.incrementAndGet();
|
||||
latch.countDown();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user