Java @Poller-s在Spring集成中是如何工作的?
我正在使用两个Java @Poller-s在Spring集成中是如何工作的?,java,spring,spring-boot,spring-integration,Java,Spring,Spring Boot,Spring Integration,我正在使用两个PollableChannels构建Sprint集成的实现: 常规频道 错误通道 消息从常规通道轮询并处理。如果在处理过程中出现错误(例如,外部服务不可用),则将消息发送到错误通道。它从错误通道重新排队到常规通道,循环继续,直到消息成功处理 其想法是不经常轮询错误通道,以便给处理器一些时间(希望)恢复 我在以下测试中模拟了此工作流: package com.stackoverflow.questions.sipoller; 导入java.time.Duration; 导入java.
PollableChannel
s构建Sprint集成的实现:
package com.stackoverflow.questions.sipoller;
导入java.time.Duration;
导入java.time.Instant;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.List;
导入java.util.concurrent.AtomicInteger;
导入java.util.stream.collector;
导入java.util.stream.IntStream;
导入org.junit.jupiter.api.Test;
导入org.slf4j.Logger;
导入org.slf4j.LoggerFactory;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.beans.factory.annotation.Qualifier;
导入org.springframework.boot.test.context.SpringBootTest;
导入org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入org.springframework.context.annotation.import;
导入org.springframework.integration.annotation.MessageEndpoint;
导入org.springframework.integration.annotation.Poller;
导入org.springframework.integration.annotation.Router;
导入org.springframework.integration.annotation.ServiceActivator;
导入org.springframework.integration.config.EnableIntegration;
导入org.springframework.integration.dsl.MessageChannel;
导入org.springframework.messaging.Message;
导入org.springframework.messaging.PollableChannel;
导入org.springframework.messaging.support.MessageBuilder;
导入静态org.awaibility.awaibility.await;
导入静态org.waitibility.Durations.FIVE_MINUTES;
导入静态org.waitibility.Durations.ONE_一百_毫秒;
@春靴测试
类SiPollerApplicationTests{
私有最终静态记录器日志=LoggerFactory.getLogger(SiPollerApplicationTests.class);
私有最终静态字符串队列\u通道\u正则=“queueChannelRegular”;
私有最终静态字符串队列\u通道\u ERROR=“queueChannelError”;
私有最终静态字符串轮询器\u PERIOD\u REGULAR=“500”//0.5秒
私有最终静态字符串轮询器\u PERIOD\u ERROR=“3000”//3秒
私有最终静态AtomicInteger尝试次数=新AtomicInteger();
private final static AtomicInteger NUMBER_OF_successfuls=new AtomicInteger();
私有最终静态列表尝试_INSTANTS=Collections.synchronizedList(new ArrayList());
@自动连线
@限定符(队列\通道\常规)
专用可轮询信道;
@试验
void testimingofmessageprocessing(){
channelRegular.send(MessageBuilder.withPayload(“测试消息”).build());
等待
.atMost(五分钟)
.与()
.pollInterval(一百毫秒)
.直到(
() -> {
if(成功的次数。intValue()==1){
报告差距();
返回true;
}
返回false;
}
);
}
私有void reportGaps(){
List gaps=IntStream
.range(1,尝试\u INSTANTS.size())
.mapToObj(
i->持续时间
.中间(
尝试获取(i-1),
尝试获取(i)
)
.toMillis()
)
.collect(Collectors.toList());
LOG.info(“尝试之间的间隔(毫秒):{}”,间隔);
}
@配置
@使能集成
@导入(SiPollerApplicationTestEndpoint.class)
静态类SiPollerApplicationTestConfig{
@Bean(名称=队列\通道\常规)
公共可轮询通道queueChannelRegular(){
return MessageChannels.queue(queue_CHANNEL_REGULAR).get();
}
@Bean(名称=队列\通道\错误)
公共可轮询通道queueChannelError(){
return MessageChannels.queue(queue\u CHANNEL\u ERROR).get();
}
@路由器(
inputChannel=队列\通道\错误,
轮询器=@poller(fixedRate=poller\u PERIOD\u错误)
)
公共字符串retryProcessing(){
返回队列\通道\常规;
}
}
@消息端点
静态类SiPollerApplicationTestEndpoint{
@自动连线
@限定符(队列\通道\错误)
专用可轮询信道错误;
@服务激活器(
inputChannel=队列\通道\常规,
轮询器=@poller(fixedRate=poller\u PERIOD\u REGULAR)
)
公共无效handleMessage(消息消息){
//计数和计时尝试
int numberOfAttempts=尝试次数。getAndIncrement();
尝试添加(Instant.now());
//前几次-拒绝处理消息并将其跳转到
//错误通道
如果(尝试次数<5){
channelError.send(消息);
回来
}
//之后-处理消息
成功的数量。getAndIncrement();
}
}
}
pom.xml
依赖项包括:
org.springframework.boot
弹簧靴起动器
org.springframework.integration
弹簧积分
Gaps between attempts (in ms): [1, 0, 0, 0, 0]