阿里云-消息服务-SDK手册-D_第1页
阿里云-消息服务-SDK手册-D_第2页
阿里云-消息服务-SDK手册-D_第3页
阿里云-消息服务-SDK手册-D_第4页
阿里云-消息服务-SDK手册-D_第5页
已阅读5页,还剩53页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、消息服务SDK使用手册消息服务/SDK使用手册消息服务/SDK使用手册 PAGE 57 PAGE 57SDK使用手册Java SDK MNS JavaSDK建议下载最新发布的SDK版本以获得最佳性能和稳定性。Version 1.1.5更新日期2016-05-26 sdk下载 sample下载更新内容增加事务消息队列TransactionQueue;增加一对多广播消息功能;新增Javasdk性能测试示例代码;使用帮助下载sample并解压aliyun-sdk-mns-samples-1.1.5.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在

2、用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行QueueSample.javaTopicSample.javaCloudPullTopicDemo.java(广播消息示例代

3、码), TransactionMessageDemo.java(事务队列完全封装版使用示例),TransactionMessageDemo2.java(事务队列用户自定义版示例,需要用户自定义本地事务,做failover处理)Version 1.1.4更新日期2016-04-25 sdk下载 sample下载更新内容Subscription支持Queue/MailEndpointTopic支持消息过滤BugFix(maxConnectionsPerRoute)最大链接数导致请求超时使用帮助下载sample并解压aliyun-sdk-mns-samples-1.1.4.zip;用Eclipse导

4、入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行QueueSample.javaTopicSamp

5、le.javaVersion 1.1.3更新日期2016-03-28 sdk下载 sample下载更新内容支持HTTPS去除Message对象中priority,dequeueCount,delaySeconds的默认初始化值使用帮助下载sample并解压aliyun-sdk-mns-samples-1.1.3.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和Acce

6、ssKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行QueueSample.javaTopicSample.javaVersion 1.1.2更新日期2016-01-30 sdk下载 sample下载更新内容1. fixbug: popMessage接口无参数情况下waitseconds取QueueMeta中设置的值,而非0使用帮助下载sample并解压ali

7、yun-sdk-mns-samples-1.1.2.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_acc

8、esskeysecret运行QueueSample.javaTopicSample.javaVersion 1.1.1更新日期2016-01-19 sdk下载 sample下载更新内容1. fixbug: 中文消息使用UTF8编码,而非平台默认字符集使用帮助下载sample并解压aliyun-sdk-mns-samples-1.1.1.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、Acce

9、ssKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行QueueSample.javaTopicSample.javaVersion 1.1.0更新日期2016-01-06 sdk下载 sample下载更新内容添加对于Topic功能的支持添加对于STSToken的支持消息Base64编码支持可选使用帮助下载sample并解压aliyun-

10、sdk-mns-samples-1.1.0.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accessk

11、eysecret运行QueueSample.javaTopicSample.javaVersion 1.0.5更新日期2015-12-02 sdk下载 sample下载更新内容修复bug:多CloudAccount对象时导致内存泄漏依赖的httpasyncclient版本升至4.1使用帮助下载sample并解压aliyun-sdk-mns-samples-1.0.5.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.pertie

12、s文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行Sample.java文件Version 1.0.4更新日期2015-11-05 sdk下载 sample下载更新内容修复bug:网络异常时极端情况下线程挂起修复bug:关闭空闲连接回收常驻线程使用帮助下载sample并解压aliyun-sdk-mns-

13、samples-1.0.4.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret

14、运行Sample.java文件Version 1.0.3更新日期2015-06-09 sdk下载 sample下载更新内容修复bug:大量closewait的连接导致SDK挂起;增加samplecode;3. API协议升级:x-mns-version=2015-06-06;4. 支持BatchSendMessage、BatchReceiveMessage、BatchPeekMessage、BatchDeleteMessage;使用帮助下载sample并解压aliyun-sdk-mns-samples-1.0.3.zip;用Eclipse导入Maven工程,选中aliyun-sdk-mns-s

15、amples文件夹;在用户目录(/home/YOURNAME/inLinuxorC:UsersYOURNAMEinWindows)中创建.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:mns.accountendpoint=http:/$ mns.accountendpoint=http:/$ mns.accesskeyid=$your_accesskeyid mns.accesskeysecret=$your_accesskeysecret运行Sample.java文件Version 1.0.2更新日期2015-03-03 下载更新内容1. 优化

