Added the MessagePriority enum (INT-141).
This commit is contained in:
@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessageHeader;
|
||||
import org.springframework.integration.message.MessagePriority;
|
||||
|
||||
/**
|
||||
* A message channel that prioritizes messages based on a {@link Comparator}.
|
||||
@@ -97,8 +98,8 @@ public class PriorityChannel extends BaseBlockingQueueChannel {
|
||||
private static class MessagePriorityComparator implements Comparator<Message<?>> {
|
||||
|
||||
public int compare(Message<?> message1, Message<?> message2) {
|
||||
Integer priority1 = message1.getHeader().getPriority();
|
||||
Integer priority2 = message2.getHeader().getPriority();
|
||||
MessagePriority priority1 = message1.getHeader().getPriority();
|
||||
MessagePriority priority2 = message2.getHeader().getPriority();
|
||||
return priority1.compareTo(priority2);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -46,7 +46,7 @@ public class MessageHeader {
|
||||
|
||||
private volatile int sequenceSize = 1;
|
||||
|
||||
private volatile int priority = 0;
|
||||
private volatile MessagePriority priority = MessagePriority.NORMAL;
|
||||
|
||||
private final Properties properties = new Properties();
|
||||
|
||||
@@ -108,11 +108,11 @@ public class MessageHeader {
|
||||
this.sequenceSize = sequenceSize;
|
||||
}
|
||||
|
||||
public int getPriority() {
|
||||
public MessagePriority getPriority() {
|
||||
return this.priority;
|
||||
}
|
||||
|
||||
public void setPriority(int priority) {
|
||||
public void setPriority(MessagePriority priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
* 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.message;
|
||||
|
||||
/**
|
||||
* An enumeration of the possible values for a message's priority.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @see MessageHeader#getPriority()
|
||||
* @see MessageHeader#setPriority(MessagePriority)
|
||||
*/
|
||||
public enum MessagePriority {
|
||||
|
||||
HIGHEST,
|
||||
HIGH,
|
||||
NORMAL,
|
||||
LOW,
|
||||
LOWEST
|
||||
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import java.util.Comparator;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.integration.message.Message;
|
||||
import org.springframework.integration.message.MessagePriority;
|
||||
import org.springframework.integration.message.StringMessage;
|
||||
|
||||
/**
|
||||
@@ -46,21 +47,21 @@ public class PriorityChannelTests {
|
||||
@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);
|
||||
Message<?> priority1 = createPriorityMessage(MessagePriority.HIGHEST);
|
||||
Message<?> priority2 = createPriorityMessage(MessagePriority.HIGH);
|
||||
Message<?> priority3 = createPriorityMessage(MessagePriority.NORMAL);
|
||||
Message<?> priority4 = createPriorityMessage(MessagePriority.LOW);
|
||||
Message<?> priority5 = createPriorityMessage(MessagePriority.LOWEST);
|
||||
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());
|
||||
assertEquals("test-HIGHEST", channel.receive(0).getPayload());
|
||||
assertEquals("test-HIGH", channel.receive(0).getPayload());
|
||||
assertEquals("test-NORMAL", channel.receive(0).getPayload());
|
||||
assertEquals("test-LOW", channel.receive(0).getPayload());
|
||||
assertEquals("test-LOWEST", channel.receive(0).getPayload());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -84,7 +85,7 @@ public class PriorityChannelTests {
|
||||
}
|
||||
|
||||
|
||||
private static Message<?> createPriorityMessage(int priority) {
|
||||
private static Message<?> createPriorityMessage(MessagePriority priority) {
|
||||
Message<?> message = new StringMessage("test-" + priority);
|
||||
message.getHeader().setPriority(priority);
|
||||
return message;
|
||||
|
||||
@@ -36,6 +36,7 @@ import org.springframework.integration.handler.MessageHandler;
|
||||
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.scheduling.SimpleMessagingTaskScheduler;
|
||||
|
||||
@@ -211,11 +212,11 @@ public class ChannelParserTests {
|
||||
"priorityChannelParserTests.xml", this.getClass());
|
||||
MessageChannel channel = (MessageChannel) context.getBean("priorityChannelWithDefaultComparator");
|
||||
Message<?> lowPriorityMessage = new StringMessage("low");
|
||||
lowPriorityMessage.getHeader().setPriority(777);
|
||||
lowPriorityMessage.getHeader().setPriority(MessagePriority.LOW);
|
||||
Message<?> midPriorityMessage = new StringMessage("mid");
|
||||
midPriorityMessage.getHeader().setPriority(77);
|
||||
midPriorityMessage.getHeader().setPriority(MessagePriority.NORMAL);
|
||||
Message<?> highPriorityMessage = new StringMessage("high");
|
||||
highPriorityMessage.getHeader().setPriority(7);
|
||||
highPriorityMessage.getHeader().setPriority(MessagePriority.HIGH);
|
||||
channel.send(lowPriorityMessage);
|
||||
channel.send(highPriorityMessage);
|
||||
channel.send(midPriorityMessage);
|
||||
|
||||
Reference in New Issue
Block a user