Skip to content

Commit

Permalink
Expose sequenceId in message (apache#1026)
Browse files Browse the repository at this point in the history
  • Loading branch information
sijie authored and merlimat committed Jan 6, 2018
1 parent 558b0b9 commit 4d94413
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,24 @@ public interface Message {
*/
long getEventTime();

/**
* Get the sequence id associated with this message. It is typically set by the applications via
* {@link MessageBuilder#setSequenceId(long)}.
*
* @return sequence id associated with this message.
* @see MessageBuilder#setEventTime(long)
* @since 1.22.0
*/
long getSequenceId();

/**
* Get the producer name who produced this message.
*
* @return producer name who produced this message, null if producer name is not set.
* @since 1.22.0
*/
String getProducerName();

/**
* Check whether the message has a key
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,24 @@ public byte[] getData() {
}
}

@Override
public long getSequenceId() {
checkNotNull(msgMetadataBuilder);
if (msgMetadataBuilder.hasSequenceId()) {
return msgMetadataBuilder.getSequenceId();
}
return -1;
}

@Override
public String getProducerName() {
checkNotNull(msgMetadataBuilder);
if (msgMetadataBuilder.hasProducerName()) {
return msgMetadataBuilder.getProducerName();
}
return null;
}

ByteBuf getDataBuffer() {
return payload;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.client.impl;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;

import java.nio.ByteBuffer;
import org.apache.pulsar.common.api.proto.PulsarApi.MessageMetadata;
import org.testng.annotations.Test;

/**
* Unit test of {@link MessageImpl}.
*/
public class MessageImplTest {

@Test
public void testGetSequenceIdNotAssociated() {
MessageMetadata.Builder builder = MessageMetadata.newBuilder();
ByteBuffer payload = ByteBuffer.wrap(new byte[0]);
MessageImpl msg = MessageImpl.create(builder, payload);

assertEquals(-1, msg.getSequenceId());
}

@Test
public void testGetSequenceIdAssociated() {
MessageMetadata.Builder builder = MessageMetadata.newBuilder()
.setSequenceId(1234);

ByteBuffer payload = ByteBuffer.wrap(new byte[0]);
MessageImpl msg = MessageImpl.create(builder, payload);

assertEquals(1234, msg.getSequenceId());
}

@Test
public void testGetProducerNameNotAssigned() {
MessageMetadata.Builder builder = MessageMetadata.newBuilder();
ByteBuffer payload = ByteBuffer.wrap(new byte[0]);
MessageImpl msg = MessageImpl.create(builder, payload);

assertNull(msg.getProducerName());
}

@Test
public void testGetProducerNameAssigned() {
MessageMetadata.Builder builder = MessageMetadata.newBuilder()
.setProducerName("test-producer");

ByteBuffer payload = ByteBuffer.wrap(new byte[0]);
MessageImpl msg = MessageImpl.create(builder, payload);

assertEquals("test-producer", msg.getProducerName());
}

}

0 comments on commit 4d94413

Please sign in to comment.