-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added begin timestamp to reader #5
base: limit-store
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ | |
|
||
import java.io.IOException; | ||
import java.nio.ByteBuffer; | ||
import java.time.LocalDateTime; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
|
||
|
@@ -27,17 +28,20 @@ public class ReaderService { | |
private final MessageDeserializer messageDeserializer; | ||
private final String subject; | ||
private final MessageStore messageStore; | ||
private final LocalDateTime beginTimestamp; | ||
|
||
private final Executor executorService = Executors.newSingleThreadExecutor(); | ||
|
||
public ReaderService(String natsServerUrl, | ||
MessageDeserializer messageDeserializer, | ||
String subject, | ||
MessageStore messageStore) { | ||
MessageStore messageStore, | ||
LocalDateTime beginTimestamp) { | ||
this.natsServerUrl = natsServerUrl; | ||
this.messageDeserializer = messageDeserializer; | ||
this.subject = subject; | ||
this.messageStore = messageStore; | ||
this.beginTimestamp = beginTimestamp; | ||
} | ||
|
||
@EventListener(ApplicationReadyEvent.class) | ||
|
@@ -93,21 +97,35 @@ private void continuouslyReadMessages( | |
Message message = subscription.nextMessage(100); | ||
// Print the message | ||
if (message != null) { | ||
try { | ||
DeserializedMessage deserializedMessage = | ||
messageDeserializer.deserializeMessage(ByteBuffer.wrap(message.getData())); | ||
ReadMessage msg = | ||
new ReadMessage( | ||
message.getSubject(), | ||
deserializedMessage.name(), | ||
deserializedMessage.content(), | ||
message.metaData().timestamp().toLocalDateTime()); | ||
messageStore.add(msg); | ||
LocalDateTime messageTimestamp = message | ||
.metaData() | ||
.timestamp() | ||
.toLocalDateTime(); | ||
|
||
if (messageTimestamp.isAfter(beginTimestamp)) { | ||
deserializeMessage(messageDeserializer, message, messageTimestamp); | ||
} else { | ||
LOG.warn("Timestamp from message {}, smaller then begin timestamp {}, message {} will not be process", messageTimestamp, beginTimestamp, message.getSID()); | ||
message.ack(); | ||
} catch (Exception e) { | ||
LOG.warn("Unable to deserialize message", e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void deserializeMessage(MessageDeserializer messageDeserializer, Message message, LocalDateTime timestamp) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about using |
||
try { | ||
DeserializedMessage deserializedMessage = | ||
messageDeserializer.deserializeMessage(ByteBuffer.wrap(message.getData())); | ||
ReadMessage msg = | ||
new ReadMessage( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how about adding SID here for it to be displayed on frontend ? |
||
message.getSubject(), | ||
deserializedMessage.name(), | ||
deserializedMessage.content(), | ||
timestamp); | ||
messageStore.add(msg); | ||
message.ack(); | ||
} catch (Exception e) { | ||
LOG.warn("Unable to deserialize message", e); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this ever be null ?