Skip to content
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

Redelivery count field addition to message #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/io/nats/streaming/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class Message {
private long sequence;
private long timestamp;
private boolean redelivered;
private int redeliveryCount;
private int crc32;
private boolean immutable;

Expand All @@ -46,6 +47,7 @@ public class Message {
this.sequence = msgp.getSequence();
this.timestamp = msgp.getTimestamp();
this.redelivered = msgp.getRedelivered();
this.redeliveryCount = msgp.getRedeliveryCount();
this.crc32 = msgp.getCRC32();
immutable = true;
}
Expand Down Expand Up @@ -186,6 +188,15 @@ public boolean isRedelivered() {
return redelivered;
}

/**
* Returns number of times this message has been redelivered to this client's connection.
*
* @return number of times this message was redelivered
*/
public int getRedeliveryCount() {
return redeliveryCount;
}

/**
* Returns the CRC32 checksum for the message.
*
Expand Down Expand Up @@ -241,8 +252,8 @@ public String toString() {
StringBuilder sb = new StringBuilder();
// Date theDate = new Date(TimeUnit.NANOSECONDS.toMillis(getTimestamp()));
sb.append(String.format(
"{Timestamp=%d;Sequence=%d;Redelivered=%b;Subject=%s;Reply=%s;Payload=<",
getTimestamp(), getSequence(), isRedelivered(), getSubject(), getReplyTo()));
"{Timestamp=%d;Sequence=%d;Redelivered=%b;RedeliveryCount=%d;Subject=%s;Reply=%s;Payload=<",
getTimestamp(), getSequence(), isRedelivered(), getRedeliveryCount(), getSubject(), getReplyTo()));
// dateFormat.format(theDate), getSequence(), isRedelivered(), getSubject(), getReplyTo()));

for (int i = 0; i < maxBytes && i < len; i++) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/proto/protocol.proto
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,13 @@ message PubAck {
// Msg struct. Sequence is assigned for global ordering by
// the cluster after the publisher has been acknowledged.
message MsgProto {
uint64 sequence = 1; // globally ordered sequence number for the subject's channel
string subject = 2; // subject
string reply = 3; // optional reply
bytes data = 4; // payload
int64 timestamp = 5; // received timestamp
bool redelivered = 6; // Flag specifying if the message is being redelivered
uint64 sequence = 1; // globally ordered sequence number for the subject's channel
string subject = 2; // subject
string reply = 3; // optional reply
bytes data = 4; // payload
int64 timestamp = 5; // received timestamp
bool redelivered = 6; // Flag specifying if the message is being redelivered
uint32 redeliveryCount = 7; // Number of times the message has been redelivered (count currently not persisted)

uint32 CRC32 = 10; // optional IEEE CRC32
}
Expand Down
7 changes: 5 additions & 2 deletions src/test/java/io/nats/streaming/MessageTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,21 @@ public void testMessageMsgProto() {
final byte[] data = "Hello World".getBytes();
final long sequence = 1234567890;
final boolean redelivered = true;
final int redeliveryCount = 3;
final int crc32 = 9898989;

long timestamp = System.nanoTime();

MsgProto msgp = MsgProto.newBuilder().setSubject(subject).setReply(reply)
.setData(ByteString.copyFrom(data)).setTimestamp(timestamp).setSequence(sequence)
.setRedelivered(redelivered).setCRC32(crc32).build();
.setRedelivered(redelivered).setRedeliveryCount(redeliveryCount).setCRC32(crc32).build();
Message msg = new Message(msgp);
assertEquals(subject, msg.getSubject());
assertEquals(reply, msg.getReplyTo());
assertArrayEquals(data, msg.getData());
assertEquals(sequence, msg.getSequence());
assertEquals(redelivered, msg.isRedelivered());
assertEquals(redeliveryCount, msg.getRedeliveryCount());
assertEquals(crc32, msg.getCrc32());
assertNotNull(msg.getInstant());
}
Expand Down Expand Up @@ -89,13 +91,14 @@ public void testImmutable() {
final byte[] data = "Hello World".getBytes();
final long sequence = 1234567890;
final boolean redelivered = true;
final int redeliveryCount = 3;
final int crc32 = 9898989;

long timestamp = System.nanoTime();

MsgProto msgp = MsgProto.newBuilder().setSubject(subject).setReply(reply)
.setData(ByteString.copyFrom(data)).setTimestamp(timestamp).setSequence(sequence)
.setRedelivered(redelivered).setCRC32(crc32).build();
.setRedelivered(redelivered).setRedeliveryCount(redeliveryCount).setCRC32(crc32).build();
Message msg = new Message(msgp);

boolean exThrown = false;
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/io/nats/streaming/RedeliveryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ public void testHighRedeliveryToQueueSubMoreThanOnce() throws Exception {
}

@Test
public void testRedeliveredFlag() throws Exception {
public void testRedeliveredFlagAndRedeliveryCount() throws Exception {
try (NatsStreamingTestServer srv = new NatsStreamingTestServer(clusterName, false)) {
Options options = new Options.Builder().natsUrl(srv.getURI()).build();
try (StreamingConnection sc = NatsStreaming.connect(clusterName, clientName, options)) {
Expand Down Expand Up @@ -273,6 +273,10 @@ public void testRedeliveredFlag() throws Exception {
fail("Expected a redelivered flag to be set on msg: "
+ msg.getSequence());
}
if ((msg.getSequence() % 2 == 0) && msg.getRedeliveryCount() <= 0) {
fail("Expected a redelivery count to be higher than 0 on msg: "
+ msg.getSequence());
}
}
} catch (IOException e) {
e.printStackTrace();
Expand Down