[backcolor=transparent]目前支持的域包括公网、华东1、华北2、华东2、华南1。
定时消息可以做到在指定时间之后才可被消费者消费,用于解决一些消息生产和消费有时间窗口要求的场景,或者通过消息触发定时任务的场景,类似于延迟队列。代码示例如下:
- [backcolor=transparent]#include[backcolor=transparent] [backcolor=transparent]"ONSFactory.h"
- [backcolor=transparent]#include[backcolor=transparent] [backcolor=transparent]"ONSClientException.h"
- [backcolor=transparent]using[backcolor=transparent] [backcolor=transparent]namespace[backcolor=transparent] ons[backcolor=transparent];
- [backcolor=transparent]int[backcolor=transparent] main[backcolor=transparent]()
- [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]//创建producer和发送消息所必需的信息;
- [backcolor=transparent] [backcolor=transparent]ONSFactoryProperty[backcolor=transparent] factoryInfo[backcolor=transparent];
- [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
- [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
- [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
- [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]//阿里云身份验证,在阿里云服务器管理控制台创建
- [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]//阿里云身份验证,在阿里云服务器管理控制台创建
- [backcolor=transparent] [backcolor=transparent]//create producer;
- [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]);
- [backcolor=transparent] [backcolor=transparent]//在发送消息前,必须调用start方法来启动Producer,只需调用一次即可;
- [backcolor=transparent] pProducer[backcolor=transparent]->[backcolor=transparent]start[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]Message[backcolor=transparent] msg[backcolor=transparent](
- [backcolor=transparent] [backcolor=transparent]//Message Topic
- [backcolor=transparent] factoryInfo[backcolor=transparent].[backcolor=transparent]getPublishTopics[backcolor=transparent](),
- [backcolor=transparent] [backcolor=transparent]//Message Tag,可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在 MQ 服务器过滤
- [backcolor=transparent] [backcolor=transparent]"TagA"[backcolor=transparent],
- [backcolor=transparent] [backcolor=transparent]//Message Body, 不能为空,MQ不做任何干预,需要Producer与Consumer协商好一致的序列化和反序列化方式
- [backcolor=transparent] factoryInfo[backcolor=transparent].[backcolor=transparent]getMessageContent[backcolor=transparent]()
- [backcolor=transparent] [backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 设置代表消息的业务关键属性,请尽可能全局唯一。
- [backcolor=transparent] [backcolor=transparent]// 以方便您在无法正常收到消息情况下,可通过 MQ 控制台查询消息并补发。
- [backcolor=transparent] [backcolor=transparent]// 注意:不设置也不会影响消息正常收发
- [backcolor=transparent] msg[backcolor=transparent].[backcolor=transparent]setKey[backcolor=transparent]([backcolor=transparent]"ORDERID_100"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// deliver time 单位 ms,指定一个时刻,在这个时刻之后才能被消费,这个例子表示3s后才能被消费
- [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];
- [backcolor=transparent] msg[backcolor=transparent].[backcolor=transparent]setStartDeliverTime[backcolor=transparent]([backcolor=transparent]deliverTime[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]//发送消息,只要不抛出异常,就代表发送成功
- [backcolor=transparent] [backcolor=transparent]try
- [backcolor=transparent] [backcolor=transparent]{
- [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]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]ONSClientException[backcolor=transparent] [backcolor=transparent]&[backcolor=transparent] e[backcolor=transparent])
- [backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]//自定义处理exception的细节
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]// 在应用退出前,必须销毁Producer对象,否则会导致内存泄露等问题
- [backcolor=transparent] pProducer[backcolor=transparent]->[backcolor=transparent]shutdown[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]0[backcolor=transparent];
- [backcolor=transparent]}