阿里云消息队列RocketMQ发送/订阅事务消息SDK代码

发送事务消息
发送事务消息代码示例如下:
 
package com.chinamobile.tuxedo.sdk.example;
 
import com.chinamobile.tuxedo.sdk.api.Message;
import com.chinamobile.tuxedo.sdk.api.PropertyKeyConst;
import com.chinamobile.tuxedo.sdk.api.SendResult;
import com.chinamobile.tuxedo.sdk.api.TuxeFactory;
import com.chinamobile.tuxedo.sdk.api.transaction.TransactionProducer;
import com.chinamobile.tuxedo.sdk.shade.client.producer.LocalTransactionState;
import com.chinamobile.tuxedo.sdk.shade.client.producer.TransactionListener;
import com.chinamobile.tuxedo.sdk.shade.common.message.MessageExt;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
 
public class TranscationProducerTest {
    public static void main(String[] args) throws InterruptedException {
        Properties properties = new Properties();
        //  控制台创建的Group
        properties.put(PropertyKeyConst.GROUP_ID, "xxx");
        //  控制台创建的实例
        properties.put(PropertyKeyConst.INSTANCE_ID, "MQ_INST_xxx");
        // 启用鉴权认证
        properties.setProperty(PropertyKeyConst.AuthenticationRequired, "true");
        // 开启保存消息轨迹
        properties.put(PropertyKeyConst.MsgTraceSwitch, true);
        // AccessKey 身份验证,在移动云管理控制台创建
        properties.put(PropertyKeyConst.AccessKey, "xxx");
        // SecretKey 身份验证,在移动云管理控制台创建
        properties.put(PropertyKeyConst.SecretKey, "xxx");
        // 设置发送超时时间,单位毫秒
        properties.put(PropertyKeyConst.SendMsgTimeoutMillis, 30000);
        // 设置公网接入点
        properties.put(PropertyKeyConst.TuxeAddr, "xxx");
        TransactionProducer producer = TuxeFactory.createTransactionProducer(properties, new TransactionListener() {
 
            public LocalTransactionState executeLocalTransaction(
                com.chinamobile.tuxedo.sdk.shade.common.message.Message msg, Object arg) {
                System.out.println("execute local transaction");
                return LocalTransactionState.COMMIT_MESSAGE;
            }
 
            @Override
            public LocalTransactionState checkLocalTransaction(MessageExt msg) {
                int transaction_check_times = Integer.parseInt(msg.getUserProperty("TRANSACTION_CHECK_TIMES"));
                if (transaction_check_times >= 3) {
                    System.out.println("Local transaction succeed. return COMMIT_MESSAGE");
                    return LocalTransactionState.COMMIT_MESSAGE;
                }
                System.out.println("Local transaction hasn't succeeded. return UNKNOW");
                return LocalTransactionState.UNKNOW;
            }
        });
        // 在发送消息前,必须调用 start 方法来启动 Producer,只需调用一次即可
        producer.start();
 
        Message msg = new Message("xxx", "xyzs3", "xyzs3", "hi 3".getBytes());
        try {
            SendResult sendResult = producer.send(msg, null);
            System.out.println("send transaction message succeed. msg=" + msg.toString());
            System.out.println("msgId:->" + sendResult.getMessageId());
        } catch (Exception e) {
            // 消息发送失败,需要进行重试处理,可重新发送这条消息或持久化这条数据进行补偿处理
            System.out.println("Send mq message failed. Topic is:" + msg.getTopic());
            e.printStackTrace();
        }
        // demo example 防止进程退出(实际使用不需要这样)
        TimeUnit.MILLISECONDS.sleep(Integer.MAX_VALUE);
        producer.shutdown();
    }
}