java - 如何在 Spring Boot 中运行所需次数的方法?
问题描述
我正在学习 RabbitMq,只是想发送所需的消息次数。我有发送消息以进行交换的 Sender 类,我想重复几次。我正在使用预定的注释,但它有不同的目的,并且与我不匹配,因为它不会停止。
@Scheduled(initialDelay = 1000,fixedDelay = 1500)
public void sendNumbers(){
int index = atomicInteger.getAndIncrement();
UUID uuid = uuids.get(index);
Pair<Integer,Integer> pair = sumPair.get(uuid);
MessagePostProcessor messagePostProcessor = message -> {
MessageProperties messageProperties = message.getMessageProperties();
messageProperties.setCorrelationId(uuid.toString());
messageProperties.setReplyTo(response.getName());
return message;
};
rabbitTemplate.convertAndSend(directExchange.getName(),routingKey,pair,messagePostProcessor);
}
我该怎么做?
解决方案
只需使用for
循环重复您想要的次数。
您可以在ApplicationRunner
.
@Bean
ApplicationRunner runner(RabbitTemplate template) {
return () -> {
for (int i = 0; i < 10; i++) {
...
template.convertAndSend(...)
}
};
推荐阅读
- r - 在多重 glm 泊松回归中绘制置信区间 (IC)
- php - Nginx 默认 / URI 上的 Laravel 不起作用 - 需要“index.php” - Nginx 配置问题?
- c++ - Qt解析对象数组JSON
- ruby-on-rails - 如何在 vue 组件中附加 <%= %> 和登录表单(设计)
- c# - 从 ComponentOne RichTextBox 将 RTF 转换为 HTML?
- reactjs - 哪个本地数据库最适合基于 React + Firestore 的应用程序?
- html - 在 CSS 内容属性中使用 Angular 插值字符串
- javascript - Firebase 无法获取我的身份验证输入值
- python - Tkinter如何阻止网格关闭空单元格
- php - 用PHP在变量中加粗特定文本?