簡體   English   中英

如何使每 5 秒向 rabbitmq 中的隊列發送消息?

[英]how to make every 5 seconds send message to queue in rabbitmq?

我是 springboot 和 rabbitmq 的新手。 如何使每 5 秒在 rabbitmq 中發送一條消息。 我試圖在下面的代碼中做到這一點,但我不確定。 你能幫助我嗎? 謝謝...

示例代碼:

package com.aysenur.sr.producer;


@Service
public class NotificationProducer {

@Value("${sr.rabbit.routing.name}")
private String routingName;

@Value("${sr.rabbit.exchange.name}")
private String exchangeName;


@PostConstruct
public void init() {
    Notification notification = new Notification();
    notification.setNotificationId(UUID.randomUUID().toString());
    notification.setCreatedAt(new Date());
    notification.setMessage("WELCOME TO RABBITMQ");
    notification.setSeen(Boolean.FALSE);

    try {
        Thread t=new Thread();
        t.start();
        sendToQueue(notification);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

}

@Autowired
private RabbitTemplate rabbitTemplate;

public void sendToQueue(Notification notification) throws InterruptedException  {
    System.out.println("Notification Sent ID : " + notification.getNotificationId());
    rabbitTemplate.convertAndSend(exchangeName, routingName, notification);
    Thread.sleep(5000);
     }

}

這可能 go 與您項目的更大目標背道而馳,但您可以刪除構建后方法 + 單獨線程 + 睡眠,然后只需使用帶有“固定延遲”甚至 cron 表達式的 Spring @Scheduled 注釋。 像這樣的東西:

@Value("${sr.rabbit.routing.name}")
private String routingName;

@Value("${sr.rabbit.exchange.name}")
private String exchangeName;


@Scheduled(fixedDelay = 5000, initialDelay = 5000)
public void runSomething() {

    Notification notification = new Notification();
    notification.setNotificationId(UUID.randomUUID().toString());
    notification.setCreatedAt(new Date());
    notification.setMessage("WELCOME TO RABBITMQ");
    notification.setSeen(Boolean.FALSE);

    try {
        sendToQueue(notification);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}

@Autowired
private RabbitTemplate rabbitTemplate;

public void sendToQueue(Notification notification) throws InterruptedException  {
    System.out.println("Notification Sent ID : " + notification.getNotificationId());
    rabbitTemplate.convertAndSend(exchangeName, routingName, notification);
}

這是一個關於@Scheduled 注解的很棒的教程:

不要忘記按照教程中的說明將 @EnableScheduling 添加到您的應用程序中。

service bean 被實例化時,您的代碼只會被調用一次。 在這種情況下,添加創建新威脅的調用實際上並沒有做任何事情,因為您沒有安排針對該威脅的任何工作。

使用 spring 完成此操作的最簡單方法是在 NotificationProducer 方法中使用 (Scheduled)[ https://www.baeldung.com/spring-scheduled-tasks]annotation對方法進行注釋。 這將告訴 spring 安排調用該方法,而無需您做任何額外的工作。


@Scheduled(fixedRate=5000)
public void deliverScheduledMessage(){

    Notification notification = new Notification();
    notification.setNotificationId(UUID.randomUUID().toString());
    notification.setCreatedAt(new Date());
    notification.setMessage("WELCOME TO RABBITMQ");
    notification.setSeen(Boolean.FALSE);

   sendToQueue(notification);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM