rocketmq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jinrongt...@apache.org
Subject [rocketmq-spring.wiki] branch master updated: Created 发送事务消息 (markdown)
Date Wed, 25 Dec 2019 07:02:20 GMT
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 e579549  Created 发送事务消息 (markdown)
e579549 is described below

commit e579549d2aabd701fc8fe0c913459decac576c42
Author: rongtong <jinrongtong5@163.com>
AuthorDate: Wed Dec 25 15:02:16 2019 +0800

    Created 发送事务消息 (markdown)
---
 ...272\213\345\212\241\346\266\210\346\201\257.md" | 51 ++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git "a/\345\217\221\351\200\201\344\272\213\345\212\241\346\266\210\346\201\257.md"
"b/\345\217\221\351\200\201\344\272\213\345\212\241\346\266\210\346\201\257.md"
new file mode 100644
index 0000000..140f214
--- /dev/null
+++ "b/\345\217\221\351\200\201\344\272\213\345\212\241\346\266\210\346\201\257.md"
@@ -0,0 +1,51 @@
+修改application.properties
+
+```properties
+## application.properties
+rocketmq.name-server=127.0.0.1:9876
+rocketmq.producer.group=my-group
+```
+> 注意:
+> 
+> 请将上述示例配置中的`127.0.0.1:9876`替换成真实RocketMQ的NameServer地址与端口
+
+编写代码
+```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 {
+        try {
+            // Build a SpringMessage for sending in transaction
+            Message msg = MessageBuilder.withPayload(..)...;
+            // In sendMessageInTransaction(), the first parameter transaction name ("test")
+            // must be same with the @RocketMQTransactionListener's member field 'transName'
+            rocketMQTemplate.sendMessageInTransaction("test", "test-topic", msg, null);
+        } catch (MQClientException e) {
+            e.printStackTrace(System.out);
+        }
+    }
+
+    // Define transaction listener with the annotation @RocketMQTransactionListener
+    @RocketMQTransactionListener
+    class TransactionListenerImpl implements RocketMQLocalTransactionListener {
+          @Override
+          public RocketMQLocalTransactionState executeLocalTransaction(Message msg, Object
arg) {
+            // ... local transaction process, return bollback, commit or unknown
+            return RocketMQLocalTransactionState.UNKNOWN;
+          }
+
+          @Override
+          public RocketMQLocalTransactionState checkLocalTransaction(Message msg) {
+            // ... check transaction status and return bollback, commit or unknown
+            return RocketMQLocalTransactionState.COMMIT;
+          }
+    }
+}
+```
\ No newline at end of file


Mime
View raw message