如题所述
ããè¥è¦æ§è¡åå¨æä½ï¼æ¨éè¦ä¸ä¸ª Windows Azure åå¨å¸æ·ãå¯éè¿ä»¥ä¸æ¥éª¤å建åå¨å¸æ·ãï¼ä¹å¯ä»¥ä½¿ç¨ REST API å建åå¨å¸æ·ãï¼
ããç»å½å° Windows Azure 管çé¨æ·ã
ããå¨å¯¼èªçªæ ¼çåºé¨ï¼åå»âæ°å»ºâã
ãã+æ°å»º
ããä¾æ¬¡åå»âæ°æ®æå¡âãâåå¨âåâå¿«éå建âã
ããâå¿«éå建â对è¯æ¡
ããå¨ URL ä¸ï¼é®å ¥è¦å¨åå¨å¸æ·ç URI ä¸ä½¿ç¨çååå称ãè¾å ¥çå称å¯å å« 3-24 个å°ååæ¯åæ°åãæ¤å¼å°æ为ç¨äºå¯¹è®¢é ç Blobãéåæ表èµæºè¿è¡å¯»åç URI ä¸ç主æºåã
ããéæ©è¦å¨å ¶ä¸æ¥æ¾åå¨çåºå/å°ç¼ç»ãå¦æè¦ä½¿ç¨ Windows Azure åºç¨ç¨åºä¸çåå¨ï¼è¯·éæ©è¦å¨å ¶ä¸é¨ç½²è¯¥åºç¨ç¨åºçåºåã
ããè¿å¯ä»¥éæ©å¯ç¨å¼å°å¤å¶ã
ããåå»âå建åå¨å¸æ·âã
ããå建 Java åºç¨ç¨åº
ããå¨æ¬æåä¸ï¼æ¨å°ä½¿ç¨åå¨åè½ï¼è¿äºåè½å¯å¨ Java åºç¨ç¨åºä¸æ¬å°è¿è¡ï¼æå¨ Windows Azure ç Web è§è²æè¾ å©è§è²ä¸è¿è¡ç代ç ä¸è¿è¡ãåå®æ¨å·²ä¸è½½å¹¶å®è£ Java å¼åå·¥å ·å (JDK)ï¼å¹¶å·²æç §ä¸è½½ Windows Azure SDK for Java ä¸ç说æè¿è¡æä½æ¥å®è£ Windows Azure Libraries for Java å Windows Azure SDKï¼è¿å¨ Windows Azure 订é ä¸åå»ºäº Windows Azure åå¨å¸æ·ãæ¨å¯ä»¥ä½¿ç¨ä»»ä½å¼åå·¥å ·ï¼å æ¬âè®°äºæ¬âï¼å建åºç¨ç¨åºãæ¨åªè¦è½å¤ç¼è¯ Java 项ç®åå¼ç¨ Windows Azure Libraries for Java å³å¯ã
ããé ç½®æ¨çåºç¨ç¨åºä»¥è®¿é®éååå¨
ããå°ä¸åå¯¼å ¥è¯å¥æ·»å å°è¦å¨å ¶ä¸ä½¿ç¨ Windows Azure åå¨ API 以访é®éåç Java æ件ç顶é¨ï¼
ãã// Include the following imports to use queue APIs
ããimport com.microsoft.windowsazure.services.core.storage.*;
ããimport com.microsoft.windowsazure.services.queue.client.*;
ãã设置 Windows Azure åå¨è¿æ¥å符串
ããWindows Azure åå¨å®¢æ·ç«¯ä½¿ç¨åå¨è¿æ¥å符串æ¥åå¨ç¨äºè®¿é®æ°æ®ç®¡çæå¡çç»ç»ç¹ååæ®ãå¨å®¢æ·ç«¯åºç¨ç¨åºä¸è¿è¡æ¶ï¼å¿ é¡»æä¾ä»¥ä¸æ ¼å¼çåå¨è¿æ¥å符串ï¼å¹¶å¯¹ AccountName å AccountKey å¼ä½¿ç¨ç®¡çé¨æ·ä¸ååºçåå¨å¸æ·çå称ååå¨å¸æ·ç主访é®å¯é¥ãæ¤ç¤ºä¾æ¼ç¤ºå¦ä½å£°æç¨äºä¿åè¿æ¥å符串çéæå段ï¼
ãã// Define the connection-string with your values
ããpublic static final String storageConnectionString =
ãã"DefaultEndpointsProtocol=http;" +
ãã"AccountName=your_storage_account;" +
ãã"AccountKey=your_storage_account_key";
ããå¨ Windows Azure çè§è²ä¸è¿è¡çåºç¨ç¨åºä¸ï¼æ¤å符串å¯åå¨å¨æå¡é ç½®æ件 ServiceConfiguration.cscfg ä¸ï¼å¹¶å¯éè¿è°ç¨ RoleEnvironment.getConfigurationSettings æ¹æ³è¿è¡è®¿é®ãä¸é¢æ¯ä»æå¡é ç½®æ件ä¸å为 StorageConnectionString ç Setting å ç´ ä¸è·åè¿æ¥å符串ç示ä¾ï¼
ãã// Retrieve storage account from connection-string
ããString storageConnectionString =
ããRoleEnvironment.getConfigurationSettings().get("StorageConnectionString");
ããå¦ä½ï¼å建éå
ããå©ç¨ CloudQueueClient 对象ï¼å¯ä»¥è·å¾éåçå¼ç¨å¯¹è±¡ã以ä¸ä»£ç å°å建 CloudQueueClient 对象ã
ããæ¬æåä¸çææ代ç é½ä½¿ç¨å£°æäºä¸è¿°ä¸¤ç§æ¹å¼ä¹ä¸çåå¨è¿æ¥å符串ãè¿å¯ä»¥éè¿å ¶ä»æ¹å¼å建 CloudStorageAccount 对象ãæå ³è¯¦ç»ä¿¡æ¯ï¼è¯·åé Javadocs ææ¡£ä¸ç CloudStorageAccountã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ããä½¿ç¨ CloudQueueClient 对象è·å对è¦ä½¿ç¨çéåçå¼ç¨ãå¦æéåä¸åå¨ï¼æ¨å¯ä»¥å建å®ã
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Create the queue if it doesn't already exist
ããqueue.createIfNotExist();
ããå¦ä½ï¼åéåæ·»å æ¶æ¯
ããè¥è¦å°æ¶æ¯æå ¥ç°æéåï¼è¯·å å建ä¸æ¡æ°ç CloudQueueMessageãç´§æ¥çï¼è°ç¨ addMessage æ¹æ³ãå¯ä»å符串ï¼UTF-8 æ ¼å¼ï¼æåèæ°ç»å建 CloudQueueMessageã以ä¸ä»£ç å°å建éåï¼å¦æéåä¸åå¨ï¼å¹¶æå ¥æ¶æ¯âHello, Worldâã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Create the queue if it doesn't already exist
ããqueue.createIfNotExist();
ãã// Create a message and add it to the queue
ããCloudQueueMessage message = new CloudQueueMessage("Hello, World");
ããqueue.addMessage(message);
ããå¦ä½ï¼æ¥çä¸ä¸æ¡æ¶æ¯
ããéè¿è°ç¨ peekMessageï¼å¯ä»¥æ¥çéååé¢çæ¶æ¯ï¼èä¸å¿ ä»éåä¸å°å ¶å é¤ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Peek at the next message
ããCloudQueueMessage peekedMessage = queue.peekMessage();
ããå¦ä½ï¼åæ¶å¯¹ä¸ä¸æ¡æ¶æ¯çæé
ããæ¨ç代ç éè¿ä¸¤ä¸ªæ¥éª¤åæ¶å¨éåä¸å¯¹æ¶æ¯çæéãå¨è°ç¨ retrieveMessage æ¶ï¼æ¨å°è·å¾éåä¸çä¸ä¸æ¡æ¶æ¯ãä» retrieveMessage è¿åçæ¶æ¯åå¾å¯¹ä»æ¤éå读åæ¶æ¯çä»»ä½å ¶ä»ä»£ç ä¸å¯è§ãé»è®¤æ åµä¸ï¼æ¤æ¶æ¯å°æç» 30 ç§ä¸å¯è§ãè¥è¦å®å ¨ä»éåä¸å é¤æ¶æ¯ï¼æ¨è¿å¿ é¡»è°ç¨ deleteMessageãæ¤å é¤æ¶æ¯ç两æ¥è¿ç¨å¯ç¡®ä¿å½æ¨ç代ç å 硬件æ软件æ éèæ æ³å¤çæ¶æ¯æ¶ï¼æ¨çå ¶ä»ä»£ç å®ä¾å¯ä»¥è·ååä¸æ¶æ¯å¹¶éè¯ãæ¨ç代ç å¨å¤çæ¶æ¯åä¼ç«å³è°ç¨ deleteMessageã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve the first visible message in the queue
ããCloudQueueMessage retrievedMessage = queue.retrieveMessage();
ãã// Process the message in less than 30 seconds, and then delete the message.
ããqueue.deleteMessage(retrievedMessage);
ããå¦ä½ï¼æ´æ¹å·²æéæ¶æ¯çå 容
ããæ¨å¯ä»¥æ´æ¹éåä¸å·²å°±ä½æ¶æ¯çå 容ãå¦ææ¶æ¯è¡¨ç¤ºå·¥ä½ä»»å¡ï¼åå¯ä»¥ä½¿ç¨æ¤åè½æ´æ°å·¥ä½ä»»å¡çç¶æã以ä¸ä»£ç 使ç¨æ°å 容æ´æ°éåæ¶æ¯ï¼å¹¶å°å¯è§æ§è¶ æ¶è®¾ç½®ä¸ºåå»¶é¿ 60 ç§ãè¿å°ä¿åä¸æ¶æ¯å ³èçå·¥ä½çç¶æï¼å¹¶é¢å¤ä¸ºå®¢æ·ç«¯æä¾ä¸åéçæ¶é´æ¥ç»§ç»å¤çæ¶æ¯ãå¯ä½¿ç¨æ¤æ¹æ³è·è¸ªéåæ¶æ¯ä¸çå¤æ¥éª¤å·¥ä½æµï¼å³ä½¿å¤çæ¥éª¤å 硬件æ软件æ éè失败ï¼ä¹æ éä»å¤´å¼å§æä½ãé常ï¼æ¨è¿å¯ä»¥ä¿çéè¯è®¡æ°ï¼å¦æææ¡æ¶æ¯çéè¯æ¬¡æ°è¶ è¿ n 次ï¼æ¨å°å é¤æ¤æ¶æ¯ãè¿å¯é¿å æ¶æ¯å¨æ¯æ¬¡å¤çæ¶é½è§¦ååºç¨ç¨åºé误ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve the first visible message in the queue
ããCloudQueueMessage message = queue.retrieveMessage();
ãã// Modify the message content and set it to be visible in 60 seconds
ããmessage.setMessageContent("Updated contents.");
ããEnumSet<MessageUpdateFields> updateFields =
ããEnumSet.of(MessageUpdateFields.CONTENT, MessageUpdateFields.VISIBILITY);
ããqueue.updateMessage(message, 60, updateFields, null, null);
ããç¨äºå¯¹æ¶æ¯åæ¶æéçå ¶ä»æ¹æ³
ããæ¨å¯ä»¥éè¿ä¸¤ç§æ¹å¼èªå®ä¹éåä¸çæ¶æ¯æ£ç´¢ãé¦å ï¼æ¨å¯ä»¥è·åä¸æ¹æ¶æ¯ï¼æå¤ 32 个ï¼ãå ¶æ¬¡ï¼æ¨å¯ä»¥è®¾ç½®æ´é¿ææ´ççä¸å¯è§è¶ æ¶ï¼ä»èå 许æ¨ç代ç 使ç¨æ´å¤ææ´å°çæ¶é´æ¥å½»åºå¤çæ¯æ¡æ¶æ¯ã
ãã以ä¸ä»£ç 示ä¾ä½¿ç¨ retrieveMessages æ¹æ³éè¿ä¸æ¬¡è°ç¨è·å 20 æ¡æ¶æ¯ãç¶åï¼å®ä½¿ç¨ for 循ç¯æ¥å¤çæ¯æ¡æ¶æ¯ãå®è¿å°æ¯æ¡æ¶æ¯çä¸å¯è§è¶ æ¶è®¾ç½®ä¸ºäºåéï¼300 ç§ï¼ã请注æï¼è¿äºåéè¶ æ¶å¯¹äºæææ¶æ¯é½æ¯åæ¶å¼å§çï¼å æ¤å¨è°ç¨ retrieveMessages äºåéåï¼å°æªå é¤çä»»ä½æ¶æ¯é½å°å次åå¾å¯è§ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve 20 messages from the queue with a visibility timeout of 300 seconds
ããfor (CloudQueueMessage message : queue.retrieveMessages(20, 300, null, null)) {
ãã// Do processing for all messages in less than 5 minutes,
ãã// deleting each message after processing.
ããqueue.deleteMessage(message);
ãã}
ããå¦ä½ï¼è·åéåé¿åº¦
ããæ¨å¯ä»¥è·å¾éåä¸æ¶æ¯ç估计æ°ãdownloadAttributes æ¹æ³ä¼è¯¢é®éåæå¡ä¸äºå½åå¼ï¼å æ¬éåä¸æ¶æ¯ç计æ°ãæ¤è®¡æ°ä» 为è¿ä¼¼å¼ï¼å 为åªè½å¨éåæå¡ååºæ¨ç请æ±åæ·»å æå é¤æ¶æ¯ãgetApproximateMethodCount æ¹æ³è¿åéè¿è°ç¨ downloadAttributes æ£ç´¢å°çæåä¸ä¸ªå¼ï¼èä¸ä¼è°ç¨éåæå¡ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Download the approximate message count from the server
ããqueue.downloadAttributes();
ãã// Retrieve the newly cached approximate message count
ããlong cachedMessageCount = queue.getApproximateMessageCount();
ããå¦ä½ï¼å é¤éå
ããè¥è¦å é¤éååå ¶å å«çæææ¶æ¯ï¼è¯·å¯¹éå对象è°ç¨ delete æ¹æ³ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Delete the queue
ããqueue.delete();
ããç»å½å° Windows Azure 管çé¨æ·ã
ããå¨å¯¼èªçªæ ¼çåºé¨ï¼åå»âæ°å»ºâã
ãã+æ°å»º
ããä¾æ¬¡åå»âæ°æ®æå¡âãâåå¨âåâå¿«éå建âã
ããâå¿«éå建â对è¯æ¡
ããå¨ URL ä¸ï¼é®å ¥è¦å¨åå¨å¸æ·ç URI ä¸ä½¿ç¨çååå称ãè¾å ¥çå称å¯å å« 3-24 个å°ååæ¯åæ°åãæ¤å¼å°æ为ç¨äºå¯¹è®¢é ç Blobãéåæ表èµæºè¿è¡å¯»åç URI ä¸ç主æºåã
ããéæ©è¦å¨å ¶ä¸æ¥æ¾åå¨çåºå/å°ç¼ç»ãå¦æè¦ä½¿ç¨ Windows Azure åºç¨ç¨åºä¸çåå¨ï¼è¯·éæ©è¦å¨å ¶ä¸é¨ç½²è¯¥åºç¨ç¨åºçåºåã
ããè¿å¯ä»¥éæ©å¯ç¨å¼å°å¤å¶ã
ããåå»âå建åå¨å¸æ·âã
ããå建 Java åºç¨ç¨åº
ããå¨æ¬æåä¸ï¼æ¨å°ä½¿ç¨åå¨åè½ï¼è¿äºåè½å¯å¨ Java åºç¨ç¨åºä¸æ¬å°è¿è¡ï¼æå¨ Windows Azure ç Web è§è²æè¾ å©è§è²ä¸è¿è¡ç代ç ä¸è¿è¡ãåå®æ¨å·²ä¸è½½å¹¶å®è£ Java å¼åå·¥å ·å (JDK)ï¼å¹¶å·²æç §ä¸è½½ Windows Azure SDK for Java ä¸ç说æè¿è¡æä½æ¥å®è£ Windows Azure Libraries for Java å Windows Azure SDKï¼è¿å¨ Windows Azure 订é ä¸åå»ºäº Windows Azure åå¨å¸æ·ãæ¨å¯ä»¥ä½¿ç¨ä»»ä½å¼åå·¥å ·ï¼å æ¬âè®°äºæ¬âï¼å建åºç¨ç¨åºãæ¨åªè¦è½å¤ç¼è¯ Java 项ç®åå¼ç¨ Windows Azure Libraries for Java å³å¯ã
ããé ç½®æ¨çåºç¨ç¨åºä»¥è®¿é®éååå¨
ããå°ä¸åå¯¼å ¥è¯å¥æ·»å å°è¦å¨å ¶ä¸ä½¿ç¨ Windows Azure åå¨ API 以访é®éåç Java æ件ç顶é¨ï¼
ãã// Include the following imports to use queue APIs
ããimport com.microsoft.windowsazure.services.core.storage.*;
ããimport com.microsoft.windowsazure.services.queue.client.*;
ãã设置 Windows Azure åå¨è¿æ¥å符串
ããWindows Azure åå¨å®¢æ·ç«¯ä½¿ç¨åå¨è¿æ¥å符串æ¥åå¨ç¨äºè®¿é®æ°æ®ç®¡çæå¡çç»ç»ç¹ååæ®ãå¨å®¢æ·ç«¯åºç¨ç¨åºä¸è¿è¡æ¶ï¼å¿ é¡»æä¾ä»¥ä¸æ ¼å¼çåå¨è¿æ¥å符串ï¼å¹¶å¯¹ AccountName å AccountKey å¼ä½¿ç¨ç®¡çé¨æ·ä¸ååºçåå¨å¸æ·çå称ååå¨å¸æ·ç主访é®å¯é¥ãæ¤ç¤ºä¾æ¼ç¤ºå¦ä½å£°æç¨äºä¿åè¿æ¥å符串çéæå段ï¼
ãã// Define the connection-string with your values
ããpublic static final String storageConnectionString =
ãã"DefaultEndpointsProtocol=http;" +
ãã"AccountName=your_storage_account;" +
ãã"AccountKey=your_storage_account_key";
ããå¨ Windows Azure çè§è²ä¸è¿è¡çåºç¨ç¨åºä¸ï¼æ¤å符串å¯åå¨å¨æå¡é ç½®æ件 ServiceConfiguration.cscfg ä¸ï¼å¹¶å¯éè¿è°ç¨ RoleEnvironment.getConfigurationSettings æ¹æ³è¿è¡è®¿é®ãä¸é¢æ¯ä»æå¡é ç½®æ件ä¸å为 StorageConnectionString ç Setting å ç´ ä¸è·åè¿æ¥å符串ç示ä¾ï¼
ãã// Retrieve storage account from connection-string
ããString storageConnectionString =
ããRoleEnvironment.getConfigurationSettings().get("StorageConnectionString");
ããå¦ä½ï¼å建éå
ããå©ç¨ CloudQueueClient 对象ï¼å¯ä»¥è·å¾éåçå¼ç¨å¯¹è±¡ã以ä¸ä»£ç å°å建 CloudQueueClient 对象ã
ããæ¬æåä¸çææ代ç é½ä½¿ç¨å£°æäºä¸è¿°ä¸¤ç§æ¹å¼ä¹ä¸çåå¨è¿æ¥å符串ãè¿å¯ä»¥éè¿å ¶ä»æ¹å¼å建 CloudStorageAccount 对象ãæå ³è¯¦ç»ä¿¡æ¯ï¼è¯·åé Javadocs ææ¡£ä¸ç CloudStorageAccountã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ããä½¿ç¨ CloudQueueClient 对象è·å对è¦ä½¿ç¨çéåçå¼ç¨ãå¦æéåä¸åå¨ï¼æ¨å¯ä»¥å建å®ã
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Create the queue if it doesn't already exist
ããqueue.createIfNotExist();
ããå¦ä½ï¼åéåæ·»å æ¶æ¯
ããè¥è¦å°æ¶æ¯æå ¥ç°æéåï¼è¯·å å建ä¸æ¡æ°ç CloudQueueMessageãç´§æ¥çï¼è°ç¨ addMessage æ¹æ³ãå¯ä»å符串ï¼UTF-8 æ ¼å¼ï¼æåèæ°ç»å建 CloudQueueMessageã以ä¸ä»£ç å°å建éåï¼å¦æéåä¸åå¨ï¼å¹¶æå ¥æ¶æ¯âHello, Worldâã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Create the queue if it doesn't already exist
ããqueue.createIfNotExist();
ãã// Create a message and add it to the queue
ããCloudQueueMessage message = new CloudQueueMessage("Hello, World");
ããqueue.addMessage(message);
ããå¦ä½ï¼æ¥çä¸ä¸æ¡æ¶æ¯
ããéè¿è°ç¨ peekMessageï¼å¯ä»¥æ¥çéååé¢çæ¶æ¯ï¼èä¸å¿ ä»éåä¸å°å ¶å é¤ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Peek at the next message
ããCloudQueueMessage peekedMessage = queue.peekMessage();
ããå¦ä½ï¼åæ¶å¯¹ä¸ä¸æ¡æ¶æ¯çæé
ããæ¨ç代ç éè¿ä¸¤ä¸ªæ¥éª¤åæ¶å¨éåä¸å¯¹æ¶æ¯çæéãå¨è°ç¨ retrieveMessage æ¶ï¼æ¨å°è·å¾éåä¸çä¸ä¸æ¡æ¶æ¯ãä» retrieveMessage è¿åçæ¶æ¯åå¾å¯¹ä»æ¤éå读åæ¶æ¯çä»»ä½å ¶ä»ä»£ç ä¸å¯è§ãé»è®¤æ åµä¸ï¼æ¤æ¶æ¯å°æç» 30 ç§ä¸å¯è§ãè¥è¦å®å ¨ä»éåä¸å é¤æ¶æ¯ï¼æ¨è¿å¿ é¡»è°ç¨ deleteMessageãæ¤å é¤æ¶æ¯ç两æ¥è¿ç¨å¯ç¡®ä¿å½æ¨ç代ç å 硬件æ软件æ éèæ æ³å¤çæ¶æ¯æ¶ï¼æ¨çå ¶ä»ä»£ç å®ä¾å¯ä»¥è·ååä¸æ¶æ¯å¹¶éè¯ãæ¨ç代ç å¨å¤çæ¶æ¯åä¼ç«å³è°ç¨ deleteMessageã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve the first visible message in the queue
ããCloudQueueMessage retrievedMessage = queue.retrieveMessage();
ãã// Process the message in less than 30 seconds, and then delete the message.
ããqueue.deleteMessage(retrievedMessage);
ããå¦ä½ï¼æ´æ¹å·²æéæ¶æ¯çå 容
ããæ¨å¯ä»¥æ´æ¹éåä¸å·²å°±ä½æ¶æ¯çå 容ãå¦ææ¶æ¯è¡¨ç¤ºå·¥ä½ä»»å¡ï¼åå¯ä»¥ä½¿ç¨æ¤åè½æ´æ°å·¥ä½ä»»å¡çç¶æã以ä¸ä»£ç 使ç¨æ°å 容æ´æ°éåæ¶æ¯ï¼å¹¶å°å¯è§æ§è¶ æ¶è®¾ç½®ä¸ºåå»¶é¿ 60 ç§ãè¿å°ä¿åä¸æ¶æ¯å ³èçå·¥ä½çç¶æï¼å¹¶é¢å¤ä¸ºå®¢æ·ç«¯æä¾ä¸åéçæ¶é´æ¥ç»§ç»å¤çæ¶æ¯ãå¯ä½¿ç¨æ¤æ¹æ³è·è¸ªéåæ¶æ¯ä¸çå¤æ¥éª¤å·¥ä½æµï¼å³ä½¿å¤çæ¥éª¤å 硬件æ软件æ éè失败ï¼ä¹æ éä»å¤´å¼å§æä½ãé常ï¼æ¨è¿å¯ä»¥ä¿çéè¯è®¡æ°ï¼å¦æææ¡æ¶æ¯çéè¯æ¬¡æ°è¶ è¿ n 次ï¼æ¨å°å é¤æ¤æ¶æ¯ãè¿å¯é¿å æ¶æ¯å¨æ¯æ¬¡å¤çæ¶é½è§¦ååºç¨ç¨åºé误ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve the first visible message in the queue
ããCloudQueueMessage message = queue.retrieveMessage();
ãã// Modify the message content and set it to be visible in 60 seconds
ããmessage.setMessageContent("Updated contents.");
ããEnumSet<MessageUpdateFields> updateFields =
ããEnumSet.of(MessageUpdateFields.CONTENT, MessageUpdateFields.VISIBILITY);
ããqueue.updateMessage(message, 60, updateFields, null, null);
ããç¨äºå¯¹æ¶æ¯åæ¶æéçå ¶ä»æ¹æ³
ããæ¨å¯ä»¥éè¿ä¸¤ç§æ¹å¼èªå®ä¹éåä¸çæ¶æ¯æ£ç´¢ãé¦å ï¼æ¨å¯ä»¥è·åä¸æ¹æ¶æ¯ï¼æå¤ 32 个ï¼ãå ¶æ¬¡ï¼æ¨å¯ä»¥è®¾ç½®æ´é¿ææ´ççä¸å¯è§è¶ æ¶ï¼ä»èå 许æ¨ç代ç 使ç¨æ´å¤ææ´å°çæ¶é´æ¥å½»åºå¤çæ¯æ¡æ¶æ¯ã
ãã以ä¸ä»£ç 示ä¾ä½¿ç¨ retrieveMessages æ¹æ³éè¿ä¸æ¬¡è°ç¨è·å 20 æ¡æ¶æ¯ãç¶åï¼å®ä½¿ç¨ for 循ç¯æ¥å¤çæ¯æ¡æ¶æ¯ãå®è¿å°æ¯æ¡æ¶æ¯çä¸å¯è§è¶ æ¶è®¾ç½®ä¸ºäºåéï¼300 ç§ï¼ã请注æï¼è¿äºåéè¶ æ¶å¯¹äºæææ¶æ¯é½æ¯åæ¶å¼å§çï¼å æ¤å¨è°ç¨ retrieveMessages äºåéåï¼å°æªå é¤çä»»ä½æ¶æ¯é½å°å次åå¾å¯è§ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Retrieve 20 messages from the queue with a visibility timeout of 300 seconds
ããfor (CloudQueueMessage message : queue.retrieveMessages(20, 300, null, null)) {
ãã// Do processing for all messages in less than 5 minutes,
ãã// deleting each message after processing.
ããqueue.deleteMessage(message);
ãã}
ããå¦ä½ï¼è·åéåé¿åº¦
ããæ¨å¯ä»¥è·å¾éåä¸æ¶æ¯ç估计æ°ãdownloadAttributes æ¹æ³ä¼è¯¢é®éåæå¡ä¸äºå½åå¼ï¼å æ¬éåä¸æ¶æ¯ç计æ°ãæ¤è®¡æ°ä» 为è¿ä¼¼å¼ï¼å 为åªè½å¨éåæå¡ååºæ¨ç请æ±åæ·»å æå é¤æ¶æ¯ãgetApproximateMethodCount æ¹æ³è¿åéè¿è°ç¨ downloadAttributes æ£ç´¢å°çæåä¸ä¸ªå¼ï¼èä¸ä¼è°ç¨éåæå¡ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Download the approximate message count from the server
ããqueue.downloadAttributes();
ãã// Retrieve the newly cached approximate message count
ããlong cachedMessageCount = queue.getApproximateMessageCount();
ããå¦ä½ï¼å é¤éå
ããè¥è¦å é¤éååå ¶å å«çæææ¶æ¯ï¼è¯·å¯¹éå对象è°ç¨ delete æ¹æ³ã
ãã// Retrieve storage account from connection-string
ããCloudStorageAccount storageAccount =
ããCloudStorageAccount.parse(storageConnectionString);
ãã// Create the queue client
ããCloudQueueClient queueClient = storageAccount.createCloudQueueClient();
ãã// Retrieve a reference to a queue
ããCloudQueue queue = queueClient.getQueueReference("myqueue");
ãã// Delete the queue
ããqueue.delete();
温馨提示:答案为网友推荐,仅供参考