superleo-cn commented on a change in pull request #1836: [ISSUE #1833] Support 
messageBuilder for message
URL: https://github.com/apache/rocketmq/pull/1836#discussion_r398283490
 
 

 ##########
 File path: 
common/src/main/java/org/apache/rocketmq/common/message/MessageBuilder.java
 ##########
 @@ -0,0 +1,99 @@
+/*
+ * 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.rocketmq.common.message;
+
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+// user facede of message to send, less pressure about message.
+public class MessageBuilder {
+
+    private String topic;
+    private Map<String, String> properties;
+    private byte[] body;
+
 
 Review comment:
   I reviewed the code and added some new methods to support `flag` and 
`transactionId`, at the same time, I applied these new features into the 
example of quickstart to demonstrate the usage. 
   
   ```java
   /*
    * 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.rocketmq.common.message;
   
   import java.util.Collection;
   import java.util.HashMap;
   import java.util.Map;
   
   // user facede of message to send, less pressure about message.
   public class MessageBuilder {
   
       private String topic;
       private int flag;
       private Map<String, String> properties;
       private byte[] body;
       private String transactionId;
   
       public MessageBuilder() {
       }
   
       public MessageBuilder withFlag(int flag) {
           this.flag = flag;
           return this;
       }
   
       public MessageBuilder withTopic(String topic) {
           this.topic = topic;
           return this;
       }
   
       public MessageBuilder withBody(byte[] body) {
           this.body = body;
           return this;
       }
   
       public MessageBuilder withTransactionId(String transactionId) {
           this.transactionId = transactionId;
           return this;
       }
   
       private MessageBuilder withProperty(final String key, final String 
value) {
           if (null == this.properties) {
               this.properties = new HashMap<String, String>();
           }
   
           this.properties.put(key, value);
           return this;
       }
   
       public MessageBuilder withKeys(String keys) {
           return this.withProperty(MessageConst.PROPERTY_KEYS, keys);
       }
   
       public MessageBuilder withKeys(Collection<String> keys) {
           StringBuilder sb = new StringBuilder();
           for (String k : keys) {
               sb.append(k);
               sb.append(MessageConst.KEY_SEPARATOR);
           }
   
           return this.withKeys(sb.toString().trim());
       }
   
       public MessageBuilder withDelayTimeLevel(int level) {
           return this.withProperty(MessageConst.PROPERTY_DELAY_TIME_LEVEL, 
String.valueOf(level));
       }
   
       public MessageBuilder withTags(String tags) {
           return this.withProperty(MessageConst.PROPERTY_TAGS, tags);
       }
   
       public MessageBuilder withUserProperty(String key, String value) {
           if (MessageConst.STRING_HASH_SET.contains(key)) {
               throw new RuntimeException(String.format(
                       "The Property<%s> is used by system, input another 
please", key));
           }
   
           if (value == null || value.trim().isEmpty()
                   || key == null || key.trim().isEmpty()) {
               throw new IllegalArgumentException(
                       "The name or value of property can not be null or blank 
string!"
               );
           }
   
           return this.withProperty(key, value);
       }
   
       public Message build() {
           Message msg = new Message(topic, body);
           if (this.properties != null) {
               for (Map.Entry<String, String> entry : 
this.properties.entrySet()) {
                   msg.putProperty(entry.getKey(), entry.getValue());
               }
           }
           return msg;
       }
   }
   ```
   
   ```java
   /*
    * 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.rocketmq.example.quickstart;
   
   import org.apache.rocketmq.client.exception.MQClientException;
   import org.apache.rocketmq.client.producer.DefaultMQProducer;
   import org.apache.rocketmq.client.producer.SendResult;
   import org.apache.rocketmq.common.message.Message;
   import org.apache.rocketmq.common.message.MessageBuilder;
   import org.apache.rocketmq.remoting.common.RemotingHelper;
   
   /**
    * This class demonstrates how to send messages to brokers using provided 
{@link DefaultMQProducer}.
    */
   public class Producer {
       public static void main(String[] args) throws MQClientException, 
InterruptedException {
   
           /*
            * Instantiate with a producer group name.
            */
           DefaultMQProducer producer = new 
DefaultMQProducer("please_rename_unique_group_name");
   
           /*
            * Specify name server addresses.
            * <p/>
            *
            * Alternatively, you may specify name server addresses via 
exporting environmental variable: NAMESRV_ADDR
            * <pre>
            * {@code
            * 
producer.setNamesrvAddr("name-server1-ip:9876;name-server2-ip:9876");
            * }
            * </pre>
            */
   
           /*
            * Launch the instance.
            */
           producer.start();
   
           for (int i = 0; i < 1000; i++) {
               try {
   
                   /*
                    * Create a message instance, specifying topic, tag and 
message body.
                    */
                   Message msg = new 
MessageBuilder().withFlag(1).withTopic("TopicTest").withTransactionId("transactionId"
 + i)
                           .withBody(("Hello RocketMQ " + 
i).getBytes(RemotingHelper.DEFAULT_CHARSET))
                           .withTags("TagA").build();
   
                   /*
                    * Call send message to deliver message to one of brokers.
                    */
                   SendResult sendResult = producer.send(msg);
   
                   System.out.printf("%s%n", sendResult);
               } catch (Exception e) {
                   e.printStackTrace();
                   Thread.sleep(1000);
               }
           }
   
           /*
            * Shut down once the producer instance is not longer in use.
            */
           producer.shutdown();
       }
   }
   ```

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


With regards,
Apache Git Services

Reply via email to