All Products
Search
Document Center

JAVA

Last Updated: Nov 26, 2019

Input parameters

Name Type O/M Example Description
MessageType String Mandatory SmsReport SmsReport
QueueName String Mandatory Alicom-Queue-xxxxxx-SmsReport The name of the message queue returned from the console——System Setting.

Output parameters

Name Name Example Description
MessageId String 10080303003003 The ID generated by Alibaba Cloud to uniquely identify an SMS message.
To String 452220121 The phone number of the SMS message recipient.
Status Number 1 The status of the SMS message. Valid values: 1 (Success), 0 (Fail).
ErrorCode String success The returned error code.
ErrorDescription String success The returned error message.
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was sent.
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 The time and date when the SMS message was received.

Developer integration process

Note: To use the SMS Send API and Query API, you need to import the Core SDK first, and add maven dependencies or download the SDK.

1.SDK download: Click to view the download link

2.Writing a sample program

When calling the interface, please note:

  • The regionIdForPop and endpointNameForPop must be “ap-southeast-1”, please do not modify.
  • domainForPop must be “dybaseapi.ap-southeast-1.aliyuncs.com”, please do not modify it.
  • mnsAccountEndpoint must be “http://1493622401794734.mns.ap-southeast-1.aliyuncs.com”, please do not modify.
  1. package com.alicom.mns.sample;
  2. import java.text.ParseException;
  3. import java.text.SimpleDateFormat;
  4. import java.util.Date;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import com.alicom.mns.tools.DefaultAlicomMessagePuller;
  10. import com.alicom.mns.tools.MessageListener;
  11. import com.aliyun.mns.model.Message;
  12. import com.google.gson.Gson;
  13. /**
  14. * This is the DEMO for SMS Delivery Report API
  15. */
  16. public class ReceiveDemo {
  17. private static Log logger=LogFactory.getLog(ReceiveDemo.class);
  18. static class MyMessageListener implements MessageListener{
  19. private Gson gson=new Gson();
  20. @Override
  21. public boolean dealMessage(Message message) {
  22. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  23. System.out.println("message receiver time from mns:" + format.format(new Date()));
  24. System.out.println("message handle: " + message.getReceiptHandle());
  25. System.out.println("message body: " + message.getMessageBodyAsString());
  26. System.out.println("message id: " + message.getMessageId());
  27. System.out.println("message dequeue count:" + message.getDequeueCount());
  28. System.out.println("Thread:" + Thread.currentThread().getName());
  29. try{
  30. Map<String,Object> contentMap=gson.fromJson(message.getMessageBodyAsString(), HashMap.class);
  31. String arg = (String) contentMap.get("arg");
  32. //Please start your own code here
  33. }catch(com.google.gson.JsonSyntaxException e){
  34. logger.error("error_json_format:"+message.getMessageBodyAsString(),e);
  35. // Message will be deleted for the format error
  36. return true;
  37. } catch (Throwable e) {
  38. // The exception caused by your own code. Message will not be deleted and be pushed again
  39. return false;
  40. }
  41. // Message will be deleted for the format error
  42. return true;
  43. }
  44. }
  45. public static void main(String[] args) throws Exception, ParseException {
  46. DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();
  47. //Set the size of asynchronous thread
  48. puller.setConsumeMinThreadSize(6);
  49. puller.setConsumeMaxThreadSize(16);
  50. puller.setThreadQueueSize(200);
  51. puller.setPullMsgThreadSize(1);
  52. //Only enabled when debugging, should be disabled at working status
  53. puller.openDebugLog(false);
  54. String regionIdForPop = "ap-southeast-1";
  55. String endpointNameForPop = "ap-southeast-1";
  56. String domainForPop = "dybaseapi.ap-southeast-1.aliyuncs.com";
  57. String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-1.aliyuncs.com";
  58. //AccessKey, you can get if from Alicloud Console
  59. String accessKeyId="yourAccessKeyId";
  60. String accessKeySecret="yourAccessKeySecret";
  61. //Message Type: SmsReport
  62. String messageType="SmsReport";
  63. //Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
  64. String queueName="Alicom-Queue-********-SmsReport";
  65. puller.startReceiveMsgForVPC(accessKeyId, accessKeySecret, messageType, queueName, regionIdForPop,
  66. endpointNameForPop, domainForPop, mnsAccountEndpoint,new MyMessageListener());
  67. }
  68. }