全部產品
Search
文件中心

Short Message Service:輕量訊息佇列(原MNS)消費Demo

更新時間:Dec 20, 2025

本文為您提供當前主流程式設計語言的訊息回執樣本,您可通過下載相應的SDK安裝包拉取隊列訊息。

SDK版本說明

阿里雲簡訊服務提供V1.0 SDKV2.0 SDK兩個版本。V2.0 SDK為最新發行的版本,不涉及介面功能變更,相較於V1.0 SDK,它支援更多的程式設計語言,解決了V1.0 SDK中存在的單Client安全執行緒問題,同時具備更強的健壯性和易用性,從而為開發人員在開發過程中提供更優質的使用體驗。

說明

V1.0 SDK已不再維護,推薦您使用V2.0 SDK。若您使用的是V1.0 SDK,建議升級到V2.0 SDK

SDK下載地址

國際簡訊SDK支援多種程式設計語言。您可以在OpenAPI門戶擷取SDK的安裝方式,也可以在開源平台GitHub中查看源碼及相關安裝指引。建議使用各程式設計語言主流的依賴管理工具進行安裝。

語言

SDK 安裝方式

Github地址

快速入門

Java

SDK for Java

java-dysmsapi-20180501

通過IDE使用阿里雲Java SDK

Python

SDK for Python

python-dysmsapi-20180501

通過IDE使用阿里雲Python SDK

PHP

SDK for PHP

php-dysmsapi-20180501

通過IDE使用阿里雲PHP SDK

Go

SDK for Go

go-dysmsapi-20180501

通過IDE使用阿里雲Go SDK

TypeScript

SDK for TypeScript

typescript-dysmsapi-20180501

通過IDE使用阿里雲Node.js SDK

C++

SDK for Swift

cpp-dysmsapi-20180501

/

C#

SDK for C#

csharp-dysmsapi-20180501

通過IDE使用阿里雲.NET SDK

Swift

SDK for Swift

swift-dysmsapi-20180501

/

訊息回執樣本

說明

調用介面前需配置環境變數,通過環境變數讀取存取憑證。AccessKey ID和AccessKey Secret的環境變數名:ALIBABA_CLOUD_ACCESS_KEY_IDALIBABA_CLOUD_ACCESS_KEY_SECRET。配置詳情請參見環境變數配置步驟

Java Demo

package com.alicom.mns.sample;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.alicom.mns.tools.inter.DefaultAlicomMessagePuller;
import com.alicom.mns.tools.inter.MessageListener;
import com.aliyun.mns.model.Message;

/**
 * This is the DEMO for SMS MNS Queue messages consumption
 * MessageType: SmsReport
 */
public class ReceiveDemo {


    static class MyMessageListener implements MessageListener {
        @Override
        public boolean dealMessage(Message message) {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            System.out.println("message receiver time from mns:" + format.format(new Date()));
            System.out.println("message handle: " + message.getReceiptHandle());
            System.out.println("message body: " + message.getMessageBodyAsString());
            System.out.println("message id: " + message.getMessageId());
            System.out.println("message dequeue count:" + message.getDequeueCount());
            System.out.println("Thread:" + Thread.currentThread().getName());
            try {
                //Please start your own code here

            } catch (Throwable e) {
                // The exception caused by your own code. Message will not be deleted and can be retrieved again
                return false;
            }
            // Message will be deleted for the format error
            return true;
        }

    }

    public static void main(String[] args) {

        DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();

        //Set the size of asynchronous thread
        puller.setConsumeMinThreadSize(6);
        puller.setConsumeMaxThreadSize(16);
        puller.setThreadQueueSize(200);
        puller.setPullMsgThreadSize(1);
        //Only enabled when debugging, should be disabled at working status
        puller.openDebugLog(false);

        String regionIdForPop = "ap-southeast-1";
        String endpointNameForPop = "ap-southeast-1";

        //If you are sending SMS requests to Singapore Region OpenAPI (domain: dysmsapi.ap-southeast-1.aliyuncs.com)
        //Please use
        String domainForPop = "dybaseapi.ap-southeast-1.aliyuncs.com";
        String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-1.aliyuncs.com";

        //If you are sending SMS requests to Indonesia Region(Jakarta) OpenAPI (domain: dysmsapi.ap-southeast-5.aliyuncs.com)
        //Please use
        //String domainForPop = "dybaseapi.ap-southeast-5.aliyuncs.com";
        //String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-5.aliyuncs.com";

        //If you are sending SMS requests to Germany Region(Frankfurt) OpenAPI (domain: dysmsapi.eu-central-1.aliyuncs.com)
        //String domainForPop = "dybaseapi.eu-central-1.aliyuncs.com";
        //String mnsAccountEndpoint = "http://1493622401794734.mns.eu-central-1.aliyuncs.com";

        //If you are sending SMS requests to US East Region(Virginia) OpenAPI (domain: dysmsapi.us-east-1.aliyuncs.com)
        //String domainForPop = "dybaseapi.us-east-1.aliyuncs.com";
        //String mnsAccountEndpoint = "http://1493622401794734.mns.us-east-1.aliyuncs.com";

        //AccessKey and SecretKey you can get if from Alibaba Cloud Console
        String accessKeyId = "";
        String accessKeySecret = "";

        //SmsReport
        String messageType = "SmsReport";

        //QueueName you can get it from Alibaba Cloud Sms Console
        //Pattern: Alicom-Queue-{UID}-{messageType}
        String queueName = "";

        puller.startReceiveMsgForVPC(accessKeyId, accessKeySecret, messageType, queueName, regionIdForPop,
            endpointNameForPop, domainForPop, mnsAccountEndpoint, new MyMessageListener());

        //or init your IAcsClient(accessKeySecret free)
        //puller.startReceiveMsg(acsClient, messageType, queueName, new MyMessageListener(), mnsAccountEndpoint);
    }

}

常見問題

Mac整合Python SDK報SSL認證驗證失敗“SSL: CERTIFICATE_VERIFY_FAILED”

在Mac環境下整合Python SDK出現“SSL: CERTIFICATE_VERIFY_FAILED”異常是什麼原因?(websocket closed due to [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000))。

在串連WebSocket時,可能會遇到OpenSSL驗證認證失敗的問題,提示找不到認證。這通常是由於Python環境的認證配置不正確導致的。可以通過以下步驟手動定位並修複認證問題:

  1. 匯出系統認證並設定環境變數:執行以下命令,將macOS系統中的所有認證匯出到一個檔案,並將其設定為Python和相關庫的預設憑證路徑。

    security find-certificate -a -p > ~/all_mac_certs.pem
    export SSL_CERT_FILE=~/all_mac_certs.pem
    export REQUESTS_CA_BUNDLE=~/all_mac_certs.pem
  2. 建立符號連結以修複Python的OpenSSL配置:如果Python的OpenSSL配置缺失認證,可以通過以下命令手動建立符號連結。請確保替換命令中的路徑為本地Python版本的實際安裝目錄。

    # 3.9是樣本版本號碼,請根據您本地安裝的 Python 版本調整路徑ln -s /etc/ssl/* /Library/Frameworks/Python.framework/Versions/3.9/etc/openssl
  3. 重新啟動終端並清除緩衝:完成上述操作後,請關閉並重新開啟終端,以確保環境變數生效。清除可能的緩衝後重試串連WebSocket。

通過以上步驟,可以解決因認證配置錯誤導致的串連問題。如果問題仍未解決,請檢查目標伺服器的認證配置是否正確。