全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网 钉钉智能硬件
消息服务

示例代码

更新时间:2017-06-07 13:26:11

示例代码

0.准备工作

将 G_Config_Endpoint,G_Config_AccessId, G_Config_AccessKey, G_Config_SecurityToken设置为对应的MNS endpoint, accessid,accesskey, securityToken(临时AK,可选)

场景:创建队列, 消息生产者, 消息消费者, 删除队列,创建主题,发布消息,删除主题。

1.创建队列

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey];
  3. NSString* queueName = @"ali-test-queue-1";
  4. AliMNSQueue* queue = [account getQueue:queueName];
  5. AliMNSQueueMeta* queueMeta = [[AliMNSQueueMeta alloc] initWithQueueName:queueName];
  6. queueMeta.maximumMessageSize = 200;
  7. queueMeta.visibilityTimeout = 100;
  8. @try {
  9. NSString* queueUrl = [queue create:queueMeta];
  10. NSLog(@"Create queue successfully, queueUrl=%@", queueUrl);
  11. } @catch(AliMNSClientNetworkException* cne){
  12. NSLog(@"network error:%@", cne.subMessage);
  13. } @catch(AliMNSServerException* se){
  14. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  15. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  16. }

2.消息生产者

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* queueName = @"ali-test-queue-1"; // assume this queue is created pepreviously.
  4. AliMNSQueue* queue = [account getQueue:queueName];
  5. AliMNSMessage* msg = [AliMNSMessage new];
  6. msg.messageBody = @"test body.";
  7. msg.delaySeconds = 1;// delay 1s
  8. @try {
  9. AliMNSMessage* retMsg = [queue sendMessage:msg];
  10. NSLog(@"messageId=%@, messageBodyMd5=%@ receiptHandler=%@", retMsg.messageId, retMsg.messageBodyMd5, retMsg.receiptHandle);// if the message is delay message, it will return receiptHandler as well, and we can use this to delete it before it visible to the consumer.
  11. } @catch(AliMNSClientNetworkException* cne){
  12. NSLog(@"network error:%@", cne.subMessage);
  13. } @catch(AliMNSServerException* se){
  14. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  15. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  16. }

3.消息消费者

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* queueName = @"ali-test-queue-1"; // assume this queue is created pepreviously.
  4. AliMNSQueue* queue = [account getQueue:queueName];
  5. NSInteger waitSeconds = 10; // this means the request will hold by MNS server side when the queue is empty.during this period, if there is new message, the request will return at once.
  6. @try {
  7. AliMNSMessage* getMsg = [queue receiveMessage: waitSeconds];
  8. //todo: add your logic to deal with the message. Here we only print the body of message.
  9. NSLog(@"messegeBody=%@", getMsg.messageBody);
  10. //Important:delete the message after deal with the message successfully.
  11. [queue deleteMessage:getMsg.receiptHandle];
  12. } @catch(AliMNSClientNetworkException* cne){
  13. NSLog(@"network error:%@", cne.subMessage);
  14. } @catch(AliMNSServerException* se){
  15. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  16. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  17. }

4.删除队列

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* queueName = @"ali-test-queue-1";
  4. AliMNSQueue* queue = [account getQueue:queueName];
  5. @try {
  6. [queue deleteQueue];
  7. } @catch(AliMNSClientNetworkException* cne){
  8. NSLog(@"network error:%@", cne.subMessage);
  9. } @catch(AliMNSServerException* se){
  10. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  11. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  12. }

5.创建主题(topic)

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* topicName = @"ali-test-topic-1";
  4. AliMNSTopic* topic = [account getTopic:topicName];
  5. AliMNSTopicMeta* topicMeta = [[AliMNSTopicMeta alloc] initWithTopicName:topicName];
  6. topicMeta.maximumMessageSize = 2000;
  7. @try {
  8. NSString* topicUrl = [topic create:topicMeta];
  9. NSLog(@"topicUrl=%@", topicUrl);
  10. } @catch(AliMNSClientNetworkException* cne){
  11. NSLog(@"network error:%@", cne.subMessage);
  12. } @catch(AliMNSServerException* se){
  13. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  14. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  15. }

6.发布消息(PublishMessage)

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* topicName = @"ali-test-topic-1";
  4. AliMNSTopic* topic = [account getTopic:topicName];
  5. AliMNSTopicMessage* msg = [AliMNSTopicMessage new];
  6. msg.messageBody = @"my publish message";
  7. @try {
  8. AliMNSTopicMessage* retMsg = [topic publishMessage:msg];
  9. NSLog(@"messageId=%@, messageBodyMd5=%@", retMsg.messageId, retMsg.messageBodyMd5);
  10. } @catch(AliMNSClientNetworkException* cne){
  11. NSLog(@"network error:%@", cne.subMessage);
  12. } @catch(AliMNSServerException* se){
  13. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  14. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  15. }

7.删除主题(topic)

  1. AliMNSAccount* account = [[AliMNSAccount alloc]
  2. initWithEndpoint:G_Config_Endpoint withAccessId:G_Config_AccessId withAccessKey:G_Config_AccessKey withSecurityToken:G_Config_SecurityToken];
  3. NSString* topicName = @"ali-test-topic-1";
  4. AliMNSTopic* topic = [account getTopic:topicName];
  5. @try {
  6. [topic deleteTopic];
  7. } @catch(AliMNSClientNetworkException* cne){
  8. NSLog(@"network error:%@", cne.subMessage);
  9. } @catch(AliMNSServerException* se){
  10. NSLog(@"mns server side error: errorCode=%@", se.errorCode);
  11. // the table for description MNS sever side error is in: https://help.aliyun.com/document_detail/27501.html
  12. }
本文导读目录