通常定时任务我们一般采用spring注解@EnableScheduling来启动,但如何与webflux响应式代码结合实现定时任务呢?下面给出了一个企业内使用的真实案例,希望能帮到你。
@Component
@EnableScheduling
@Slf4j
public class TestTask {
@Resource
private ReactiveStringRedisTemplate redisTemplate;
private final AtomicBoolean isRunning=new AtomicBoolean(false);
private final Sinks.Many<MapRecord<String, Object, Object>> sinks = Sinks.many().unicast().onBackpressureBuffer();
@PostConstruct
public void init(){
//初始化时订阅sinks,等待有数据流进来,这里并发执行流中数据
sinks.asFlux().onErrorResume(err->{
log.error("出现异常",err);
return Mono.empty();
}).parallel().runOn(Schedulers.boundedElastic()).flatMap(record->{
String orderNo=(String) record.getValue().getOrDefault("ord
标签:方案,sinks,webflux,private,EnableScheduling,定时,final,Sinks
From: https://blog.csdn.net/zhuguoli200851/article/details/144772955