@ToString(of = { "user", "id" })
class MessageTo
{
- final static Pattern SPLIT_ID = Pattern.compile("^([a-z-]+)--([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})$");
+ final static Pattern SPLIT_ID = Pattern.compile("^([a-z-]+)--([0-9]+)$");
private String id;
private Long serial;
private LocalDateTime time;
Matcher matcher = SPLIT_ID.matcher(id);
if (!matcher.matches())
throw new RuntimeException("MessageTo with invalid ID: " + id);
- UUID uuid = UUID.fromString(matcher.group(2));
+ Long messageId = Long.parseLong(matcher.group(2));
String user = matcher.group(1);
- return new Message(Message.MessageKey.of(user, uuid), serial, time, text);
+ return new Message(Message.MessageKey.of(user, messageId), serial, time, text);
}
static MessageTo from(Message message)
{
return
new MessageTo(
- message.getId(),
+ message.getId() + "--" + message.getUsername(),
message.getSerialNumber(),
message.getTimestamp(),
- message.getUsername(),
message.getMessageText());
}
}
package de.juplo.kafka.chat.backend.persistence.storage.mongodb;
-import de.juplo.kafka.chat.backend.api.ChatRoomTo;
import de.juplo.kafka.chat.backend.domain.ChatRoom;
import de.juplo.kafka.chat.backend.persistence.StorageStrategy;
import reactor.core.publisher.Flux;
{
chatroomFlux
.log()
- .doFirst(() ->
- {
- try
- {
- generator.useDefaultPrettyPrinter();
- generator.writeStartArray();
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- })
- .doOnTerminate(() ->
- {
- try
- {
- generator.writeEndArray();
- generator.close();
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
- })
+ .map(ChatRoomTo::from)
.subscribe(chatroom ->
{
- try
- {
- ChatRoomTo chatroomTo = ChatRoomTo.from(chatroom);
- generator.writeObject(chatroomTo);
- writeMessages(chatroomTo, chatroom.getMessages());
- }
- catch (IOException e)
- {
- throw new RuntimeException(e);
- }
});
}
catch (IOException e)