You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by ji...@apache.org on 2020/02/21 07:28:12 UTC

[rocketmq-spring.wiki] branch master updated: Created Request-reply message exchange pattern (markdown)

This is an automated email from the ASF dual-hosted git repository.

jinrongtong pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-spring.wiki.git


The following commit(s) were added to refs/heads/master by this push:
     new 7454287  Created Request-reply message exchange pattern (markdown)
7454287 is described below

commit 7454287f650451870b5ff35cd31f5d3e88bc7d27
Author: rongtong <ji...@163.com>
AuthorDate: Fri Feb 21 15:28:10 2020 +0800

    Created Request-reply message exchange pattern (markdown)
---
 Request-reply-message-exchange-pattern.md | 93 +++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/Request-reply-message-exchange-pattern.md b/Request-reply-message-exchange-pattern.md
new file mode 100644
index 0000000..a8bcab4
--- /dev/null
+++ b/Request-reply-message-exchange-pattern.md
@@ -0,0 +1,93 @@
+
+RocketMQ-Spring provide request/reply semantics support.
+
+- Producer     
+
+Send request messages using sendAndReceice method.
+
+> Note:
+>
+> Synchronous sending needs to indicate the return value type in the parameter of the method.
+>
+> Asynchronous sending needs to indicate the return value type in the callback's interface.
+
+```java
+@SpringBootApplication
+public class ProducerApplication implements CommandLineRunner{
+    @Resource
+    private RocketMQTemplate rocketMQTemplate;
+    
+    public static void main(String[] args){
+        SpringApplication.run(ProducerApplication.class, args);
+    }
+    
+    public void run(String... args) throws Exception {
+        // Send request in sync mode and receive a reply of String type.
+        String replyString = rocketMQTemplate.sendAndReceive(stringRequestTopic, "request string", String.class);
+        
+        // Send request in async mode and receive a reply of User type.
+        rocketMQTemplate.sendAndReceive(objectRequestTopic, new User("requestUserName",(byte) 9), new RocketMQLocalRequestCallback<User>() {
+            @Override public void onSuccess(User message) {
+                System.out.printf("send user object and receive %s %n", message.toString());
+            }
+
+            @Override public void onException(Throwable e) {
+                e.printStackTrace();
+            }
+        }, 5000);
+    }
+    
+    @Data
+    @AllArgsConstructor
+    public class User implements Serializable{
+        private String userName;
+    		private Byte userAge;
+    }
+}
+```
+- Consumer
+
+It is neccessary to implement the RocketMQReplyListener\< T, R \> interface.  T represents the type of received value and R represents the type of returned value.
+
+```java
+@SpringBootApplication
+public class ConsumerApplication{
+    
+    public static void main(String[] args){
+        SpringApplication.run(ConsumerApplication.class, args);
+    }
+    
+     /**
+      * The consumer that replying String
+      */
+      @Service
+      @RocketMQMessageListener(topic = "stringRequestTopic", consumerGroup = "stringRequestConsumer")
+      public class StringConsumerWithReplyString implements RocketMQReplyListener<String, String> {
+        @Override
+        public String onMessage(String message) {
+          System.out.printf("------- StringConsumerWithReplyString received: %s \n", message);
+          return "reply string";
+        }
+      }
+   
+   /**
+    * The consumer that replying Object
+    */
+    @Service
+		@RocketMQMessageListener(topic = "objectRequestTopic", consumerGroup = "objectRequestConsumer")
+    public class ObjectConsumerWithReplyUser implements RocketMQReplyListener<User, User>{
+        public void onMessage(User user) {
+          	System.out.printf("------- ObjectConsumerWithReplyUser received: %s \n", user);
+          	User replyUser = new User("replyUserName",(byte) 10);	
+          	return replyUser;
+        }
+    }
+
+		@Data
+    @AllArgsConstructor
+    public class User implements Serializable{
+        private String userName;
+    		private Byte userAge;
+    }
+}
+```
\ No newline at end of file