You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by GitBox <gi...@apache.org> on 2018/01/12 09:56:48 UTC

[GitHub] merlimat closed pull request #1054: Populate message properties after building a message

merlimat closed pull request #1054: Populate message properties after building a message
URL: https://github.com/apache/incubator-pulsar/pull/1054
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageImpl.java
index 443f585d2..5edf9e97c 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageImpl.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/MessageImpl.java
@@ -26,6 +26,7 @@
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
 
 import org.apache.pulsar.client.api.Message;
 import org.apache.pulsar.client.api.MessageId;
@@ -58,7 +59,7 @@ static MessageImpl create(MessageMetadata.Builder msgMetadataBuilder, ByteBuffer
         msg.messageId = null;
         msg.cnx = null;
         msg.payload = Unpooled.wrappedBuffer(payload);
-        msg.properties = Collections.emptyMap();
+        msg.properties = null;
         return msg;
     }
 
@@ -75,12 +76,8 @@ static MessageImpl create(MessageMetadata.Builder msgMetadataBuilder, ByteBuffer
         this.payload = Unpooled.copiedBuffer(payload);
 
         if (msgMetadata.getPropertiesCount() > 0) {
-            Map<String, String> properties = Maps.newTreeMap();
-            for (KeyValue entry : msgMetadata.getPropertiesList()) {
-                properties.put(entry.getKey(), entry.getValue());
-            }
-
-            this.properties = Collections.unmodifiableMap(properties);
+            this.properties = Collections.unmodifiableMap(msgMetadataBuilder.getPropertiesList().stream()
+                    .collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue)));
         } else {
             properties = Collections.emptyMap();
         }
@@ -216,13 +213,21 @@ public MessageId getMessageId() {
     }
 
     @Override
-    public Map<String, String> getProperties() {
-        return properties;
+    public synchronized Map<String, String> getProperties() {
+        if (this.properties == null) {
+            if (msgMetadataBuilder.getPropertiesCount() > 0) {
+                this.properties = Collections.unmodifiableMap(msgMetadataBuilder.getPropertiesList().stream()
+                        .collect(Collectors.toMap(KeyValue::getKey, KeyValue::getValue)));
+            } else {
+                this.properties = Collections.emptyMap();
+            }
+        }
+        return this.properties;
     }
 
     @Override
     public boolean hasProperty(String name) {
-        return properties.containsKey(name);
+        return getProperties().containsKey(name);
     }
 
     @Override
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageBuilderTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageBuilderTest.java
index 53cbf5e0d..90b1bd181 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageBuilderTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/MessageBuilderTest.java
@@ -21,10 +21,14 @@
 
 import static org.testng.Assert.assertEquals;
 
+import java.util.Map;
+
 import org.apache.pulsar.client.api.Message;
 import org.apache.pulsar.client.api.MessageBuilder;
 import org.testng.annotations.Test;
 
+import com.google.common.collect.Maps;
+
 /**
  * Unit test of {@link MessageBuilderImpl}.
  */
@@ -60,4 +64,16 @@ public void testBuildMessageWithoutEventTime() {
         assertEquals(0L, msg.getEventTime());
     }
 
+    @Test
+    public void testSetMessageProperties() {
+        MessageBuilder builder = MessageBuilder.create();
+        builder.setContent(new byte[0]);
+        Map<String, String> map = Maps.newHashMap();
+        map.put("key1", "value1");
+        builder.setProperties(map);
+        Message msg = builder.build();
+        assertEquals(map, msg.getProperties());
+        assertEquals("value1", msg.getProperty("key1"));
+    }
+    
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services