简体   繁体   中英

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

I'm new for springboot and rabbitmq. how to make every 5 seconds send a message in rabbitmq. I tried to do it in the code below but I'm not sure. Can you help me? thanks...

Sample code:

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

}

This may go against the greater goal of your project, but you could remove the post-construct method + separate thread + sleep, and then simply use the Spring @Scheduled annotation with a 'fixed delay' or perhaps even a cron expression. Something like this:

@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);
}

Here is a great tutorial on the @Scheduled annotation:

Don't forget to add the @EnableScheduling to your application as mentioned in the tutorial.

Your code is only being called once, when the service bean is being instantiated. Adding the call to create a new threat does not actually do anything in this case because you are not scheduling any work on that threat.

The simplest way to accomplish this with spring is to use annotate a method in your NotificationProducer method with the (Scheduled)[ https://www.baeldung.com/spring-scheduled-tasks]annotation . This will tell spring to schedule calling that method without the need for you to do any additional work.


@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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM