Linux C 实现rocketMQ生产者和消费者

一、环境:

PC操作系统:CentOS Linux release 7.9.2009 (Core)
gcc 版本:4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
rocketmq-client-cpp版本:2.2.0

以下demo基于 rocketmq-client-cpp-2.2.0 实现,编译可参考 Linux 下 rocketmq-client-cpp 2.2.0 编译

二、代码:

我这里把生产者和消费者的代码又稍微封装了一下,形成一个可以方便调用的工具模块。
只是简单示范一下函数调用。

1、生产者

tool_product.c

/**************************************************************************************************
**  文件名称:tool_product.c
**  文件描述:rocketMQ生产者工具
**  ===============================================================================================
**  创建信息:| 2022-4-12 | hrx | 创建本模块
**  ===============================================================================================
**************************************************************************************************/
#include <stdio.h>
#include "CCommon.h"
#include "CMessage.h"
#include "CProducer.h"
#include "CSendResult.h"
#include <memory.h>

#include "tool_product.h"

static CProducer *s_producer = NULL;                                                               /* 生产者结构 */

/**************************************************************************************************
**  函数名称:tool_mq_product_packmsg
**  功能描述:消息打包
**  输入参数:*topic:消息的主题
**           *tag:消息的tag
**           *key:消息的key
**           *msgstr:消息内容
**  输出参数:无
**  返回参数:消息结构体(用完需要被释放)
**************************************************************************************************/
CMessage *tool_mq_product_create_msg(char *topic, char *tag, char *key, char *msgstr)
{
   
    
    
    CMessage *msg;                                                                                 /* 消息结构体 */
    
    if (NULL == topic || NULL == tag || NULL == key || NULL == msgstr) {
   
    
    
        return NULL;
    }
    
    msg = CreateMessage(topic);
    SetMessageTags(msg, tag);
    SetMessageKeys(msg, key);
    SetMessageBody(msg, msgstr);

    return msg;
}

/**************************************************************************************************
**  函数名称:tool_mq_product_destory_msg
**  功能描述:销毁消息
**  输入参数:*msg:消息结构体
**  输出参数:无
**  返回参数:无
**************************************************************************************************/
void tool_mq_product_destory_msg(CMessage *msg)
{
   
    
    
    if (msg) {
   
    
    
        DestroyMessage(msg);
    }
}

/**************************************************************************************************
**  函数名称:tool_mq_product_sendmsg
**  功能描述:rocketMQ消息队列客户端发送消息
**  输入参数:*msgstr:消息内容
**            msglen:消息内容长度
**  输出参数:无
**  返回参数:成功:0, 失败-1
**************************************************************************************************/
int tool_mq_product_sendmsg(CMessage 

猜你喜欢

转载自blog.csdn.net/lang523493505/article/details/124257744