Added push messages using an interval Flux.
This commit is contained in:
parent
18954efcee
commit
66fff973ae
@ -18,6 +18,10 @@
|
|||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-starter-websocket</artifactId>
|
<artifactId>spring-boot-starter-websocket</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>io.projectreactor</groupId>
|
||||||
|
<artifactId>reactor-core</artifactId>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.github.javafaker</groupId>
|
<groupId>com.github.javafaker</groupId>
|
||||||
<artifactId>javafaker</artifactId>
|
<artifactId>javafaker</artifactId>
|
||||||
|
@ -0,0 +1,32 @@
|
|||||||
|
package com.baeldung.websockets;
|
||||||
|
|
||||||
|
import com.github.javafaker.Faker;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.messaging.simp.SimpMessagingTemplate;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import reactor.core.publisher.Flux;
|
||||||
|
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.time.Duration;
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ReactiveScheduledPushMessages implements InitializingBean {
|
||||||
|
|
||||||
|
private final SimpMessagingTemplate simpMessagingTemplate;
|
||||||
|
|
||||||
|
private final Faker faker;
|
||||||
|
|
||||||
|
public ReactiveScheduledPushMessages(SimpMessagingTemplate simpMessagingTemplate) {
|
||||||
|
this.simpMessagingTemplate = simpMessagingTemplate;
|
||||||
|
this.faker = new Faker();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
Flux.interval(Duration.ofSeconds(4L))
|
||||||
|
.map((n) -> new OutputMessage(faker.backToTheFuture().character(), faker.backToTheFuture().quote(),
|
||||||
|
new SimpleDateFormat("HH:mm").format(new Date())))
|
||||||
|
.subscribe(message -> simpMessagingTemplate.convertAndSend("/topic/messages", message));
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user