Search in sources :

Example 1 with CmqResponse

use of com.qcloud.cmq.entity.CmqResponse in project cmq-java-sdk by tencentyun.

the class QueueTest method testSingleMessageNew.

@Test
public void testSingleMessageNew() throws Exception {
    QueueMeta meta = new QueueMeta();
    meta.pollingWaitSeconds = 10;
    meta.visibilityTimeout = 10;
    meta.maxMsgSize = 1048576;
    meta.msgRetentionSeconds = 345600;
    String queueName1 = "testCmqQueue" + System.currentTimeMillis();
    deleteQueueList.add(queueName1);
    // 使用新接口,发送并接收消息,验证消息内容一样,消息进队时间晚于发送时间,出队次数为1次等
    account.createQueue(queueName1, meta);
    LocalDateTime now = LocalDateTime.now();
    Thread.sleep(3000);
    Queue queue = account.getQueue(queueName1);
    Thread.sleep(3000);
    CmqResponse cmqResponse = queue.send("hello world,this is cmq sdk for java");
    Assert.assertTrue(cmqResponse != null && cmqResponse.getMsgId().trim().length() > 0);
    Assert.assertTrue(cmqResponse != null && cmqResponse.getRequestId().trim().length() > 0);
    Message msg = queue.receiveMessage();
    Assert.assertEquals("hello world,this is cmq sdk for java", msg.msgBody);
    Assert.assertEquals(1, msg.dequeueCount);
    Instant instant = Instant.ofEpochSecond(msg.enqueueTime);
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime enqueueTime = LocalDateTime.ofInstant(instant, zone);
    Assert.assertTrue(now.isBefore(enqueueTime));
    Instant dequeueInstant = Instant.ofEpochSecond(msg.firstDequeueTime);
    LocalDateTime dequeueTime = LocalDateTime.ofInstant(dequeueInstant, zone);
    Assert.assertTrue(enqueueTime.isBefore(dequeueTime) || enqueueTime.isEqual(dequeueTime));
    queue.deleteMessage(msg.receiptHandle);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) CmqResponse(com.qcloud.cmq.entity.CmqResponse) Test(org.junit.Test)

Example 2 with CmqResponse

use of com.qcloud.cmq.entity.CmqResponse in project cmq-java-sdk by tencentyun.

the class QueueTest method testSingleMessageReceive.

@Test
public void testSingleMessageReceive() throws Exception {
    QueueMeta meta = new QueueMeta();
    meta.pollingWaitSeconds = 10;
    meta.visibilityTimeout = 10;
    meta.maxMsgSize = 1048576;
    meta.msgRetentionSeconds = 345600;
    String queueName1 = "testCmqQueue" + System.currentTimeMillis();
    deleteQueueList.add(queueName1);
    // 使用新接口,发送并使用老接口接收消息
    account.createQueue(queueName1, meta);
    LocalDateTime now = LocalDateTime.now();
    Thread.sleep(3000);
    Queue queue = account.getQueue(queueName1);
    Thread.sleep(5000);
    CmqResponse cmqResponse = queue.send("hello world,this is cmq sdk for java");
    Assert.assertTrue(cmqResponse != null && cmqResponse.getMsgId().trim().length() > 0);
    Assert.assertTrue(cmqResponse != null && cmqResponse.getRequestId().trim().length() > 0);
    Message msg = queue.receiveMessage(10);
    Assert.assertEquals("hello world,this is cmq sdk for java", msg.msgBody);
    Assert.assertEquals(1, msg.dequeueCount);
    Instant instant = Instant.ofEpochSecond(msg.enqueueTime);
    ZoneId zone = ZoneId.systemDefault();
    LocalDateTime enqueueTime = LocalDateTime.ofInstant(instant, zone);
    Assert.assertTrue(now.isBefore(enqueueTime));
    Instant dequeueInstant = Instant.ofEpochSecond(msg.firstDequeueTime);
    LocalDateTime dequeueTime = LocalDateTime.ofInstant(dequeueInstant, zone);
    Assert.assertTrue(enqueueTime.isBefore(dequeueTime) || enqueueTime.isEqual(dequeueTime));
    queue.deleteMessage(msg.receiptHandle);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) CmqResponse(com.qcloud.cmq.entity.CmqResponse) Test(org.junit.Test)

