MQ 发送普通消息
请参考以下示例代码进行消息发送。
- #include "ONSFactory.h"
- #include "ONSClientException.h"
- using namespace ons;
- int main()
- {
-     //创建producer和发送消息所必需的信息;
-     ONSFactoryProperty factoryInfo;   
-     factoryInfo.setFactoryProperty(ONSFactoryProperty::ProducerId, "XXX");//您在控制台创建的Producer ID
-     factoryInfo.setFactoryProperty(ONSFactoryProperty::PublishTopics,"XXX" );// 消息内容
-     factoryInfo.setFactoryProperty(ONSFactoryProperty::MsgContent, "XXX");//消息内容
-     factoryInfo.setFactoryProperty(ONSFactoryProperty::AccessKey, "XXX");//AccessKey 阿里云身份验证,在阿里云服务器管理控制台创建
-     factoryInfo.setFactoryProperty(ONSFactoryProperty::SecretKey, "XXX" );//SecretKey 阿里云身份验证,在阿里云服务器管理控制台创建
-     //create producer;
-     Producer *pProducer = ONSFactory::getInstance()->createProducer(factoryInfo);
-     //在发送消息前,必须调用start方法来启动Producer,只需调用一次即可;
-     pProducer->start();
-     Message msg(
-             //Message Topic
-             factoryInfo.getPublishTopics(),
-             //Message Tag,可理解为Gmail中的标签,对消息进行再归类,方便Consumer指定过滤条件在MQ服务器过滤       
-             "TagA",
-             //Message Body,不能为空,MQ不做任何干预,需要Producer与Consumer协商好一致的序列化和反序列化方式
-             factoryInfo.getMessageContent()
-     );
-     // 设置代表消息的业务关键属性,请尽可能全局唯一
-     // 以方便您在无法正常收到消息情况下,可通过 MQ 控制台查询消息并补发
-     // 注意:不设置也不会影响消息正常收发
-     msg.setKey("ORDERID_100");
-     //发送消息,只要不抛出异常,就代表发送成功     
-     try
-     {
-         SendResultONS sendResult = pProducer->send(msg);
-     }
-     catch(ONSClientException & e)
-     {
-         //自定义处理exception的细节
-     }
-     // 在应用退出前,必须销毁Producer对象,否则会导致内存泄露等问题
-     pProducer->shutdown();
-     return 0;
- }