INT-2031, INT-2112 Added support to fall back on FIFO semantics to the PriorityChannel if PRIORITY header is not provided, changed type of PRIORITY header to be Long instead of Integer
INT-2031, INT-2112 fixed one more broken test INT-2031 changed the priority type back to Integer INT-2031 added SequenceFallbackComparator INT-2031 changed the name of the comparator from delegatingComparator to targetComparator
This commit is contained in:
committed by
Mark Fisher
parent
90061778a4
commit
2930ad0065
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 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.
|
||||
@@ -17,8 +17,11 @@
|
||||
package org.springframework.integration.channel;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.PriorityBlockingQueue;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
import org.springframework.beans.DirectFieldAccessor;
|
||||
import org.springframework.integration.Message;
|
||||
import org.springframework.integration.MessageHeaders;
|
||||
import org.springframework.integration.util.UpperBound;
|
||||
@@ -28,10 +31,15 @@ import org.springframework.integration.util.UpperBound;
|
||||
* The default comparator is based upon the message header's 'priority'.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class PriorityChannel extends QueueChannel {
|
||||
|
||||
private final UpperBound upperBound;
|
||||
|
||||
private final AtomicLong sequenceCounter = new AtomicLong();
|
||||
|
||||
private static final String SEQUENCE_HEADER_NAME = "__priorityChannelSequence__";
|
||||
|
||||
|
||||
/**
|
||||
@@ -42,8 +50,7 @@ public class PriorityChannel extends QueueChannel {
|
||||
* {@link MessageHeaders#getPriority()}.
|
||||
*/
|
||||
public PriorityChannel(int capacity, Comparator<Message<?>> comparator) {
|
||||
super(new PriorityBlockingQueue<Message<?>>(11,
|
||||
(comparator != null) ? comparator : new MessagePriorityComparator()));
|
||||
super(new PriorityBlockingQueue<Message<?>>(11, new SequenceFallbackComparator(comparator)));
|
||||
this.upperBound = new UpperBound(capacity);
|
||||
}
|
||||
|
||||
@@ -74,32 +81,58 @@ public class PriorityChannel extends QueueChannel {
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
@Override
|
||||
protected boolean doSend(Message<?> message, long timeout) {
|
||||
if (!upperBound.tryAcquire(timeout)) {
|
||||
return false;
|
||||
}
|
||||
Map innerMap = (Map) new DirectFieldAccessor(message.getHeaders()).getPropertyValue("headers");
|
||||
innerMap.put(SEQUENCE_HEADER_NAME, sequenceCounter.incrementAndGet());
|
||||
return super.doSend(message, 0);
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes"})
|
||||
@Override
|
||||
protected Message<?> doReceive(long timeout) {
|
||||
Message<?> message = super.doReceive(timeout);
|
||||
if (message != null) {
|
||||
upperBound.release();
|
||||
return message;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private static class MessagePriorityComparator implements Comparator<Message<?>> {
|
||||
if (message != null) {
|
||||
Map innerMap = (Map) new DirectFieldAccessor(message.getHeaders()).getPropertyValue("headers");
|
||||
innerMap.remove(SEQUENCE_HEADER_NAME);
|
||||
upperBound.release();
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
private static class SequenceFallbackComparator implements Comparator<Message<?>> {
|
||||
|
||||
private final Comparator<Message<?>> targetComparator;
|
||||
|
||||
public SequenceFallbackComparator(Comparator<Message<?>> targetComparator){
|
||||
this.targetComparator = targetComparator;
|
||||
}
|
||||
|
||||
public int compare(Message<?> message1, Message<?> message2) {
|
||||
Integer priority1 = message1.getHeaders().getPriority();
|
||||
Integer priority2 = message2.getHeaders().getPriority();
|
||||
priority1 = priority1 != null ? priority1 : 0;
|
||||
priority2 = priority2 != null ? priority2 : 0;
|
||||
return priority2.compareTo(priority1);
|
||||
int compareResult = 0;
|
||||
if (this.targetComparator != null){
|
||||
compareResult = this.targetComparator.compare(message1, message2);
|
||||
}
|
||||
else {
|
||||
Integer priority1 = message1.getHeaders().getPriority();
|
||||
Integer priority2 = message2.getHeaders().getPriority();
|
||||
|
||||
priority1 = priority1 != null ? priority1 : 0;
|
||||
priority2 = priority2 != null ? priority2 : 0;
|
||||
compareResult = priority2.compareTo(priority1);
|
||||
}
|
||||
|
||||
if (compareResult == 0){
|
||||
Long sequence1 = (Long) message1.getHeaders().get(SEQUENCE_HEADER_NAME);
|
||||
Long sequence2 = (Long) message2.getHeaders().get(SEQUENCE_HEADER_NAME);
|
||||
compareResult = sequence1.compareTo(sequence2);
|
||||
}
|
||||
return compareResult;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2010 the original author or authors.
|
||||
* Copyright 2002-2011 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.
|
||||
@@ -31,6 +31,7 @@ import org.springframework.util.StringUtils;
|
||||
* references) if provided as 'header' sub-elements.
|
||||
*
|
||||
* @author Mark Fisher
|
||||
* @author Oleg Zhurakousky
|
||||
*/
|
||||
public class StandardHeaderEnricherParser extends HeaderEnricherParserSupport {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user