package com.aliyun.mns.samples.Queue; import com.aliyun.mns.client.CloudAccount; import com.aliyun.mns.client.CloudQueue; import com.aliyun.mns.client.MNSClient; import com.aliyun.mns.common.ClientException; import com.aliyun.mns.common.ServiceException; import com.aliyun.mns.common.utils.ServiceSettings; import com.aliyun.mns.model.QueueMeta; public class CreateQueueDemo { public static void main(String[] args) { CloudAccount account = new CloudAccount( ServiceSettings.getMNSAccessKeyId(), ServiceSettings.getMNSAccessKeySecret(), ServiceSettings.getMNSAccountEndpoint()); MNSClient client = account.getMNSClient(); //this client need only initialize once try { //Create Queue QueueMeta qMeta = new QueueMeta(); qMeta.setQueueName("cloud-queue-demo"); qMeta.setPollingWaitSeconds(30);//use long polling when queue is empty. CloudQueue cQueue = client.createQueue(qMeta); System.out.println("Create queue successfully. URL: " + cQueue.getQueueURL()); } 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.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 in following link. https://help.aliyun.com/document_detail/mns/api_reference/error_code/error_code.html?spm=5176.docmns/api_reference/error_code/error_response */ se.printStackTrace(); } catch (Exception e) { System.out.println("Unknown exception happened!"); e.printStackTrace(); } client.close(); } }