edit-icon download-icon

Query message by message ID

Last Updated: Jan 30, 2018

OnsMessageGetByMsgId interface queries the information of the specified message with the transferred MsgId, including the sending time, storage server, and key and Tag of a message, and determines if the message has been consumed.

Usage

This function is used for determining if the message is successfully sent, and querying the sending time, target server, and so on.

Querying a message by MsgId is a precise query. MsgId required in the query criterion is retrieved from the SendResult after each successful sending . Therefore, the business party must store results of each sent message.

Request parameters

Name Type Required Description
OnsRegionId String Yes It specifies the region to be queried. The value is obtained through the OnsRegionList interface.
OnsPlatform String No The source of the request, which by default is POP platform.
PreventCache Long Yes It’s used for CSRF verification. Set it to the current system time.
MsgId String Yes MsgId of the message to be queried
Topic String Yes Topic of the message to be queried

Response parameters

Name Type Description
RequestId String A public parameter unique for each request
HelpUrl String A help link
Data Data Query result

Data structure

Name Type Description
Topic String Topic of the message
Flag Integer null
PropertyList List(MessageProperty) List of message properties
Body String The message body
StoreSize Integer The message size
BornTimestamp Long The time stamp of generation
BornHost String Then client instance that generated this message
StoreTimestamp Long Time stamp stored by Server
StoreHost String The server instance that stores the message
MsgId String Message ID
BodyCRC Integer CRC verification value of the message body
ReconsumeTimes Integer Times for re-consuming a message
  • OnsMessageGetByMsgKey: queries a message through a fuzzy matching by MsgKey
  • OnsMessageGetByTopic: queries a message according to the Topic and time range

Example

  1. public static void main(String []args) {
  2. String regionId = "cn-hangzhou";
  3. String accessKey = "XXXXXXXXXXXXXXXXX";
  4. String secretKey = "XXXXXXXXXXXXXXXXX";
  5. String endPointName ="cn-hangzhou";
  6. String productName ="Ons";
  7. String domain ="ons.cn-hangzhou.aliyuncs.com";
  8. /**
  9. *Select Region based on the region you will access, and set the corresponding access point.
  10. */
  11. try {
  12. DefaultProfile.addEndpoint(endPointNameregionIdproductNamedomain);
  13. } catch (ClientException e) {
  14. e.printStackTrace();
  15. }
  16. IClientProfile profile= DefaultProfile.getProfile(regionIdaccessKeysecretKey);
  17. IAcsClient iAcsClient= new DefaultAcsClient(profile);
  18. OnsMessageGetByMsgIdRequest request = new OnsMessageGetByMsgIdRequest();
  19. /**
  20. *OnsRegionId refers to the resource of the region of MQ required to be accessed by API.
  21. *This value must be selected and configured by the list obtained through the OnsRegionList method because OnsRegionId is changing, and cannot be written as a fixed value.
  22. */
  23. request.setOnsRegionId("daily");
  24. request.setPreventCache(System.currentTimeMillis());
  25. request.setAcceptFormat(FormatType.JSON);
  26. request.setMsgId("0ADA91A600002A9F000002ADDA8137E8");
  27. request.setTopic("TopicTest");
  28. try {
  29. OnsMessageGetByMsgIdResponse response = iAcsClient.getAcsResponse(request);
  30. OnsMessageGetByMsgIdResponse.Data data =response.getData();
  31. byte[] msgbody= Base64.decode(data.getBody());
  32. String message= new String(msgbody);
  33. System.out.println(data.getTopic()+" "+
  34. message+" "+
  35. data.getFlag()+" "+
  36. data.getBornHost()+" "+
  37. data.getStoreSize()+" "+
  38. data.getStoreHost()+" "+
  39. data.getStoreTimestamp()+" “+
  40. data.getReconsumeTimes());
  41. } catch (ServerException e) {
  42. e.printStackTrace();
  43. } catch (ClientException e) {
  44. e.printStackTrace();
  45. }
  46. }
Thank you! We've received your feedback.