Example 3 with CmqResponse

use of com.qcloud.cmq.entity.CmqResponse in project cmq-java-sdk by tencentyun.

the class QueueTest method testBatchMessageNew.

@Test
public void testBatchMessageNew() throws Exception {
    // 使用新接口,发送并接收消息,验证消息内容一样,消息进队时间晚于发送时间,出队次数为1次等
    QueueMeta meta = new QueueMeta();
    meta.pollingWaitSeconds = 10;
    meta.visibilityTimeout = 10;
    meta.maxMsgSize = 1048576;
    meta.msgRetentionSeconds = 345600;
    String queueName1 = "testCmqQueue" + System.currentTimeMillis();
    deleteQueueList.add(queueName1);
    account.createQueue(queueName1, meta);
    LocalDateTime now = LocalDateTime.now();
    Thread.sleep(3000);
    Queue queue = account.getQueue(queueName1);
    Thread.sleep(5000);
    ArrayList<String> vtMsgBody = new ArrayList<String>();
    String msgBody = "hello world,this is cmq sdk for java 1";
    vtMsgBody.add(msgBody);
    msgBody = "hello world,this is cmq sdk for java 2";
    vtMsgBody.add(msgBody);
    msgBody = "hello world,this is cmq sdk for java 3";
    vtMsgBody.add(msgBody);
    List<CmqResponse> cmqResponses = queue.batchSend(vtMsgBody);
    Assert.assertTrue(cmqResponses != null && cmqResponses.size() == 3);
    Thread.sleep(2000);
    // 保存服务器返回的消息句柄,用于删除消息
    ArrayList<String> vtReceiptHandle = new ArrayList<>();
    List<Message> msgList = queue.batchReceiveMessage(10);
    Assert.assertTrue(msgList != null && msgList.size() == 3);
    for (int i = 0; i < msgList.size(); i++) {
        Message msg1 = msgList.get(i);
        Instant instant = Instant.ofEpochSecond(msg1.enqueueTime);
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime enqueueTime = LocalDateTime.ofInstant(instant, zone);
        Assert.assertTrue(now.isBefore(enqueueTime));
        Instant dequeueInstant = Instant.ofEpochSecond(msg1.firstDequeueTime);
        LocalDateTime dequeueTime = LocalDateTime.ofInstant(dequeueInstant, zone);
        Assert.assertTrue(enqueueTime.isBefore(dequeueTime) || enqueueTime.isEqual(dequeueTime));
        Assert.assertEquals(1, msg1.dequeueCount);
        vtReceiptHandle.add(msg1.receiptHandle);
    }
    queue.batchDeleteMessage(vtReceiptHandle);
}
Also used : LocalDateTime(java.time.LocalDateTime) ZoneId(java.time.ZoneId) Instant(java.time.Instant) ArrayList(java.util.ArrayList) CmqResponse(com.qcloud.cmq.entity.CmqResponse) Test(org.junit.Test)

Example 4 with CmqResponse

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

use of com.qcloud.cmq.entity.CmqResponse 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)

Aggregations

CmqResponse (com.qcloud.cmq.entity.CmqResponse)8 Instant (java.time.Instant)4 LocalDateTime (java.time.LocalDateTime)4 ZoneId (java.time.ZoneId)4 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 JSONObject (com.qcloud.cmq.json.JSONObject)3 TreeMap (java.util.TreeMap)3 Account (com.qcloud.cmq.Account)1 CMQServerException (com.qcloud.cmq.CMQServerException)1 Queue (com.qcloud.cmq.Queue)1 QueueMeta (com.qcloud.cmq.QueueMeta)1 JSONArray (com.qcloud.cmq.json.JSONArray)1