RabbitMQ-C 客户端接口使用说明

  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

转载于:https://www.cnblogs.com/yorkyang/p/6294049.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值