在春季云 kafka 示例中更改供应商函数的计划



我正在尝试修改本 Spring Cloud Stream 教程中的示例发送器以更改默认发送间隔。

该示例已更新为使用功能Supplier并删除了@EnableScheduling@Scheduled注释,但我无法弄清楚如何在新版本中更改计划间隔 - 这是我尝试失败的:

@Configuration
@EnableScheduling
public class UsageDetailSender {
private String[] users = {"user1", "user2", "user3", "user4", "user5"};
@Bean
@Scheduled(fixedDelay = 3000)
public Supplier<UsageDetail> sendEvents() {
return () -> {
UsageDetail usageDetail = new UsageDetail();
usageDetail.setUserId(this.users[new Random().nextInt(5)]);
usageDetail.setDuration(new Random().nextInt(300));
usageDetail.setData(new Random().nextInt(700));
return usageDetail;
};
}
}

如何修改退回的供应商以每 3 秒更新一次?

您需要提供轮询器配置属性。

在此处查看文档:https://cloud.spring.io/spring-cloud-static/spring-cloud-stream/3.0.4.RELEASE/reference/html/spring-cloud-stream.html#_polling_configuration_properties

因此,对于您的every 3s,它可能是这样的:

spring.cloud.stream.poller.fixedDelay=3000