zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    Membership和Role Providers
    浏览器兼容手册
    手机开发与测试的Firefox插件:User Agent Switcher
    控制input输入框的高度
    纯 CSS3 打造的按钮实例
    CSS对各个浏览器兼容
    网页配色的天然范儿
    Jquery的each里面用return false代替break; return ture 代替continue
    li标签float:left,IE6中第二行会空缺一块,ie8和FF正常,怎么解决?
    用XMLHTTP实现无刷新的与server通信
  • 原文地址:https://www.cnblogs.com/yorkyang/p/6294049.html
Copyright © 2011-2022 走看看