16、XML解析逻辑,提升性能Version 1.0.1更新日期2014-12-19 下载更新内容1. 缺省线程池修正为50, 修复大规模并发同步时SDK端的性能瓶颈Version 1.0.0更新日期2014-08-01 下载队列使用手册本文档介绍如何使用java sdk中的sample代码,完成创建队列、发送消息、接收删除消息和删除队列操作。准备下载最新版javasdk,解压到aliyun-sdk-mns-samples文件夹;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(Linux系统为/home/YOURNAME/目录或者Windows

17、系统为.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:AccessKeyId、AccessKeySecret访问阿里云API的密钥对;如果使用主账号访问,登陆AccessKey创建、查看;如果使用子账号访问,请登录阿里云访问控制控制台查看;Endpoint访问MNS的接入地址,登陆MNS控制台 单击右上角 获取Endpoint 查看;不同地域的接入地址不同,分为公网以及内网域名;创建队列public class CreateQueueDemo public static void main(String args) CloudAccount acc

18、ount = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全String queueName = TestQueue;public class CreateQueueDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId,

19、YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全String queueName = TestQueue;QueueMeta metanew QueueMeta()/生成本地QueueMeta属性,有关队列属性详细介绍见/document_detail/27476.htmlmeta.setQueueName(queueName); / 设置队列名meta.setPollingWaitSeconds(15); meta.

20、setMaxMessageSize(2048L);try CloudQueue queue = client.createQueue(meta); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Please check your network and DNS availablity.); ce.printStackTrace(); catch (ServiceException ex)if (se

21、.getErrorCode().equals(QueueNotExist)System.out.println(Queue is not exist.Please create before use); else if (se.getErrorCode().equals(TimeExpired)System.out.println(The request is time expired. Please check your local machine timeclock);se.printStackTrace(); catch (Exception e)System.out.println(U

22、nknown exception happened!); e.printStackTrace();client.close(); / 程序退出时,需主动调用client的close方法进行资源释放发送消息public class ProducerDemo public static void main(String args) public class ProducerDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNS

23、Endpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全try CloudQueue queue = client.getQueueRef(TestQueue); Message message = new Message(); message.setMessageBody(I am test message ); Message putMsg = queue.putMessage(message);System.out.println(Send message

24、 id is: + putMsg.getMessageId(); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Please check your network and DNS availablity.); ce.printStackTrace(); catch (ServiceException ex)if (se.getErrorCode().equals(QueueNotExist)Syst

25、em.out.println(Queue is not exist.Please create before use); else if (se.getErrorCode().equals(TimeExpired)System.out.println(The request is time expired. Please check your local machine timeclock);se.printStackTrace(); catch (Exception e)System.out.println(Unknown exception happened!); e.printStack

26、Trace();client.close(); / 程序退出时,需主动调用client的close方法进行资源释放client.close(); / 程序退出时,需主动调用client的close方法进行资源释放接收和删除消息public class ConsumerDemo public static void main(String args) public class ConsumerDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAcc

27、essKey, MNSEndpoint);MNSClient clientaccount.getMNSClient()/,CloudAccount以及MNSClient单例实现即可,多线程安全tryCloudQueue queue = client.getQueueRef(TestQueue);Message popMsg = queue.popMessage(); if (popMsg != null)System.out.println(message handle: + popMsg.getReceiptHandle(); System.out.println(message body:

28、 + popMsg.getMessageBodyAsString(); System.out.println(message id: + popMsg.getMessageId(); System.out.println(messagedequeuecount:+popMsg.getDequeueCount();/删除已经取出消费的消息queue.deleteMessage(popMsg.getReceiptHandle(); System.out.println(delete message successfully.n);elseSystem.out.println(message not

29、 exist in TestQueue.n); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Please check your network and DNS availablity.); ce.printStackTrace(); catch (ServiceException se)if (se.getErrorCode().equals(QueueNotExist)System.out.pr

30、intln(Queue is not exist.Please create queue before use); else if (se.getErrorCode().equals(TimeExpired)System.out.println(The request is time expired. Please check your local machine timeclock);se.printStackTrace(); catch (Exception e)System.out.println(Unknown exception happened!); e.printStackTra

31、ce();client.close();public class DeleteQueueDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient clientpublic class DeleteQueueDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccess

32、Id, YourAccessKey, MNSEndpoint);MNSClient clientaccount.getMNSClient()/,CloudAccount以及MNSClient单例实现即可,多线程安全tryCloudQueue queue = client.getQueueRef(TestQueue);queue.delete(); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Ple

33、ase check your network and DNS availablity.); ce.printStackTrace(); catch (ServiceException se)se.printStackTrace(); catch (Exception e)System.out.println(Unknown exception happened!); e.printStackTrace();client.close();主题使用手册本文档介绍如何使用java sdk中的sample代码,完成创建主题、创建订阅,发布消息、接收消息以及删除主题等操作。准备下载最新版javasdk,

34、解压到aliyun-sdk-mns-samples文件夹;用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;在用户目录(Linux系统为/home/YOURNAME/目录或者Windows系统为.perties文件,并填写服务地址、AccessKeyID和AccessKeySecret:AccessKeyId、AccessKeySecret访问阿里云API的密钥对;创建主题如果使用主账号访问,登陆AccessKey创建、查看;如果使用子账号访问,请登录阿里云访问控制控制台查看;Endpoint访问MNS的接入地址,登陆MNS控制台 单击右上角 获取En

35、dpoint 查看;不同地域的接入地址不同,分为公网以及内网域名;public class CreateTopicDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全String topicName = TestTopic; Topi

36、cMeta meta = new TopicMeta(); meta.setTopicName(topicName);public class CreateTopicDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全String t

37、opicName = TestTopic; TopicMeta meta = new TopicMeta(); meta.setTopicName(topicName);try CloudTopic topic = client.createTopic(meta); catch (Exception e) e.printStackTrace();System.out.println(create topic error, + e.getMessage();client.close();启动HttpEndpoint对MNS推送消息请求做签名验证; 解析推送请求的消息body体;返回相应的处理返回

38、码:200;对MNS推送消息请求做签名验证; 解析推送请求的消息body体;返回相应的处理返回码:200;HttpEndpoint的具体实现源码可参考sdk中源码;创建订阅public class SubscribeDemo 对已经创建好的主题Topic进行订阅,在订阅时需要设置对应的推送Endpoint地址(目前支持HTTP、邮件以及 队列)、错误重试策略、推送消息格式等;public class SubscribeDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAcc

39、essId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccoun

40、t以及MNSClient单例实现即可,多线程安全CloudTopic topic = client.getTopicRef(TestTopic); try SubscriptionMeta subMeta = new SubscriptionMeta(); subMeta.setSubscriptionName(TestSub); subMeta.setEndpoint(HttpEndpoint.GenEndpointLocal(); subMeta.setNotifyContentFormat(SubscriptionMeta.NotifyContentFormat.XML); String

41、 subUrl = topic.subscribe(subMeta);System.out.println(subscription url: + subUrl); catch (Exception e) e.printStackTrace();System.out.println(subscribe/unsubribe error);client.close();5.发布消息public class PublishMessageDemo public static void main(String args) CloudAccount account = new CloudAccount(Y

42、ourAccessId, YourAccessKey, MNSEndpoint);public class PublishMessageDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全CloudTopic topic = clie

43、nt.getTopicRef(TestTopic); try TopicMessage msgnew Base64TopicMessage()/可以使用TopicMessage结构,选择不进行Base64加密msg.setMessageBody(hello world!);msg = topic.publishMessage(msg); System.out.println(msg.getMessageId(); System.out.println(msg.getMessageBodyMD5(); catch (Exception e) e.printStackTrace(); System

44、.out.println(subscribe error);client.close();查看HttpEndpoint接收消息第5步发布了一条消息到Topic中,MNS会将该消息推送到所有的订阅Endpoint,本例中的HttpEndpoint会将 接收到的消息打印出来。取消订阅public class UnsubscribeDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);public class

45、 UnsubscribeDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全CloudTopic topic = client.getTopicRef(TestTopic); try topic.unsubscribe(TestSub

46、); catch (Exception e) e.printStackTrace(); System.out.println(unsubribe error);client.close();8.删除主题public class DeleteTopicDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);public class DeleteTopicDemo public static void mai

47、n(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);MNSClient client = account.getMNSClient(); / 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全CloudTopic topic = client.getTopicRef(TestTopic); try topic.delete(); catch (Exception e) e.printStackTrace(); System.

48、out.println(delete topic error);client.close();public class ProducerDemo public static void main(String args) public class ProducerDemo public static void main(String args) CloudAccount account = new CloudAccount(YourAccessId, YourAccessKey, MNSEndpoint);/这个client仅初始化一次/这个client仅初始化一次MNSClient clien

49、t = account.getMNSClient();/循环发送10条消息try/TestQueue是你的测试队列,请提前创建CloudQueue queue = client.getQueueRef(TestQueue); for (int i = 0; i 10; i+)Message message = new Message(); message.setMessageBody(I am test message + i); Message putMsg = queue.putMessage(message);System.out.println(Send message id is:

50、+ putMsg.getMessageId(); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Please check your network and DNS availablity.); ce.printStackTrace(); catch (ServiceException se)if (se.getErrorCode().equals(QueueNotExist)System.out.p

51、rintln(Queue is not exist.Please create before use); else if (se.getErrorCode().equals(TimeExpired)System.out.println(The request is time expired. Please check your local machine timeclock);/*you can get more MNS service error code from following link: /document_detail/mns/api_reference/error_code/e

52、rror_code.html*/ se.printStackTrace(); catch (Exception e)System.out.println(Unknown exception happened!); e.printStackTrace();client.close();public class ConsumerDemo public static void main(String args) public class ConsumerDemo public static void main(String args) CloudAccount account = new Cloud

53、Account(YourAccessId, YourAccessKey, MNSEndpoint);/this client need only initialize once MNSClient client = account.getMNSClient();/循环消费10条消息tryCloudQueue queue = client.getQueueRef(TestQueue); for (int i = 0; i 10; i+)Message popMsg = queue.popMessage(); if (popMsg != null)System.out.println(messag

54、e handle: + popMsg.getReceiptHandle(); System.out.println(message body: + popMsg.getMessageBodyAsString(); System.out.println(message id: + popMsg.getMessageId(); System.out.println(messagedequeuecount:+popMsg.getDequeueCount();/删除已经消费的消息queue.deleteMessage(popMsg.getReceiptHandle(); System.out.prin

55、tln(delete message successfully.n);elseSystem.out.println(message not exist in TestQueue.n); catch (ClientException ce)System.out.println(Something wrong with the network connection between client and MNS service.+ Please check your network and DNS availablity.); ce.printStackTrace(); catch (Service

56、Exception se)if (se.getErrorCode().equals(QueueNotExist)System.out.println(Queue is not exist.Please create queue before use); else if (se.getErrorCode().equals(TimeExpired)System.out.println(The request is time expired. Please check your local machine timeclock);/*you can get more MNS service error

57、 code in following link. /document_detail/mns/api_reference/error_code/error_code.html*/ se.printStackTrace(); catch (Exception e)System.out.println(Unknown exception happened!); e.printStackTrace();client.close();并发测试Case本文档介绍了基于Java SDK提供的队列消息发送以及消费的并发测试Case。用户可指定并发度、运行时间;用户可指定并发度、运行时间;使用发送总请求数除以运

58、行时间得到QPS;初始化Endpoint= AccessId= AccessKey=Endpoint= AccessId= AccessKey=QueueName=JavaSDKPerfTestQueue ThreadNum=200TotalSeconds=180注:ThreadNum是发送/消费的线程数,MNS具备强大的高并发扩展性能; TotalSeconds是测试Case运行的时间;ThreadNum是发送/消费的线程数,MNS具备强大的高并发扩展性能; TotalSeconds是测试Case运行的时间;package com.aliyun.mns;import com.aliyun.m

59、ns.client.CloudAccount; import com.aliyun.mns.client.CloudQueue; import com.aliyun.mns.client.MNSClient;import mon.http.ClientConfiguration; package com.aliyun.mns;import com.aliyun.mns.client.CloudAccount; import com.aliyun.mns.client.CloudQueue; import com.aliyun.mns.client.MNSClient;import mon.ht

60、tp.ClientConfiguration; import com.aliyun.mns.model.Message;import com.aliyun.mns.model.QueueMeta;import java.io.BufferedInputStream; import java.io.FileInputStream;import java.io.FileNotFoundException; import java.io.IOException;import java.util.ArrayList; import java.util.Properties;import java.ut

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论