230
新东方
消费消息示例代码__Java SDK_SDK使用手册_消息服务-阿里云
消费消息
public class ConsumerDemo {
public static void main(String[] args) {
CloudAccount account = new CloudAccount("YourAccessId", "YourAccessKey", "MNSEndpoint");
//this client need only initialize once
MNSClient client = account.getMNSClient();
//循环消费10条消息
try{
CloudQueue queue = client.getQueueRef("TestQueue");
for (int i = 0; i < 10; i++)
{
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();
}
}
最后更新:2016-08-18 13:23:34
上一篇:
发送消息示例代码__Java SDK_SDK使用手册_消息服务-阿里云
下一篇:
并发测试示例代码__Java SDK_SDK使用手册_消息服务-阿里云
删除自定义转码模板__自定义转码模板接口_API使用手册_媒体转码-阿里云
添加自定义算法和流程模板__用户指南_推荐引擎-阿里云
查看导入列表__数据迁移_API 参考_云数据库 RDS 版-阿里云
数据转发到DataHub__规则引擎_控制台使用手册_阿里云物联网套件-阿里云
设置静态网站托管__管理存储空间_控制台用户指南_对象存储 OSS-阿里云
设置证书__配置操作接口_API 手册_CDN-阿里云
ColumnSchema__DataType_API 参考_表格存储-阿里云
使用教程__JavaSDK手册_SDK参考手册_开放搜索-阿里云
规则配置__舆情分析_快速开始_公众趋势分析-阿里云
计量计费相关问题__常见问题_专有网络 VPC-阿里云
相关内容
常见错误说明__附录_大数据计算服务-阿里云
发送短信接口__API使用手册_短信服务-阿里云
接口文档__Android_安全组件教程_移动安全-阿里云
运营商错误码(联通)__常见问题_短信服务-阿里云
设置短信模板__使用手册_短信服务-阿里云
OSS 权限问题及排查__常见错误及排除_最佳实践_对象存储 OSS-阿里云
消息通知__操作指南_批量计算-阿里云
设备端快速接入(MQTT)__快速开始_阿里云物联网套件-阿里云
查询API调用流量数据__API管理相关接口_API_API 网关-阿里云
使用STS访问__JavaScript-SDK_SDK 参考_对象存储 OSS-阿里云