This topic provides message consumption samples for popular programming languages. You can download the corresponding software development kit (SDK) packages to pull messages from a queue.
SDK version guide
Alibaba Cloud Short Message Service provides two software development kit (SDK) versions: V1.0 SDK and V2.0 SDK. V2.0 SDK is the latest version and offers the same API features as V1.0 SDK. Compared to V1.0 SDK, V2.0 SDK is more robust, is easier to use, and supports more programming languages. It also resolves the thread safety issue for single clients that is present in V1.0 SDK, providing a better developer experience.
V1.0 SDK is no longer maintained. We recommend that you use V2.0 SDK. If you are using V1.0 SDK, you must upgrade to V2.0 SDK.
SDK download addresses
The International SMS SDK supports multiple programming languages. You can find the SDK installation methods on the OpenAPI Portal. You can also view the source code and installation guides on GitHub. We recommend that you install the SDK using the standard dependency management tool for your programming language.
Language | SDK installation method | GitHub address | Quick Start |
Java | |||
Python | |||
PHP | |||
Go | |||
TypeScript | |||
C++ | / | ||
C# | |||
Swift | / |
Message receipt samples
Before you call an API, you must configure environment variables so that the SDK can read your access credentials. The environment variable names for the AccessKey ID and AccessKey secret are ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET respectively. For more information about the configuration, see Steps to configure environment variables.
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);
}
}
FAQ
"SSL: CERTIFICATE_VERIFY_FAILED" error when you integrate the Python SDK on macOS
Why does the "SSL: CERTIFICATE_VERIFY_FAILED" exception occur when you integrate the Python SDK on macOS? The full error message is `websocket closed due to [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1000)`.
When you connect to a WebSocket, OpenSSL may fail to authenticate the server certificate. This issue usually occurs because the required root certificate is not correctly configured in your Python environment. You can manually resolve this issue by following these steps:
Export system certificates and set environment variables: Run the following command to export all certificates from the macOS system to a file. Then, set the path to this file as the default certificate path for Python and related libraries.
security find-certificate -a -p > ~/all_mac_certs.pem export SSL_CERT_FILE=~/all_mac_certs.pem export REQUESTS_CA_BUNDLE=~/all_mac_certs.pemCreate a symbolic link to fix the OpenSSL configuration for Python: If a certificate is missing from the Python OpenSSL configuration, you can run the following command to manually create a symbolic link. Make sure that you replace the path in the command with the actual installation path of your local Python version.
# 3.9 is an example version number. Adjust the path based on the Python version installed on your local machine. ln -s /etc/ssl/* /Library/Frameworks/Python.framework/Versions/3.9/etc/opensslRestart the terminal and clear the cache: After you complete the preceding steps, you must close and reopen the terminal to ensure that the environment variables take effect. You can then clear any possible cache and try to connect to the WebSocket again.
The preceding steps can resolve connection issues that are caused by incorrect certificate configurations. If the issue persists, you must check whether the certificate configuration of the destination server is correct.