Edward Wedekind :
I am playing with a simple Spring Boot application and RabbitMQ.
However I cannot figure out how to run a method periodically.
Here is my Application class
@SpringBootApplication
public class SampleApp {
@Autowired
Sender sender;
public static void main(String[] args) {
SpringApplication.run(SampleApp.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
sender.sendMessage();
}
}
And the sendMessage method is defined as below
@Scheduled(fixedRate = 3000L)
public void sendMessage() {
log.info("Sending message...");
rabbitTemplate.convertAndSend("my-exchange", "my-routing-key", "TEST MESSAGE");
}
However this method is called only once, I can see only a single line in the console.
What I missed in my code?
Thanks.
CROSP :
In order to work with scheduled tasks in SpingBoot
we need to tell the framework to configure schedulers and threads.
You've missed a single annotation in your main class definition - @EnableScheduling
.
So add it to your class as follow:
@SpringBootApplication
@EnableScheduling
public class SampleApp {
@Autowired
Sender sender;
public static void main(String[] args) {
SpringApplication.run(SampleApp.class, args);
}
@EventListener(ApplicationReadyEvent.class)
public void doSomethingAfterStartup() {
sender.sendMessage();
}
}
And scheduling will start working fine)
Hope this helps.
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=304833&siteId=1