Quarkus替代Spring @Scheduled(fixedDelay = 10000)的方法

3

我尝试使用@Scheduled(fixedDelay = 10000)实现这个功能,通过导入org.springframework.scheduling.annotation.Scheduled类,但是在构建时出现以下错误:

java.lang.IllegalArgumentException: Invalid @Scheduled method 'monitorRoutesProcessing': 'fixedDelay' not supported

是否有一种方式可以在Quarkus中实现具有固定延迟的调度程序? 我了解我们可以使用@every来实现固定速率。


3
请使用 @io.quarkus.scheduler.Scheduled,具体用法请参考 https://quarkus.io/guides/scheduler-reference。 - undefined
1个回答

1
Quarkus的等效方式是@io.quarkus.scheduler.Scheduled。这里是一个来自官方指南的示例:
package org.acme.scheduler;

import java.util.concurrent.atomic.AtomicInteger;
import jakarta.enterprise.context.ApplicationScoped;
import io.quarkus.scheduler.Scheduled;
import io.quarkus.scheduler.ScheduledExecution;

@ApplicationScoped              
public class CounterBean {

    private AtomicInteger counter = new AtomicInteger();

    public int get() {  
        return counter.get();
    }

    @Scheduled(every="10s")     
    void increment() {
        counter.incrementAndGet(); 
    }

    @Scheduled(cron="0 15 10 * * ?") 
    void cronJob(ScheduledExecution execution) {
        counter.incrementAndGet();
        System.out.println(execution.getScheduledFireTime());
    }

    @Scheduled(cron = "{cron.expr}") 
    void cronJobWithExpressionInConfig() {
       counter.incrementAndGet();
       System.out.println("Cron expression configured in application.properties");
    }
}

另请参阅参考指南以获取更多信息。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接