Search in sources :

Example 1 with JSONArray

use of com.qcloud.cmq.json.JSONArray in project cmq-java-sdk by tencentyun.

the class Queue method batchSendMessage.

/**
 * 批量发送消息,接口在1.0.7中将被废弃
 * @param vtMsgBody 消息列表
 * @return 服务器返回的消息唯一标识列表
 */
@Deprecated
public List<String> batchSendMessage(List<String> vtMsgBody, int delayTime) throws Exception {
    if (vtMsgBody.isEmpty() || vtMsgBody.size() > 16) {
        throw new CMQClientException("Error: message size is empty or more than 16");
    }
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("queueName", this.queueName);
    for (int i = 0; i < vtMsgBody.size(); i++) {
        String k = "msgBody." + Integer.toString(i + 1);
        param.put(k, vtMsgBody.get(i));
    }
    param.put("delaySeconds", Integer.toString(delayTime));
    String result = this.client.call("BatchSendMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    ArrayList<String> vtMsgId = new ArrayList<String>();
    JSONArray jsonArray = jsonObj.getJSONArray("msgList");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = (JSONObject) jsonArray.get(i);
        vtMsgId.add(obj.getString("msgId"));
    }
    return vtMsgId;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.qcloud.cmq.json.JSONArray) TreeMap(java.util.TreeMap)

Example 2 with JSONArray

use of com.qcloud.cmq.json.JSONArray in project cmq-java-sdk by tencentyun.

the class Queue method batchReceiveMessage.

/**
 * 批量获取消息
 *
 * @param numOfMsg 准备获取消息数
 * @return 服务器返回消息列表
 * @throws CMQClientException
 * @throws CMQServerException
 */
public List<Message> batchReceiveMessage(int numOfMsg) throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("queueName", this.queueName);
    param.put("numOfMsg", Integer.toString(numOfMsg));
    String result = this.client.call("BatchReceiveMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    ArrayList<Message> vtMessage = new ArrayList<Message>();
    JSONArray jsonArray = jsonObj.getJSONArray("msgInfoList");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = (JSONObject) jsonArray.get(i);
        Message msg = new Message();
        msg.msgId = obj.getString("msgId");
        msg.receiptHandle = obj.getString("receiptHandle");
        msg.msgBody = obj.getString("msgBody");
        msg.enqueueTime = obj.getLong("enqueueTime");
        msg.nextVisibleTime = obj.getLong("nextVisibleTime");
        msg.firstDequeueTime = obj.getLong("firstDequeueTime");
        msg.dequeueCount = obj.getInt("dequeueCount");
        msg.requestId = jsonObj.getString("requestId");
        vtMessage.add(msg);
    }
    return vtMessage;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.qcloud.cmq.json.JSONArray) TreeMap(java.util.TreeMap)

Example 3 with JSONArray

use of com.qcloud.cmq.json.JSONArray in project cmq-java-sdk by tencentyun.

the class Queue method batchSend.

public List<CmqResponse> batchSend(List<String> vtMsgBody, int delayTime) throws Exception {
    if (vtMsgBody.isEmpty() || vtMsgBody.size() > 16) {
        throw new CMQClientException("Error: message size is empty or more than 16");
    }
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("queueName", this.queueName);
    for (int i = 0; i < vtMsgBody.size(); i++) {
        String k = "msgBody." + (i + 1);
        param.put(k, vtMsgBody.get(i));
    }
    param.put("delaySeconds", Integer.toString(delayTime));
    String result = this.client.call("BatchSendMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    ArrayList<CmqResponse> cmqResponses = new ArrayList<>();
    JSONArray jsonArray = jsonObj.getJSONArray("msgList");
    String requestId = jsonObj.getString("requestId");
    int code = jsonObj.getInt("code");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = (JSONObject) jsonArray.get(i);
        CmqResponse cmqResponse = new CmqResponse();
        cmqResponse.setRequestId(requestId);
        cmqResponse.setMsgId(obj.getString("msgId"));
        cmqResponse.setCode(code);
        cmqResponses.add(cmqResponse);
    }
    return cmqResponses;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) ArrayList(java.util.ArrayList) JSONArray(com.qcloud.cmq.json.JSONArray) TreeMap(java.util.TreeMap) CmqResponse(com.qcloud.cmq.entity.CmqResponse)

