本文为您提供当前主流编程语言的消息回执示例,您可通过下载相应的SDK安装包拉取队列消息。
SDK版本说明
阿里云短信服务提供V1.0 SDK和V2.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 | |||
Python | |||
PHP | |||
Go | |||
TypeScript | |||
C++ | / | ||
C# | |||
Swift | / |
消息回执示例
调用接口前需配置环境变量,通过环境变量读取访问凭证。AccessKey ID和AccessKey Secret的环境变量名:ALIBABA_CLOUD_ACCESS_KEY_ID 、ALIBABA_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环境的证书配置不正确导致的。可以通过以下步骤手动定位并修复证书问题:
导出系统证书并设置环境变量:执行以下命令,将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创建符号链接以修复Python的OpenSSL配置:如果Python的OpenSSL配置缺失证书,可以通过以下命令手动创建符号链接。请确保替换命令中的路径为本地Python版本的实际安装目录。
# 3.9是示例版本号,请根据您本地安装的 Python 版本调整路径ln -s /etc/ssl/* /Library/Frameworks/Python.framework/Versions/3.9/etc/openssl重新启动终端并清除缓存:完成上述操作后,请关闭并重新打开终端,以确保环境变量生效。清除可能的缓存后重试连接WebSocket。
通过以上步骤,可以解决因证书配置错误导致的连接问题。如果问题仍未解决,请检查目标服务器的证书配置是否正确。