edit-icon download-icon

Query message by message key

Last Updated: Jan 30, 2018

OnsMessageGetByMsgKey interface returns a list of matching messages by performing a fuzzy query with the transferred Topic and MsgKey.

Usage

Querying a message by MsgKey is a fuzzy query. Because the business party’s keys may not be unique, there may be multiple query results. This interface is usually used when the business party cannot retrieve the MsgId, in which case, the business party needs to perform a fuzzy query to retrieve a list of MsgIds, then calls the OnsMessageGetByMsgId interface to query the message.

Request parameters

Name Type Required Description
OnsRegionId String Yes The region where the queried MQ is currently located. It can be obtained through the OnsRegionList method.
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.
Topic String Yes Topic of the message to be queried
Key String Yes MsgKey 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 List(OnsRestMessageDo) Query result

OnsRestMessageDo 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 Broker
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
  • OnsMesageGetByMsgId: precisely queries a message by MsgId
  • OnsMessageGetByTopic: queries a time range by Topic

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. OnsMessageGetByKeyRequest request = new OnsMessageGetByKeyRequest();
  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.setTopic("MingduanTest");
  27. request.setKey("hello");
  28. try {
  29. OnsMessageGetByKeyResponse response = iAcsClient.getAcsResponse(request);
  30. List<OnsMessageGetByKeyResponse.OnsRestMessageDo> onsRestMessageDoList=response.getData();
  31. for(OnsMessageGetByKeyResponse.OnsRestMessageDo onsRestMessageDo:onsRestMessageDoList){
  32. byte[] messageBody = Base64.decode(onsRestMessageDo.getBody());
  33. String message =new String (messageBody);
  34. System.out.println(onsRestMessageDo.getTopic()+" "+message+" "+
  35. onsRestMessageDo.getFlag()+" "+
  36. onsRestMessageDo.getBornHost()+" "+
  37. onsRestMessageDo.getStoreSize()+" "+
  38. onsRestMessageDo.getStoreHost()+" "+
  39. onsRestMessageDo.getStoreTimestamp()+" "+
  40. onsRestMessageDo.getReconsumeTimes());
  41. }
  42. } catch (ServerException e) {
  43. e.printStackTrace();
  44. } catch (ClientException e) {
  45. e.printStackTrace();
  46. }
Thank you! We've received your feedback.