Example 4 with JSONArray

use of com.qcloud.cmq.json.JSONArray in project cmq-java-sdk by tencentyun.

the class Subscription method getSubscriptionAttributes.

/**
 * TODO get subscription attributes.
 *
 * @return  subscription meta object
 * @throws Exception
 */
public SubscriptionMeta getSubscriptionAttributes() throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("topicName", this.topicName);
    param.put("subscriptionName", this.subscriptionName);
    String result = this.client.call("GetSubscriptionAttributes", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    SubscriptionMeta meta = new SubscriptionMeta();
    meta.FilterTag = new Vector<String>();
    if (jsonObj.has("endpoint")) {
        meta.Endpoint = jsonObj.getString("endpoint");
    }
    if (jsonObj.has("notifyStrategy")) {
        meta.NotifyStrategy = jsonObj.getString("notifyStrategy");
    }
    if (jsonObj.has("notifyContentFormat")) {
        meta.NotifyContentFormat = jsonObj.getString("notifyContentFormat");
    }
    if (jsonObj.has("protocol")) {
        meta.Protocal = jsonObj.getString("protocol");
    }
    if (jsonObj.has("createTime")) {
        meta.CreateTime = jsonObj.getInt("createTime");
    }
    if (jsonObj.has("lastModifyTime")) {
        meta.LastModifyTime = jsonObj.getInt("lastModifyTime");
    }
    if (jsonObj.has("msgCount")) {
        meta.msgCount = jsonObj.getInt("msgCount");
    }
    if (jsonObj.has("filterTag")) {
        JSONArray jsonArray = jsonObj.getJSONArray("filterTag");
        if (jsonArray.length() > 0 && meta.FilterTag == null) {
            meta.FilterTag = new Vector<String>();
        }
        for (int i = 0; i < jsonArray.length(); i++) {
            meta.FilterTag.add(jsonArray.getString(i));
        }
    }
    if (jsonObj.has("bindingKey")) {
        JSONArray jsonArray = jsonObj.getJSONArray("bindingKey");
        if (jsonArray.length() > 0 && meta.bindingKey == null) {
            meta.bindingKey = new Vector<String>();
        }
        for (int i = 0; i < jsonArray.length(); i++) {
            meta.bindingKey.add(jsonArray.getString(i));
        }
    }
    return meta;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) JSONArray(com.qcloud.cmq.json.JSONArray) TreeMap(java.util.TreeMap)

Example 5 with JSONArray

use of com.qcloud.cmq.json.JSONArray in project cmq-java-sdk by tencentyun.

the class Topic method ListSubscription.

/**
 * TODO list subscription by topic.
 *
 * @param offset            int
 * @param limit             int
 * @param searchWord        String
 * @param vSubscriptionList List<String>
 * @return totalCount          int
 * @throws Exception
 */
public int ListSubscription(final int offset, int limit, final String searchWord, List<String> vSubscriptionList) throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("topicName", this.topicName);
    if (searchWord != null && !"".equals(searchWord)) {
        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("ListSubscriptionByTopic", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    int totalCount = jsonObj.getInt("totalCount");
    if (!jsonObj.has("subscriptionList")) {
        return 0;
    }
    JSONArray jsonArray = jsonObj.getJSONArray("subscriptionList");
    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject obj = (JSONObject) jsonArray.get(i);
        vSubscriptionList.add(obj.getString("subscriptionName"));
    }
    return totalCount;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) JSONArray(com.qcloud.cmq.json.JSONArray) TreeMap(java.util.TreeMap)

Aggregations

JSONArray (com.qcloud.cmq.json.JSONArray)6 JSONObject (com.qcloud.cmq.json.JSONObject)6 TreeMap (java.util.TreeMap)6 ArrayList (java.util.ArrayList)3 CmqResponse (com.qcloud.cmq.entity.CmqResponse)1 Vector (java.util.Vector)1