队列使用手册__Java SDK_SDK使用手册_消息服务-阿里云
本文档介绍如何使用java sdk中的sample代码,完成创建队列、发送消息、接收删除消息和删除队列操作。
1. 准备
- 下载最新版java sdk,解压到aliyun-sdk-mns-samples文件夹;
- 用Eclipse导入Maven工程,选中aliyun-sdk-mns-samples文件夹;
- 在用户目录(Linux系统为”/home/YOURNAME/“目录或者Windows系统为 “C:UsersYOURNAME”目录)中创建.aliyun-mns.properties文件,并填写服务地址、AccessKeyID和AccessKeySecret:
- AccessKeyId、AccessKeySecret
- 访问阿里云API的密钥对;
- 如果使用主账号访问,登陆阿里云 AccessKey 管理页面创建、查看;
- 如果使用子账号访问,请登录阿里云访问控制控制台查看;
- Endpoint
- 访问MNS的接入地址,登陆MNS控制台 单击右上角 获取Endpoint 查看;
- 不同地域的接入地址不同,分为公网以及内网域名;
- AccessKeyId、AccessKeySecret
2. 创建队列
下面给出了创建队列的代码段(队列详细信息请参考详情);
public class CreateQueueDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全String queueName = "TestQueue";QueueMeta meta = new QueueMeta(); //生成本地QueueMeta属性,有关队列属性详细介绍见https://help.aliyun.com/document_detail/27476.htmlmeta.setQueueName(queueName); // 设置队列名meta.setPollingWaitSeconds(15);meta.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){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.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");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close(); // 程序退出时,需主动调用client的close方法进行资源释放}}
3. 发送消息
创建完队列之后,就可以向队列发送消息
public class ProducerDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");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 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){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.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");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close(); // 程序退出时,需主动调用client的close方法进行资源释放}}
4. 接收和删除消息
队列中已经发送了1条消息,下面是从队列中取出并删除该条消息。
public class ConsumerDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全try{CloudQueue queue = client.getQueueRef("TestQueue");Message popMsg = queue.popMessage();if (popMsg != null){System.out.println("message handle: " + popMsg.getReceiptHandle());System.out.println("message body: " + popMsg.getMessageBodyAsString());System.out.println("message id: " + popMsg.getMessageId());System.out.println("message dequeue count:" + popMsg.getDequeueCount());//删除已经取出消费的消息queue.deleteMessage(popMsg.getReceiptHandle());System.out.println("delete message successfully.n");}else{System.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 (ServiceException se){se.printStackTrace();logger.error("MNS exception requestId:" + se.getRequestId(), se);if (se.getErrorCode() != null) {if (se.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");}/*you can get more MNS service error code from following link:https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html*/}} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close();}}
5. 删除队列
public class DeleteQueueDemo {public static void main(String[] args) {CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");MNSClient client = account.getMNSClient(); // 在程序中,CloudAccount以及MNSClient单例实现即可,多线程安全try{CloudQueue queue = client.getQueueRef("TestQueue");queue.delete();} 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){se.printStackTrace();} catch (Exception e){System.out.println("Unknown exception happened!");e.printStackTrace();}client.close();}}
最后更新:2016-11-23 16:04:12
上一篇:
下载__Java SDK_SDK使用手册_消息服务-阿里云
下一篇:
主题使用手册__Java SDK_SDK使用手册_消息服务-阿里云
添加解析记录__解析管理接口_API文档_云解析-阿里云
MetaService__用户指南_E-MapReduce-阿里云
资源报表__控制台使用指南_消息队列 MQ-阿里云
环境配置问题__常见问题_弹性伸缩-阿里云
错误码表__错误说明_API 网关-阿里云
查询资源类型模板信息__资源相关接口_API 文档_资源编排-阿里云
镜像部署Windows环境__建站教程_云服务器 ECS-阿里云
步骤 2:创建Windows实例__快速入门(Windows)_云服务器 ECS-阿里云
类目管理__媒体库管理_开发人员指南_视频点播-阿里云
阿里云MaxCompute香港开服 将引入更多人工智能服务
相关内容
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云