package com.qcloud.cmq; import java.util.List; import java.util.TreeMap; import java.util.Vector; import java.lang.Integer; import com.qcloud.cmq.*; import com.qcloud.cmq.Json.*; /** * CMQ_jAVA_SDK_V1.0.2 * Update: Add topic and subscription function * Tips: * 1 Account class object is not safty in multy thread. * 2 You should init different account for topic or queue * Account class * @author York. * Created 2016年9月27日. */ public class Account{ protected CMQClient client; /** * @brief * @param secretId String  * @param secretKey String * @param endpoint String https://cmq-queue-gz.api.qcloud.com * @param method String "POST" or "GET" default: "POST" */ public Account(String endpoint, String secretId, String secretKey){ this.client = new CMQClient(endpoint, "/v2/index.php", secretId, secretKey, "POST"); } /** * @brief * @param secretId String * @param secretKey String * @param endpoint https://cmq-queue-gz.api.qcloud.com * @param path String * @param method String "POST" or "GET" default :"POST" */ public Account(String secretId, String secretKey,String endpoint, String path, String method){ this.client = new CMQClient(endpoint, path, secretId, secretKey, method); } public void setSignMethod(String Method) { this.client.setSignMethod(Method); } /** * create queue * @param queueName String queue name * @param meta QueueMeta class object * @throws Exception * @throws CMQClientException * @throws CMQServerException */ public void createQueue(String queueName,QueueMeta meta) throws Exception { TreeMap param = new TreeMap(); if(queueName.equals("")) throw new CMQClientException("Invalid parameter:queueName is empty"); else param.put("queueName",queueName); if(meta.maxMsgHeapNum > 0) param.put("maxMsgHeapNum",Integer.toString(meta.maxMsgHeapNum)); if(meta.pollingWaitSeconds > 0) param.put("pollingWaitSeconds",Integer.toString(meta.pollingWaitSeconds)); if(meta.visibilityTimeout > 0) param.put("visibilityTimeout",Integer.toString(meta.visibilityTimeout)); if(meta.maxMsgSize > 0) param.put("maxMsgSize",Integer.toString(meta.maxMsgSize)); if(meta.msgRetentionSeconds > 0) param.put("msgRetentionSeconds",Integer.toString(meta.msgRetentionSeconds)); String result = this.client.call("CreateQueue", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * delete queue * * @param queueName String queue name * @throws CMQClientException * @throws CMQServerException */ public void deleteQueue(String queueName) throws Exception { TreeMap param = new TreeMap(); if(queueName.equals("")) throw new CMQClientException("Invalid parameter:queueName is empty"); else param.put("queueName",queueName); String result = this.client.call("DeleteQueue", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * list queue * * @param searchWord String * @param offset int * @param limit int * @param queueList List * @return totalCount int * @throws Exception * @throws CMQClientException * @throws CMQServerException */ public int listQueue(String searchWord, int offset, int limit, List queueList) throws Exception { TreeMap param = new TreeMap(); if(!searchWord.equals("")) param.put("searchWord",searchWord); if(offset >=0 ) param.put("offset",Integer.toString(offset)); if(limit > 0 ) param.put("limit",Integer.toString(limit)); String result = this.client.call("ListQueue", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); int totalCount = jsonObj.getInt("totalCount"); JSONArray jsonArray = jsonObj.getJSONArray("queueList"); for(int i=0;i param = new TreeMap(); if(topicName.equals("")) throw new CMQClientException("Invalid parameter:topicName is empty"); else param.put("topicName",topicName); param.put("filterType",Integer.toString(filterType)); if(maxMsgSize < 1 || maxMsgSize > 65536) throw new CMQClientException("Invalid parameter: maxMsgSize > 65536 or maxMsgSize < 1"); param.put("maxMsgSize",Integer.toString(maxMsgSize)); String result = this.client.call("CreateTopic", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * TODO delete topic * * @param topicName String * @throws Exception int */ public void deleteTopic(final String topicName) throws Exception { TreeMap param = new TreeMap(); if(topicName.equals("")) throw new CMQClientException("Invalid parameter:topicName is empty"); else param.put("topicName",topicName); String result = this.client.call("DeleteTopic", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * TODO list topic * * @param searchWord String * @param vTopicList List * @param offset int * @param limit int * @return totalCount int * @throws Exception */ public int listTopic( final String searchWord, List vTopicList , final int offset ,final int limit) throws Exception { TreeMap param = new TreeMap(); if(!searchWord.equals("")) param.put("searchWord",searchWord); if(offset >=0 ) param.put("offset",Integer.toString(offset)); if(limit > 0 ) param.put("limit",Integer.toString(limit)); String result = this.client.call("ListTopic", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); int totalCount = jsonObj.getInt("totalCount"); JSONArray jsonArray = jsonObj.getJSONArray("topicList"); vTopicList.clear(); for(int i=0;i * @param NotifyStrategy String * @param NotifyContentFormat String * @throws Exception */ public void createSubscribe(final String topicName, final String subscriptionName, final String Endpoint,final String Protocal, final List FilterTag , final ListbindingKey,final String NotifyStrategy, final String NotifyContentFormat) throws Exception { if ( FilterTag != null && FilterTag.size() > 5) throw new CMQClientException("Invalid parameter: Tag number > 5"); TreeMap param = new TreeMap(); if(topicName.equals("")) throw new CMQClientException("Invalid parameter:topicName is empty"); param.put("topicName",topicName); if(subscriptionName.equals("")) throw new CMQClientException("Invalid parameter:subscriptionName is empty"); param.put("subscriptionName", subscriptionName); if(Endpoint.equals("")) throw new CMQClientException("Invalid parameter:Endpoint is empty"); param.put("endpoint", Endpoint); if(Protocal.equals("")) throw new CMQClientException("Invalid parameter:Protocal is empty"); param.put("protocol", Protocal); if(NotifyStrategy.equals("")) throw new CMQClientException("Invalid parameter:NotifyStrategy is empty"); param.put("notifyStrategy", NotifyStrategy); if(NotifyContentFormat.equals("") ) throw new CMQClientException("Invalid parameter:NotifyContentFormat is empty"); param.put("notifyContentFormat", NotifyContentFormat); if(FilterTag != null ) { for(int i = 0 ; i < FilterTag.size() ; ++i) param.put("filterTag."+ Integer.toString(i+1),FilterTag.get(i)); } if(bindingKey != null ) { for(int i = 0 ; i < bindingKey.size() ; ++i) param.put("bindingKey."+ Integer.toString(i+1),bindingKey.get(i)); } String result = this.client.call("Subscribe", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * delete subscription . * * @param topicName String * @param subscriptionName String * @throws Exception */ public void deleteSubscribe(final String topicName, final String subscriptionName) throws Exception { TreeMap param = new TreeMap(); if(topicName.equals("")) throw new CMQClientException("Invalid parameter:topicName is empty"); else param.put("topicName",topicName); if(subscriptionName.equals("")) throw new CMQClientException("Invalid parameter:subscriptionName is empty"); else param.put("subscriptionName",subscriptionName); String result = this.client.call("Unsubscribe", param); JSONObject jsonObj = new JSONObject(result); int code = jsonObj.getInt("code"); if(code != 0) throw new CMQServerException(code,jsonObj.getString("message"),jsonObj.getString("requestId")); } /** * TODO get a subscription object. * * @param topicName String * @param subscriptionName String * @return */ public Subscription getSubscription(final String topicName,final String subscriptionName) { return new Subscription(topicName, subscriptionName, this.client); } }