I deal with the topic of reactive programming. I use WebFlux and R2dbc to access the database. I am developing a small backend for a chat app. So I have some questions on the subject and I would be very happy about suggestions and improvements.
This is service class
@Service
public class MessageService implements IMessageService {
UnicastProcessor<Message> hotProcessor = UnicastProcessor.create();
private final FluxSink<Message> fluxSink = hotProcessor.sink(FluxSink.OverflowStrategy.LATEST);
private final Flux<Message> hotFlux = hotProcessor.publish().autoConnect();
@Autowired
private MessageRepository messageRepository;
public Flux<Message> findAll() {
return this.messageRepository.findAll();
}
public Mono<Message> create(Message nachricht) {
this.fluxSink.next(nachricht);
return this.messageRepository.save(nachricht);
}
// how can I persist messages and to arrange a chat room.
public Flux<Message> finAllMessagesByChatroomId(Long id) {
return hotFlux.filter(m->m.getId().intValue() == id.intValue());
}
}
When should I use UnicastProcessor when DirectProcessor.
Update:
a little correction
public Flux<Message> finAllMessagesByChatroomId(Long id) {
return hotFlux.filter(m->m.getRoom_id == id);
}
But how can I get all the Messages from the database and add it to fluxSink?