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-阿裏雲