Added PriorityChannel (INT-115).
This commit is contained in:
@@ -132,20 +132,23 @@ public class MessageBusTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBothHandlersReceivePublishSubscribeMessage() {
|
||||
public void testBothHandlersReceivePublishSubscribeMessage() throws InterruptedException {
|
||||
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy(true);
|
||||
SimpleChannel inputChannel = new SimpleChannel(dispatcherPolicy);
|
||||
SimpleChannel inputChannel = new SimpleChannel(10, dispatcherPolicy);
|
||||
SimpleChannel outputChannel1 = new SimpleChannel();
|
||||
SimpleChannel outputChannel2 = new SimpleChannel();
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
MessageHandler handler1 = new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
message.getHeader().setReturnAddress("output1");
|
||||
latch.countDown();
|
||||
return message;
|
||||
}
|
||||
};
|
||||
MessageHandler handler2 = new MessageHandler() {
|
||||
public Message<?> handle(Message<?> message) {
|
||||
message.getHeader().setReturnAddress("output2");
|
||||
latch.countDown();
|
||||
return message;
|
||||
}
|
||||
};
|
||||
@@ -157,8 +160,9 @@ public class MessageBusTests {
|
||||
bus.registerHandler("handler2", handler2, new Subscription(inputChannel));
|
||||
bus.start();
|
||||
inputChannel.send(new StringMessage(1, "testing"));
|
||||
Message<?> message1 = outputChannel1.receive(500);
|
||||
Message<?> message2 = outputChannel2.receive(500);
|
||||
latch.await(500, TimeUnit.MILLISECONDS);
|
||||
Message<?> message1 = outputChannel1.receive(100);
|
||||
Message<?> message2 = outputChannel2.receive(100);
|
||||
bus.stop();
|
||||
assertTrue("both handlers should have received and replied to the message",
|
||||
(message1 != null && message2 != null));
|
||||
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* 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.channel;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.Comparator;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
* @author Mark Fisher
|
||||
*/
|
||||
public class PriorityChannelTests {
|
||||
|
||||
@Test
|
||||
public void testCapacityEnforced() {
|
||||
PriorityChannel channel = new PriorityChannel(3);
|
||||
assertTrue(channel.send(new StringMessage("test1"), 0));
|
||||
assertTrue(channel.send(new StringMessage("test2"), 0));
|
||||
assertTrue(channel.send(new StringMessage("test3"), 0));
|
||||
assertFalse(channel.send(new StringMessage("test4"), 0));
|
||||
channel.receive(0);
|
||||
assertTrue(channel.send(new StringMessage("test5")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultComparator() {
|
||||
PriorityChannel channel = new PriorityChannel(5);
|
||||
Message<?> priority1 = createPriorityMessage(1);
|
||||
Message<?> priority2 = createPriorityMessage(2);
|
||||
Message<?> priority3 = createPriorityMessage(3);
|
||||
Message<?> priority4 = createPriorityMessage(4);
|
||||
Message<?> priority5 = createPriorityMessage(5);
|
||||
channel.send(priority4);
|
||||
channel.send(priority3);
|
||||
channel.send(priority5);
|
||||
channel.send(priority1);
|
||||
channel.send(priority2);
|
||||
assertEquals("test-1", channel.receive(0).getPayload());
|
||||
assertEquals("test-2", channel.receive(0).getPayload());
|
||||
assertEquals("test-3", channel.receive(0).getPayload());
|
||||
assertEquals("test-4", channel.receive(0).getPayload());
|
||||
assertEquals("test-5", channel.receive(0).getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomComparator() {
|
||||
PriorityChannel channel = new PriorityChannel(5, null, new StringPayloadComparator());
|
||||
Message<?> messageA = new StringMessage("A");
|
||||
Message<?> messageB = new StringMessage("B");
|
||||
Message<?> messageC = new StringMessage("C");
|
||||
Message<?> messageD = new StringMessage("D");
|
||||
Message<?> messageE = new StringMessage("E");
|
||||
channel.send(messageC);
|
||||
channel.send(messageA);
|
||||
channel.send(messageE);
|
||||
channel.send(messageD);
|
||||
channel.send(messageB);
|
||||
assertEquals("A", channel.receive(0).getPayload());
|
||||
assertEquals("B", channel.receive(0).getPayload());
|
||||
assertEquals("C", channel.receive(0).getPayload());
|
||||
assertEquals("D", channel.receive(0).getPayload());
|
||||
assertEquals("E", channel.receive(0).getPayload());
|
||||
}
|
||||
|
||||
|
||||
private static Message<?> createPriorityMessage(int priority) {
|
||||
Message<?> message = new StringMessage("test-" + priority);
|
||||
message.getHeader().setPriority(priority);
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
private static class StringPayloadComparator implements Comparator<Message<?>> {
|
||||
|
||||
public int compare(Message<?> message1, Message<?> message2) {
|
||||
String s1 = (String) message1.getPayload();
|
||||
String s2 = (String) message2.getPayload();
|
||||
return s1.compareTo(s2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,7 +39,7 @@ public class ChannelPollingMessageRetrieverTests {
|
||||
public void testSingleMessagePerRetrieval() {
|
||||
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
|
||||
dispatcherPolicy.setReceiveTimeout(0);
|
||||
MessageChannel channel = new SimpleChannel(dispatcherPolicy);
|
||||
MessageChannel channel = new SimpleChannel(5, dispatcherPolicy);
|
||||
ChannelPollingMessageRetriever retriever = new ChannelPollingMessageRetriever(channel);
|
||||
Collection<Message<?>> results = retriever.retrieveMessages();
|
||||
assertTrue(results.isEmpty());
|
||||
@@ -58,7 +58,7 @@ public class ChannelPollingMessageRetrieverTests {
|
||||
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
|
||||
dispatcherPolicy.setReceiveTimeout(0);
|
||||
dispatcherPolicy.setMaxMessagesPerTask(2);
|
||||
MessageChannel channel = new SimpleChannel(dispatcherPolicy);
|
||||
MessageChannel channel = new SimpleChannel(5, dispatcherPolicy);
|
||||
ChannelPollingMessageRetriever retriever = new ChannelPollingMessageRetriever(channel);
|
||||
Collection<Message<?>> results = retriever.retrieveMessages();
|
||||
assertTrue(results.isEmpty());
|
||||
@@ -80,7 +80,7 @@ public class ChannelPollingMessageRetrieverTests {
|
||||
DispatcherPolicy dispatcherPolicy = new DispatcherPolicy();
|
||||
dispatcherPolicy.setReceiveTimeout(0);
|
||||
dispatcherPolicy.setMaxMessagesPerTask(1);
|
||||
MessageChannel channel = new SimpleChannel(dispatcherPolicy);
|
||||
MessageChannel channel = new SimpleChannel(5, dispatcherPolicy);
|
||||
ChannelPollingMessageRetriever retriever = new ChannelPollingMessageRetriever(channel);
|
||||
Collection<Message<?>> results = retriever.retrieveMessages();
|
||||
assertTrue(results.isEmpty());
|
||||
|
||||
@@ -81,7 +81,7 @@ public class DefaultMessageDispatcherTests {
|
||||
final CountDownLatch latch = new CountDownLatch(2);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(true));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true));
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()));
|
||||
@@ -125,7 +125,7 @@ public class DefaultMessageDispatcherTests {
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(true));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true));
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
ConcurrentHandler inactiveHandler = new ConcurrentHandler(handler2, createExecutor());
|
||||
@@ -157,7 +157,7 @@ public class DefaultMessageDispatcherTests {
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
MessageHandler handler3 = TestHandlers.countingCountDownHandler(counter3, latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(true));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true));
|
||||
channel.getDispatcherPolicy().setRejectionLimit(2);
|
||||
channel.getDispatcherPolicy().setRetryInterval(3);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
@@ -188,7 +188,7 @@ public class DefaultMessageDispatcherTests {
|
||||
final CountDownLatch latch = new CountDownLatch(3);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(true));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true));
|
||||
channel.getDispatcherPolicy().setRejectionLimit(2);
|
||||
channel.getDispatcherPolicy().setRetryInterval(3);
|
||||
channel.getDispatcherPolicy().setShouldFailOnRejectionLimit(false);
|
||||
@@ -213,7 +213,7 @@ public class DefaultMessageDispatcherTests {
|
||||
final CountDownLatch latch = new CountDownLatch(4);
|
||||
MessageHandler handler1 = TestHandlers.rejectingCountDownHandler(latch);
|
||||
MessageHandler handler2 = TestHandlers.rejectingCountDownHandler(latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(false));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(false));
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
channel.getDispatcherPolicy().setRejectionLimit(2);
|
||||
@@ -287,7 +287,7 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcherPolicy.setRejectionLimit(2);
|
||||
dispatcherPolicy.setRetryInterval(3);
|
||||
dispatcherPolicy.setShouldFailOnRejectionLimit(false);
|
||||
SimpleChannel channel = new SimpleChannel(dispatcherPolicy);
|
||||
SimpleChannel channel = new SimpleChannel(25, dispatcherPolicy);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()) {
|
||||
@@ -341,7 +341,7 @@ public class DefaultMessageDispatcherTests {
|
||||
dispatcherPolicy.setRejectionLimit(5);
|
||||
dispatcherPolicy.setRetryInterval(3);
|
||||
dispatcherPolicy.setShouldFailOnRejectionLimit(false);
|
||||
SimpleChannel channel = new SimpleChannel(dispatcherPolicy);
|
||||
SimpleChannel channel = new SimpleChannel(25, dispatcherPolicy);
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
dispatcher.addHandler(new ConcurrentHandler(handler1, createExecutor()) {
|
||||
@@ -451,7 +451,7 @@ public class DefaultMessageDispatcherTests {
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
MessageHandler handler1 = TestHandlers.countingCountDownHandler(counter1, latch);
|
||||
MessageHandler handler2 = TestHandlers.countingCountDownHandler(counter2, latch);
|
||||
SimpleChannel channel = new SimpleChannel(new DispatcherPolicy(true));
|
||||
SimpleChannel channel = new SimpleChannel(5, new DispatcherPolicy(true));
|
||||
channel.send(new StringMessage(1, "test"));
|
||||
DefaultMessageDispatcher dispatcher = new DefaultMessageDispatcher(channel, scheduler);
|
||||
DefaultMessageEndpoint endpoint1 = new DefaultMessageEndpoint(handler1);
|
||||
|
||||
Reference in New Issue
Block a user