Search in sources :

Example 1 with JSONObject

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

the class Queue method sendMessage.

/**
 * 发送消息,接口在1.0.7中将被废弃
 * @param msgBody 消息内容
 * @return 服务器返回的消息唯一标识
 */
@Deprecated
public String sendMessage(String msgBody, int delayTime) throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    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);
    return jsonObj.getString("msgId");
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) TreeMap(java.util.TreeMap)

Example 2 with JSONObject

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

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

the class Queue method receiveMessage.

/**
 * 获取消息
 * @return 服务器返回消息
 */
public Message receiveMessage() throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("queueName", this.queueName);
    String result = this.client.call("ReceiveMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    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");
    msg.requestId = jsonObj.getString("requestId");
    return msg;
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) TreeMap(java.util.TreeMap)

Example 4 with JSONObject

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

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

the class Topic method publishMessage.

/**
 * publish message .
 *
 * @param msg      String message body
 * @param vTagList Vector<String>  message tag
 * @return msgId String
 * @throws Exception
 */
public String publishMessage(String msg, List<String> vTagList, String routingKey) throws Exception {
    TreeMap<String, String> param = new TreeMap<String, String>();
    param.put("topicName", this.topicName);
    param.put("msgBody", msg);
    if (routingKey != null) {
        param.put("routingKey", routingKey);
    }
    if (vTagList != null) {
        for (int i = 0; i < vTagList.size(); ++i) {
            param.put("msgTag." + Integer.toString(i + 1), vTagList.get(i));
        }
    }
    String result = this.client.call("PublishMessage", param);
    JSONObject jsonObj = new JSONObject(result);
    CMQTool.checkResult(result);
    return jsonObj.getString("msgId");
}
Also used : JSONObject(com.qcloud.cmq.json.JSONObject) 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