Java 如何使rabbitmq中每5秒向队列发送一次消息?

Java 如何使rabbitmq中每5秒向队列发送一次消息?,java,spring-boot,spring-rabbit,Java,Spring Boot,Spring Rabbit,我是springboot和rabbitmq的新手。 如何使每5秒在rabbitmq中发送一条消息。 我试着在下面的代码中这样做,但我不确定。你能帮助我吗?谢谢 示例代码: package com.aysenur.sr.producer; @Service public class NotificationProducer { @Value("${sr.rabbit.routing.name}") private String routingName; @Value("${sr.rabbi

我是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);
     }

}

服务
bean被实例化时,您的代码只被调用一次。在这种情况下,添加创建新威胁的调用实际上不会做任何事情,因为您没有安排针对该威胁的任何工作

使用spring实现这一点的最简单方法是使用(Scheduled)[注释NotificationProducer方法中的一个方法。这将告诉spring安排调用该方法,而无需您做任何额外的工作


@计划(固定利率=5000)
公共无效传递者ScheduledMessage(){
通知通知=新通知();
notification.setNotificationId(UUID.randomUUID().toString());
notification.setCreatedAt(新日期());
notification.setMessage(“欢迎来到RABBITMQ”);
notification.setSeen(Boolean.FALSE);
发送队列(通知);
}

这可能与项目的更大目标背道而驰,但您可以删除构造后方法+独立线程+睡眠,然后简单地使用带有“固定延迟”的Spring@Scheduled注释,甚至可能是一个cron表达式。类似于:

@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 annotation的精彩教程:


不要忘记将@EnableScheduling添加到教程中提到的应用程序中。

试试spring scheduler,quartz?您可以使用spring scheduler来回答这个问题。非常感谢。这是真的。很好。您能接受这篇文章作为答案吗?谢谢!您永远不应该使用初始值设定方法发送到RabbitMQ例如
@PostConstruct
。在应用程序上下文的生命周期的早期,这非常重要,可能会导致不良的副作用。