Search in sources :

Example 6 with JSONObject

use of com.qcloud.cmq.json.JSONObject 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 7 with JSONObject

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

the class Queue method deleteMessage.

/**
 * 删除消息
 *
 * @param receiptHandle 消息句柄,获取消息时由服务器返回
 * @throws CMQClientException
 * @throws CMQServerException
 */
public CmqResponse deleteMessage(String receiptHandle) throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("queueName", this.queueName);
    param.put("receiptHandle", receiptHandle);
    String result = this.client.call("DeleteMessage", param);
    CMQTool.checkResult(result);
    JSONObject jsonObject = new JSONObject(result);
    CmqResponse cmqResponse = new CmqResponse();
    cmqResponse.setRequestId(jsonObject.getString("requestId"));
    return cmqResponse;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) TreeMap(java.util.TreeMap) CmqResponse(com.qcloud.cmq.entity.CmqResponse)

Example 8 with JSONObject

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

the class Queue method send.

public CmqResponse send(String msgBody, int delayTime) throws Exception {
    TreeMap<String, String> param = new TreeMap<>();
    param.put("queueName", this.queueName);
    param.put("msgBody", msgBody);
    param.put("delaySeconds", Integer.toString(delayTime));
    String result = this.client.call("SendMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    CmqResponse cmqResponse = new CmqResponse();
    cmqResponse.setCode(jsonObj.getInt("code"));
    cmqResponse.setMsgId(jsonObj.getString("msgId"));
    cmqResponse.setRequestId(jsonObj.getString("requestId"));
    return cmqResponse;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) TreeMap(java.util.TreeMap) CmqResponse(com.qcloud.cmq.entity.CmqResponse)

Example 9 with JSONObject

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

the class Queue method receiveMessage.

/**
 * 接口在1.0.7中将被废弃,长轮询时间应该由queue端设置,请勿使用
 * @param pollingWaitSeconds
 * @return
 * @throws Exception
 */
@Deprecated
public Message receiveMessage(int pollingWaitSeconds) throws Exception {
    TreeMap<String, String> param = new TreeMap<>();
    param.put("queueName", this.queueName);
    if (pollingWaitSeconds >= 0) {
        param.put("pollingWaitSeconds", Integer.toString(pollingWaitSeconds));
    } else {
        param.put("pollingWaitSeconds", Integer.toString(30000));
    }
    String result = this.client.call("ReceiveMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    int code = jsonObj.getInt("code");
    if (code != 0) {
        throw new CMQServerException(code, jsonObj.getString("message"));
    }
    Message msg = new Message();
    msg.msgId = jsonObj.getString("msgId");
    msg.receiptHandle = jsonObj.getString("receiptHandle");
    msg.msgBody = jsonObj.getString("msgBody");
    msg.enqueueTime = jsonObj.getLong("enqueueTime");
    msg.nextVisibleTime = jsonObj.getLong("nextVisibleTime");
    msg.firstDequeueTime = jsonObj.getLong("firstDequeueTime");
    msg.dequeueCount = jsonObj.getInt("dequeueCount");
    return msg;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) TreeMap(java.util.TreeMap)

Example 10 with JSONObject

use of com.qcloud.cmq.json.JSONObject 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)

Aggregations

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