Spring integration Spring集成:使用带有比较器的优先级通道';不要打开邮件

Spring integration Spring集成:使用带有比较器的优先级通道';不要打开邮件,spring-integration,comparator,priority-queue,classcastexception,Spring Integration,Comparator,Priority Queue,Classcastexception,我正在使用spring集成优先级通道,我想提供一个定制的比较器 <int:channel id="basedata.csv.income.sorting.channel" datatype="java.io.File"> <int:priority-queue comparator="fileMessageComparator"/> </int:channel> 但我也期望比较器在没有消息类型的情况下工作,有效负载将自动解包: @Component

我正在使用spring集成
优先级通道
,我想提供一个定制的
比较器

<int:channel id="basedata.csv.income.sorting.channel" datatype="java.io.File">
    <int:priority-queue comparator="fileMessageComparator"/>
</int:channel>
但我也期望比较器在没有消息类型的情况下工作,有效负载将自动解包:

@Component
public class FileMessageComparator implements Comparator<File> {
    @Override
    public int compare(File file1, File file2) {
        long fileLength1 = file1.length();
        long fileLength2 = file2.length();
        return Long.valueOf(fileLength2).compareTo(Long.valueOf(fileLength1));
    }
}

在过去的一段时间里,它没有在
比较器中定义消息,这是我在使用
Spring Boot 2.0.0时发现的一个例子。RC1
PriorityChannel需要整个消息(不仅仅是有效负载),所以

你的比较器应该是

public class FileMessageComparator implements Comparator<Message<File>> {..}
公共类FileMessageComparator实现Comparator{..}

干杯

嗨,奥列格,谢谢你的留言。我就是这样让它工作的。但在进行此操作时,比较器依赖于Spring集成。在过去的时间里,您不需要定义消息,如果可以的话,它会解压负载。是的,我对实际问题记忆犹新,但我发现这是一个问题的核心。但也有一线希望。消息不再在Spring集成中,而是被拉入Spring消息中,因此您的比较器不会依赖于SI。我认为书中的示例是错误的。查看代码10年未被触动的指责历史:
2018-02-19 11:35:38.243 ERROR 196 --- [oTaskExecutor-1] o.s.integration.handler.LoggingHandler   : org.springframework.messaging.MessageDeliveryException: failed to send Message to channel 'basedata.csv.income.sorting.channel'; nested exception is java.lang.ClassCastException: org.springframework.integration.channel.PriorityChannel$MessageWrapper cannot be cast to java.io.File, failedMessage=GenericMessage [payload=C:\Users\MHE\AppData\Local\Temp\ziptransformer\5caaddf0-a8e5-dbdf-6148-39ba60d5a4b8\Abrechnungen_Detailzeilen.csv, headers={zip_entryPath=, sequenceNumber=2, zipFileName=20161112, file_name=Abrechnungen_Detailzeilen.csv, sequenceSize=11, correlationId=e7b189fd-7910-d259-f25d-a3bcf4a855a9, file_originalFile=..\data\income\20161112.zip, basedataIndex=-1, id=966873e9-6461-ac1f-e8bb-62d0a9c64d5a, file_relativePath=20161112.zip, timestamp=1519036538145}]
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:464)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:388)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:181)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:160)
at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:47)
at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:108)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:418)
at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:328)
at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:219)
at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115)
at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:141)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.invokeHandler(BroadcastingDispatcher.java:224)
at org.springframework.integration.dispatcher.BroadcastingDispatcher.access$000(BroadcastingDispatcher.java:56)
at org.springframework.integration.dispatcher.BroadcastingDispatcher$1.run(BroadcastingDispatcher.java:204)
at org.springframework.integration.util.ErrorHandlingTaskExecutor.lambda$execute$0(ErrorHandlingTaskExecutor.java:53)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassCastException: org.springframework.integration.channel.PriorityChannel$MessageWrapper cannot be cast to java.io.File
at package.FileMessageComparator.compare(FileMessageComparator.java:1)
at org.springframework.integration.channel.PriorityChannel$SequenceFallbackComparator.compare(PriorityChannel.java:155)
at org.springframework.integration.channel.PriorityChannel$SequenceFallbackComparator.compare(PriorityChannel.java:143)
at java.util.concurrent.PriorityBlockingQueue.siftUpUsingComparator(Unknown Source)
at java.util.concurrent.PriorityBlockingQueue.offer(Unknown Source)
at org.springframework.integration.channel.QueueChannel.doSend(QueueChannel.java:91)
at org.springframework.integration.channel.PriorityChannel.doSend(PriorityChannel.java:128)
at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:438)
... 17 more
public class FileMessageComparator implements Comparator<Message<File>> {..}