天天看點

阿裡雲Rocket MQ Http .NET SDK使用Demo

NET Framework SDK使用

1、SDK 下載下傳

下載下傳 位址
阿裡雲Rocket MQ Http .NET SDK使用Demo

2、Zip包解壓擷取SDK

阿裡雲Rocket MQ Http .NET SDK使用Demo

3、建立項目并複制SDK到項目下面

阿裡雲Rocket MQ Http .NET SDK使用Demo

4、加入項目

阿裡雲Rocket MQ Http .NET SDK使用Demo
阿裡雲Rocket MQ Http .NET SDK使用Demo

5、Code Sample

using System;
using System.Collections.Generic;
using Aliyun.MQ.Model;
using Aliyun.MQ.Model.Exp;
using Aliyun.MQ;
using System.Threading;

namespace RocketMQHttpDemoTest123
{
    class Program
    {
        // 設定HTTP接入域名(此處以公共雲生産環境為例)
        private const string _endpoint = "http://184821781*********.mqrest.cn-qingdao-public.aliyuncs.com";
        // AccessKey 阿裡雲身份驗證,在阿裡雲伺服器管理控制台建立
        private const string _accessKeyId = "LTAIOZZg*********";
        // SecretKey 阿裡雲身份驗證,在阿裡雲伺服器管理控制台建立
        private const string _secretAccessKey = "v7CjUJCMk7j9aK****************";
        // 所屬的 Topic
        private const string _topicName = "***********";
        // Topic所屬執行個體ID,預設執行個體為空
        private const string _instanceId = "MQ_INST_184821**********_BcLPQ2p0";
        // 您在控制台建立的 Consumer ID(Group ID)
        private const string _groupId = "GID_Http*********";

        private static MQClient _client = new Aliyun.MQ.MQClient(_accessKeyId, _secretAccessKey, _endpoint);



        static MQProducer producer = _client.GetProducer(_instanceId, _topicName);


        static MQConsumer consumer = _client.GetConsumer(_instanceId, _topicName, _groupId, null);

        static void Main(string[] args)
        {
            // 生産者程式
            try
            {
                // 循環發送100條消息
                for (int i = 0; i < 50; i++)
                {
                    TopicMessage result = producer.PublishMessage(new TopicMessage("測試http類型的消息1"));
                    Console.WriteLine("publis message success: MessageId:" + result.Id + ", BodyMD5:" + result.BodyMD5);
                    result = producer.PublishMessage(new TopicMessage("測試http類型的消息2", "tag"));
                    Console.WriteLine("publis message success: MessageId:" + result.Id + ", BodyMD5:" + result.BodyMD5);
                }
            }
            catch (Exception ex)
            {
                Console.Write(ex);
            }

            // 消費者程式

            // 在目前線程循環消費消息,建議是多開個幾個線程并發消費消息
            while (true)
            {
                try
                {
                    // 長輪詢消費消息
                    // 長輪詢表示如果topic沒有消息則請求會在服務端挂住3s,3s内如果有消息可以消費則立即傳回
                    List<Message> messages = null;

                    try
                    {
                        messages = consumer.ConsumeMessage(
                            3, // 一次最多消費3條(最多可設定為16條)
                            3 // 長輪詢時間3秒(最多可設定為30秒)
                        );
                    }
                    catch (Exception exp1)
                    {
                        if (exp1 is MessageNotExistException)
                        {
                            Console.WriteLine(Thread.CurrentThread.Name + " No new message, " + ((MessageNotExistException)exp1).RequestId);
                            continue;
                        }
                        Console.WriteLine(exp1);
                        Thread.Sleep(2000);
                    }

                    if (messages == null)
                    {
                        continue;
                    }

                    List<string> handlers = new List<string>();
                    Console.WriteLine(Thread.CurrentThread.Name + " Receive Messages:");
                    // 處理業務邏輯
                    foreach (Message message in messages)
                    {
                        Console.WriteLine(message);
                        // Console.WriteLine("Property a is:" + message.GetProperty("a"));
                        handlers.Add(message.ReceiptHandle);
                    }
                    // Message.nextConsumeTime前若不确認消息消費成功,則消息會重複消費
                    // 消息句柄有時間戳,同一條消息每次消費拿到的都不一樣
                    try
                    {
                        consumer.AckMessage(handlers);
                        Console.WriteLine("Ack message success:");
                        foreach (string handle in handlers)
                        {
                            Console.Write("\t" + handle);
                        }
                        Console.WriteLine();
                    }
                    catch (Exception exp2)
                    {
                        // 某些消息的句柄可能逾時了會導緻确認不成功
                        if (exp2 is AckMessageException)
                        {
                            AckMessageException ackExp = (AckMessageException)exp2;
                            Console.WriteLine("Ack message fail, RequestId:" + ackExp.RequestId);
                            foreach (AckMessageErrorItem errorItem in ackExp.ErrorItems)
                            {
                                Console.WriteLine("\tErrorHandle:" + errorItem.ReceiptHandle + ",ErrorCode:" + errorItem.ErrorCode + ",ErrorMsg:" + errorItem.ErrorMessage);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex);
                    Thread.Sleep(2000);
                }
            }
        }
    }
}           

6、如果編譯出現錯誤:無法從“String”轉換為“char”

阿裡雲Rocket MQ Http .NET SDK使用Demo
解決政策:調整源碼
阿裡雲Rocket MQ Http .NET SDK使用Demo
阿裡雲Rocket MQ Http .NET SDK使用Demo

7、測試效果

阿裡雲Rocket MQ Http .NET SDK使用Demo

NET Core SDK使用(本身項目是Core項目)

SDK 安裝: Aliyun.MQ.Http
阿裡雲Rocket MQ Http .NET SDK使用Demo
消息的發送和接收使用方式與Framework SDK一緻。

更多收發消息

示例

阿裡雲Rocket MQ Http .NET SDK使用Demo

更多參考

C# SDK 接入說明 mq-http-csharp-sdk

繼續閱讀