2020-10-25 23:52:08 -04:00
|
|
|
package com.baeldung.websockets;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.github.javafaker.Faker;
|
|
|
|
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
|
|
|
|
import org.springframework.scheduling.annotation.Scheduled;
|
|
|
|
|
import org.springframework.stereotype.Controller;
|
2020-11-08 11:38:17 -05:00
|
|
|
import org.springframework.stereotype.Service;
|
2020-10-25 23:52:08 -04:00
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
|
2020-11-08 11:38:17 -05:00
|
|
|
@Service
|
2020-10-25 23:52:08 -04:00
|
|
|
public class ScheduledPushMessages {
|
|
|
|
|
|
|
|
|
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
|
|
|
|
|
|
|
|
|
private final Faker faker;
|
|
|
|
|
|
|
|
|
|
public ScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) {
|
|
|
|
|
this.simpMessagingTemplate = simpMessagingTemplate;
|
|
|
|
|
faker = new Faker();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Scheduled(fixedRate = 5000)
|
|
|
|
|
public void sendMessage() {
|
|
|
|
|
final String time = new SimpleDateFormat("HH:mm").format(new Date());
|
2020-11-08 11:38:17 -05:00
|
|
|
simpMessagingTemplate.convertAndSend("/topic/pushmessages",
|
2020-10-25 23:52:08 -04:00
|
|
|
new OutputMessage("Chuck Norris", faker.chuckNorris().fact(), time));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|