开发者社区> 问答> 正文

C/C++ SDK发送定时消息


[backcolor=transparent]目前支持的域包括公网、华东1、华北2、华东2、华南1。
定时消息可以做到在指定时间之后才可被消费者消费,用于解决一些消息生产和消费有时间窗口要求的场景,或者通过消息触发定时任务的场景,类似于延迟队列。代码示例如下:

  1. [backcolor=transparent]#include[backcolor=transparent] [backcolor=transparent]"ONSFactory.h"
  2. [backcolor=transparent]#include[backcolor=transparent] [backcolor=transparent]"ONSClientException.h"
  3. [backcolor=transparent]using[backcolor=transparent] [backcolor=transparent]namespace[backcolor=transparent] ons[backcolor=transparent];
  4. [backcolor=transparent]int[backcolor=transparent] main[backcolor=transparent]()
  5. [backcolor=transparent]{
  6. [backcolor=transparent]    [backcolor=transparent]//创建producer和发送消息所必需的信息;
  7. [backcolor=transparent]    [backcolor=transparent]ONSFactoryProperty[backcolor=transparent] factoryInfo[backcolor=transparent];
  8. [backcolor=transparent]    factoryInfo[backcolor=transparent].[backcolor=transparent]setFactoryProperty[backcolor=transparent]([backcolor=transparent]ONSFactoryProperty[backcolor=transparent]::[backcolor=transparent]ProducerId[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XXX"[backcolor=transparent]);[backcolor=transparent]//您在MQ控制台创建的producer
  9. [backcolor=transparent]    factoryInfo[backcolor=transparent].[backcolor=transparent]setFactoryProperty[backcolor=transparent]([backcolor=transparent]ONSFactoryProperty[backcolor=transparent]::[backcolor=transparent]PublishTopics[backcolor=transparent],[backcolor=transparent]"XXX"[backcolor=transparent] [backcolor=transparent]);[backcolor=transparent]//您在MQ控制台申请的topic
  10. [backcolor=transparent]    factoryInfo[backcolor=transparent].[backcolor=transparent]setFactoryProperty[backcolor=transparent]([backcolor=transparent]ONSFactoryProperty[backcolor=transparent]::[backcolor=transparent]MsgContent[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"xxx"[backcolor=transparent]);[backcolor=transparent]//msg content
  11. [backcolor=transparent]    factoryInfo[backcolor=transparent].[backcolor=transparent]setFactoryProperty[backcolor=transparent]([backcolor=transparent]ONSFactoryProperty[backcolor=transparent]::[backcolor=transparent]AccessKey[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"xxx"[backcolor=transparent]);[backcolor=transparent]//阿里云身份验证,在阿里云服务器管理控制台创建
  12. [backcolor=transparent]    factoryInfo[backcolor=transparent].[backcolor=transparent]setFactoryProperty[backcolor=transparent]([backcolor=transparent]ONSFactoryProperty[backcolor=transparent]::[backcolor=transparent]SecretKey[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"xxx"[backcolor=transparent] [backcolor=transparent]);[backcolor=transparent]//阿里云身份验证,在阿里云服务器管理控制台创建
  13. [backcolor=transparent]    [backcolor=transparent]//create producer;
  14. [backcolor=transparent]    [backcolor=transparent]Producer[backcolor=transparent] [backcolor=transparent]*[backcolor=transparent]pProducer [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]ONSFactory[backcolor=transparent]::[backcolor=transparent]getInstance[backcolor=transparent]()->[backcolor=transparent]createProducer[backcolor=transparent]([backcolor=transparent]factoryInfo[backcolor=transparent]);
  15. [backcolor=transparent]    [backcolor=transparent]//在发送消息前,必须调用start方法来启动Producer,只需调用一次即可;
  16. [backcolor=transparent]    pProducer[backcolor=transparent]->[backcolor=transparent]start[backcolor=transparent]();
  17. [backcolor=transparent]    [backcolor=transparent]Message[backcolor=transparent] msg[backcolor=transparent](
  18. [backcolor=transparent]            [backcolor=transparent]//Message Topic
  19. [backcolor=transparent]            factoryInfo[backcolor=transparent].[backcolor=transparent]getPublishTopics[backcolor=transparent](),
  20. [backcolor=transparent]            [backcolor=transparent]//Message Tag,可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在 MQ 服务器过滤      
  21. [backcolor=transparent]            [backcolor=transparent]"TagA"[backcolor=transparent],
  22. [backcolor=transparent]            [backcolor=transparent]//Message Body, 不能为空,MQ不做任何干预,需要Producer与Consumer协商好一致的序列化和反序列化方式
  23. [backcolor=transparent]            factoryInfo[backcolor=transparent].[backcolor=transparent]getMessageContent[backcolor=transparent]()
  24. [backcolor=transparent]    [backcolor=transparent]);
  25. [backcolor=transparent]    [backcolor=transparent]// 设置代表消息的业务关键属性,请尽可能全局唯一。
  26. [backcolor=transparent]    [backcolor=transparent]// 以方便您在无法正常收到消息情况下,可通过 MQ 控制台查询消息并补发。
  27. [backcolor=transparent]    [backcolor=transparent]// 注意:不设置也不会影响消息正常收发
  28. [backcolor=transparent]    msg[backcolor=transparent].[backcolor=transparent]setKey[backcolor=transparent]([backcolor=transparent]"ORDERID_100"[backcolor=transparent]);
  29. [backcolor=transparent]    [backcolor=transparent]// deliver time 单位 ms,指定一个时刻,在这个时刻之后才能被消费,这个例子表示3s后才能被消费
  30. [backcolor=transparent]    [backcolor=transparent]long[backcolor=transparent] deliverTime [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]获取系统当前时间([backcolor=transparent]ms[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]3000[backcolor=transparent];
  31. [backcolor=transparent]    msg[backcolor=transparent].[backcolor=transparent]setStartDeliverTime[backcolor=transparent]([backcolor=transparent]deliverTime[backcolor=transparent]);
  32. [backcolor=transparent]    [backcolor=transparent]//发送消息,只要不抛出异常,就代表发送成功    
  33. [backcolor=transparent]    [backcolor=transparent]try
  34. [backcolor=transparent]    [backcolor=transparent]{
  35. [backcolor=transparent]        [backcolor=transparent]SendResultONS[backcolor=transparent] sendResult [backcolor=transparent]=[backcolor=transparent] pProducer[backcolor=transparent]->[backcolor=transparent]send[backcolor=transparent]([backcolor=transparent]msg[backcolor=transparent]);
  36. [backcolor=transparent]    [backcolor=transparent]}
  37. [backcolor=transparent]    [backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]ONSClientException[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent] e[backcolor=transparent])
  38. [backcolor=transparent]    [backcolor=transparent]{
  39. [backcolor=transparent]        [backcolor=transparent]//自定义处理exception的细节
  40. [backcolor=transparent]    [backcolor=transparent]}
  41. [backcolor=transparent]    [backcolor=transparent]// 在应用退出前,必须销毁Producer对象,否则会导致内存泄露等问题
  42. [backcolor=transparent]    pProducer[backcolor=transparent]->[backcolor=transparent]shutdown[backcolor=transparent]();
  43. [backcolor=transparent]    [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
  44. [backcolor=transparent]}

展开
收起
猫饭先生 2017-10-26 14:26:51 1759 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
一个跨平台的云服务SDK需要什么 立即下载
使用C++11开发PHP7扩展 立即下载
GPON Class C++ SFP O;T Transce 立即下载