我玩更多的Spring整合,我很感兴趣,但我认为有一个奇怪的行为,我找不到答案。
我有一个使用Queue-Channel的简单应用程序:
<int:channel id="ticketChannel" datatype="ch.elca.prototype.model.Ticket">
<int:queue capacity="1"/>
</int:channel>
我还尝试了具有相同效果的Rendezvous Queue:
<int:channel id="ticketChannel" datatype="ch.elca.prototype.model.Ticket">
<int:rendezvous-queue/>
</int:channel>
以我的理解,现在在那个通道中一次只能移动一条消息。如果你认为你有一个额外的容量,也许是2个。我不知道如何阅读它。但是我可以在不消耗的情况下向那个通道发送四次,这对我来说有点奇怪,那时我不明白容量。
见以下内容:
主要应用:在这里,我流式传输10张票,并为每张票调用openTicket:
public static void main(final String[] args) throws InterruptedException {
try (ConfigurableApplicationContext context = SpringApplication.run(SassSimulatorApplication2.class, args)) {
final TicketGenerator generator = context.getBean(TicketGenerator.class);
final ProblemReporter reporter = context.getBean(ProblemReporter.class);
generator.createTickets().limit(10).forEach(reporter::openTicket);
context.close();
}
}
问题记者:
public class ProblemReporter {
private volatile QueueChannel channel;
public synchronized void openTicket(final Ticket ticket){
final Message<Ticket> build = TicketMessageBuilder.buildMessage(ticket);
boolean send = channel.send(build);
System.out.println("send: " + send);
System.out.println("getQueueSize: " + channel.getQueueSize());
System.out.println("getSendCount: " + channel.getSendCount());
System.out.println("getReceiveCount: " + channel.getReceiveCount());
System.out.println("getSendErrorCount: " + channel.getSendErrorCount());
System.out.println("getRemainingCapacity: " + channel.getRemainingCapacity());
}
@Value("#{ticketChannel}")
public void setChannel(final QueueChannel channel) {
this.channel = channel;
}
}
开始应用程序时,我得到以下内容:
send: true
getQueueSize: 0
getSendCount: 0
getReceiveCount: 0
getSendErrorCount: 0
getRemainingCapacity: 1
send: true
getQueueSize: 0
getSendCount: 0
getReceiveCount: 0
getSendErrorCount: 0
getRemainingCapacity: 1
send: true
getQueueSize: 1
getSendCount: 0
getReceiveCount: 0
getSendErrorCount: 0
getRemainingCapacity: 0
send: true
getQueueSize: 1
getSendCount: 0
getReceiveCount: 0
getSendErrorCount: 0
getRemainingCapacity: 0
我正在使用Spring-Boot 1.3.3、Sprint-Integration4.2.5。释放。我还尝试了使用Spring-Integration4.1.9的Spring-Boot 1.2.8。
这是预期的行为吗???
提前感谢。
看起来像你的channel. send(build,30000);
是针对本地
变量完成的,而不是共享的bean
。我的测试用例如下所示:
QueueChannel channel = new QueueChannel(3);
IntStream.range(0, 4)
.forEach(i -> {
boolean send = channel.send(new GenericMessage<>("test-" + i), 100);
System.out.println("send: " + send);
System.out.println("getQueueSize: " + channel.getQueueSize());
System.out.println("getRemainingCapacity: " + channel.getRemainingCapacity());
});
结果是:
send: true
getQueueSize: 1
getRemainingCapacity: 2
send: true
getQueueSize: 2
getRemainingCapacity: 1
send: true
getQueueSize: 3
getRemainingCapacity: 0
send: false
getQueueSize: 3
getRemainingCapacity: 0
注意:sendCount
(及类似)只能通过@EnableIntegrationMBean导出
或@EnableIntegrationManagement
启用。请参阅参考手册中的管理。
您还可以在框架中找到一些关于该问题的测试用例,例如QueueChannelTest
。