package de.juplo.kafka.chat.backend.domain;
+import de.juplo.kafka.chat.backend.domain.exceptions.ChatRoomInactiveException;
import de.juplo.kafka.chat.backend.domain.exceptions.InvalidUsernameException;
import de.juplo.kafka.chat.backend.domain.exceptions.MessageMutationException;
import lombok.extern.slf4j.Slf4j;
private final ChatMessageService service;
private final Clock clock;
- private final int bufferSize;
+ private final int historyLimit;
private Sinks.Many<Message> sink;
+ private volatile boolean active = true;
public ChatRoomData(
Clock clock,
ChatMessageService service,
- int bufferSize)
+ int historyLimit)
{
- log.info("Created ChatRoom with buffer-size {}", bufferSize);
+ log.info("Created ChatRoom with history-limit {}", historyLimit);
this.clock = clock;
this.service = service;
- this.bufferSize = bufferSize;
+ this.historyLimit = historyLimit;
// @RequiredArgsConstructor unfortunately not possible, because
- // the `bufferSize` is not set, if `createSink()` is called
+ // the `historyLimit` is not set, if `createSink()` is called
// from the variable declaration!
- this.sink = createSink();
}
sink.error(new MessageMutationException(existing, text));
}
})
- .switchIfEmpty(
- Mono
+ .switchIfEmpty(active
+ ? Mono
.defer(() -> service.persistMessage(key, LocalDateTime.now(clock), text))
.doOnNext(m ->
{
{
log.warn("Emitting of message failed with {} for {}", result.name(), m);
}
- }));
+ })
+ : Mono.error(new ChatRoomInactiveException(service.getChatRoomId())));
}
synchronized public Flux<Message> listen()
{
- return sink
- .asFlux()
- .doOnCancel(() -> sink = createSink()); // Sink hast to be recreated on auto-cancel!
+ return active
+ ? sink
+ .asFlux()
+ .doOnCancel(() -> sink = createSink()) // Sink hast to be recreated on auto-cancel!
+ : Flux
+ .error(new ChatRoomInactiveException(service.getChatRoomId()));
+
}
public Flux<Message> getMessages()
return service.getMessages(first, last);
}
+ public void activate()
+ {
+ log.info("{} is being activated", service.getChatRoomId());
+ this.sink = createSink();
+ active = true;
+ }
+
+ public void deactivate()
+ {
+ log.info("{} is being deactivated", service.getChatRoomId());
+ active = false;
+ sink.emitComplete(Sinks.EmitFailureHandler.FAIL_FAST);
+ }
+
private Sinks.Many<Message> createSink()
{
return Sinks
.many()
- .multicast()
- .onBackpressureBuffer(bufferSize);
+ .replay()
+ .limit(historyLimit);
}
}