繁体   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