X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fimplementation%2Finmemory%2FSimpleChatHomeService.java;h=8e3cc4305b3df480059eb4d934996f8c36f4bd7e;hb=900422dccb5a92fbceac34caa5e614b0d7f05ad7;hp=5c3fe2e5e1d8c3ac60b9313dcb6c3f7dfc8ffe0e;hpb=dc53848961fd5622f777621fd4140cb01c2c8739;p=demos%2Fkafka%2Fchat diff --git a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java index 5c3fe2e5..8e3cc430 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java +++ b/src/main/java/de/juplo/kafka/chat/backend/implementation/inmemory/SimpleChatHomeService.java @@ -18,24 +18,24 @@ public class SimpleChatHomeService implements ChatHomeService private final Map chatRoomInfo; private final Map chatRoomData; private final Clock clock; - private final int bufferSize; + private final int historyLimit; public SimpleChatHomeService( Clock clock, - int bufferSize) + int historyLimit) { this( null, clock, - bufferSize); + historyLimit); } public SimpleChatHomeService( Integer shard, Clock clock, - int bufferSize) + int historyLimit) { log.debug("Creating SimpleChatHomeService"); @@ -43,12 +43,15 @@ public class SimpleChatHomeService implements ChatHomeService this.chatRoomInfo = new HashMap<>(); this.chatRoomData = new HashMap<>(); this.clock = clock; - this.bufferSize = bufferSize; + this.historyLimit = historyLimit; } Mono restore(StorageStrategy storageStrategy) { + chatRoomInfo.clear(); + chatRoomData.clear(); + return storageStrategy .readChatRoomInfo() .filter(info -> @@ -73,29 +76,32 @@ public class SimpleChatHomeService implements ChatHomeService new InMemoryChatMessageService(chatRoomId); chatRoomInfo.put(chatRoomId, info); - chatRoomData.put( - info.getId(), + ChatRoomData chatRoomData = new ChatRoomData( clock, chatMessageService, - bufferSize)); + historyLimit); + chatRoomData.activate(); + this.chatRoomData.put(info.getId(), chatRoomData); return chatMessageService.restore(storageStrategy); }) - .then() - .doOnSuccess(empty -> log.info("Restored {}", this)) - .doOnError(throwable -> log.error("Could not restore {}", this)); + .count() + .doOnSuccess(count -> log.info("Restored {} with {} chat-rooms", this, count)) + .doOnError(throwable -> log.error("Could not restore {}", this)) + .then(); } @Override public Mono createChatRoom(UUID id, String name) { - log.info("Creating ChatRoom with buffer-size {}", bufferSize); + log.info("Creating ChatRoom with history-limit {}", historyLimit); ChatMessageService service = new InMemoryChatMessageService(id); ChatRoomInfo chatRoomInfo = new ChatRoomInfo(id, name, shard); this.chatRoomInfo.put(id, chatRoomInfo); - ChatRoomData chatRoomData = new ChatRoomData(clock, service, bufferSize); + ChatRoomData chatRoomData = new ChatRoomData(clock, service, historyLimit); + chatRoomData.activate(); this.chatRoomData.put(id, chatRoomData); return Mono.just(chatRoomInfo); }