首页 技术 正文
技术 2022年11月15日
0 收藏 742 点赞 3,260 浏览 3869 个字

  rabbitmq-c是一个用于C语言的,与AMQP server进行交互的client库。AMQP协议为版本0-9-1。rabbitmq-c与server进行交互前需要首先进行login操作,在操作后,可以根据AMQP协议规范,执行一系列操作。

  这里,根据项目需求,只进行部分接口说明,文后附demo的github地址。

接口描述

  • 接口说明:声明一个新的amqp connection

    amqp_connection_state_t amqp_new_connection(void);

  • 接口说明:获取socket

     参数说明:hostname         RabbitMQ server所在主机

       portnumber      RabbitMQ server监听端口

    int amqp_open_socket(char const *hostname, int portnumber);

  • 接口说明:将amqp connection和sockfd进行绑定

    void amqp_set_sockfd(amqp_connection_state_t state,int sockfd);

  • 接口说明:用于登录RabbitMQ server,主要目的为了进行权限管理;

    参数说明:state    amqp connection

vhost   rabbit-mq的虚机主机,是rabbit-mq进行权限管理的最小单位

channel_max  最大链接数,此处设成0即可

frame_max  和客户端通信时所允许的最大的frame size.默认值为131072,增大这个值有助于提高吞吐,降低这个值有利于降低时延

heartbeat 含义未知,默认值填0

sasl_method  用于SSL鉴权,默认值参考后文demo

   amqp_rpc_reply_t amqp_login(amqp_connection_state_t state, char const *vhost,int channel_max,int frame_max,int heartbeat,amqp_sasl_method_enum sasl_method, …);

  • 接口说明:用于关联conn和channel

    amqp_channel_open_ok_t *amqp_channel_open(amqp_connection_state_t state, amqp_channel_t channel);

  • 接口说明:声明declare

   参数说明:state

        channel

exchange

type     “fanout”  “direct” “topic”三选一

passive

curable

arguments

amqp_exchange_declare_ok_t *amqp_exchange_declare(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t exchange, amqp_bytes_t type, amqp_boolean_t passive, amqp_boolean_t durable, amqp_table_t arguments);

  • 接口说明:声明queue

   参数说明:state   amqp connection

channel

queue  queue name

passive

durable  队列是否持久化

exclusive  当前连接不在时,队列是否自动删除

aoto_delete 没有consumer时,队列是否自动删除

arguments 用于拓展参数,比如x-ha-policy用于mirrored queue

   amqp_queue_declare_ok_t *amqp_queue_declare(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, amqp_boolean_t passive, amqp_boolean_t durable, amqp_boolean_t exclusive, amqp_boolean_t auto_delete, amqp_table_t arguments);

  • 接口说明:声明binding

   amqp_queue_bind_ok_t *amqp_queue_bind(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, amqp_bytes_t exchange, amqp_bytes_t routing_key, amqp_tab le_t arguments);

  • 接口说明:qos是 quality of service,我们这里使用主要用于控制预取消息数,避免消息按条数均匀分配,需要和no_ack配合使用

参数说明:state

channel

prefetch_size 以bytes为单位,0为unlimited

prefetch_count 预取的消息条数

global

  amqp_basic_qos_ok_t *amqp_basic_qos(amqp_connection_state_t state, amqp_channel_t channel, uint32_t prefetch_size, uint16_t prefetch_count, amqp_boolean_t global);

  • 接口说明:开始一个queue consumer

参数说明:state

channel

queue

consumer_tag

no_local

no_ack    是否需要确认消息后再从队列中删除消息

exclusive

arguments

  amqp_basic_consume_ok_t *amqp_basic_consume(amqp_connection_state_t state, amqp_channel_t channel, amqp_bytes_t queue, amqp_bytes_t consumer_tag, amqp_boolean_t no_local, amqp_boolean_t no_ack, amqp_boolean_t exclusive, amqp_table_t arguments);

  • 接口说明:发布消息

  参数说明:state

channel

exchange

routing_key  当exchange为默认“”时,此处填写queue_name,当exchange为direct,此处为binding_key

mandatory 参见参考文献2

immediate 同上

properties 更多属性,如何设置消息持久化,参见文后demo

body 消息体

   int amqp_basic_publish(amqp_connection_state_t state,amqp_channel_t channel,amqp_bytes_t exchange,amqp_bytes_t routing_key,amqp_boolean_t mandatory,amqp_boolean_t immediate,struct amqp_basic_properties_t_ const *properties,amqp_bytes_t body);

  amqp_rpc_reply_t amqp_channel_close(amqp_connection_state_t state,amqp_channel_t channel,int code);

  amqp_rpc_reply_t amqp_connection_close(amqp_connection_state_t state,int code);

  int amqp_destroy_connection(amqp_connection_state_t state);

  如何consume消息,参见文后demo。

demo

https://github.com/liuhaobupt/rabbitmq_work_queues_demo-with-rabbit-c-client-lib

其中 rmq_new_task.c和rmq_worker.c对应于RabbitMQ tutorial里的work queues章节(http://www.rabbitmq.com/tutorials/tutorial-two-python.html),emit_log_direct.c和receive_logs_direct.c对应于RabbitMQ tutorial里的routing章节(http://www.rabbitmq.com/tutorials/tutorial-four-python.html),这两个demo覆盖了RabbitMQ的常用应用场景。

编译需要librabbitmq.a库,同时需要rabbitmq-c提供的几个头文件(amqp.h和amqp_framing.h)以及utils.c文件,这些在github project页面均可获得。

参考文献

  1. rabbitmq-c主页 http://hg.rabbitmq.com/rabbitmq-c/summary
  2. http://www.rabbitmq.com/amqp-0-9-1-reference.html

 转自:http://www.cnblogs.com/liuhao/archive/2012/04/13/2445641.html

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,994
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,507
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,350
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,135
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,768
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,845