Cmakelist.txt之Liunx-rabbitmq

[复制链接]
发表于 2025-12-20 16:34:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
1.cmakelist.txt
  1. cmake_minimum_required(VERSION 3.16)
  2. ​
  3. project(rabbitmq_linux_test LANGUAGES C)
  4. ​
  5. add_library(examples-common OBJECT)
  6. target_sources(examples-common PRIVATE
  7.   utils.h
  8.   utils.c)
  9. if(WIN32)
  10.   target_sources(examples-common PRIVATE win32/platform_utils.c)
  11. else()
  12.   target_sources(examples-common PRIVATE unix/platform_utils.c)
  13. endif()
  14. target_link_libraries(examples-common PRIVATE rabbitmq)
  15. ​
  16. add_executable(rabbitmq_linux_test main.c)
  17. ​
  18. ​
  19. target_link_libraries(rabbitmq_linux_test examples-common rabbitmq)
  20. ​
  21. ​
  22. include(GNUInstallDirs)
  23. install(TARGETS rabbitmq_linux_test
  24.    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  25.    RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  26. )
复制代码
  1. ​
复制代码

2.测试代码
  1. // Copyright 2007 - 2021, Alan Antonuk and the rabbitmq-c contributors.
  2. // SPDX-License-Identifier: mit
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <rabbitmq-c/amqp.h>
  8. #include <rabbitmq-c/tcp_socket.h>
  9. #include "utils.h"
  10. #define SUMMARY_EVERY_US 1000000
  11. static void send_batch(amqp_connection_state_t conn, char const *queue_name,
  12.                        int rate_limit, int message_count) {
  13.     uint64_t start_time = now_microseconds();
  14.     int i;
  15.     int sent = 0;
  16.     int previous_sent = 0;
  17.     uint64_t previous_report_time = start_time;
  18.     uint64_t next_summary_time = start_time + SUMMARY_EVERY_US;
  19.     char message[256];
  20.     amqp_bytes_t message_bytes;
  21.     for (i = 0; i < (int)sizeof(message); i++) {
  22.         message[i] = i & 0xff;
  23.     }
  24.     message_bytes.len = sizeof(message);
  25.     message_bytes.bytes = message;
  26.     for (i = 0; i < message_count; i++) {
  27.         uint64_t now = now_microseconds();
  28.         die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes("amq.direct"),
  29.                                         amqp_cstring_bytes(queue_name), 0, 0, NULL,
  30.                                         message_bytes),
  31.                      "Publishing");
  32.         sent++;
  33.         if (now > next_summary_time) {
  34.             int countOverInterval = sent - previous_sent;
  35.             double intervalRate =
  36.                 countOverInterval / ((now - previous_report_time) / 1000000.0);
  37.             printf("%d ms: Sent %d - %d since last report (%d Hz)\n",
  38.                    (int)(now - start_time) / 1000, sent, countOverInterval,
  39.                    (int)intervalRate);
  40.             previous_sent = sent;
  41.             previous_report_time = now;
  42.             next_summary_time += SUMMARY_EVERY_US;
  43.         }
  44.         while (((i * 1000000.0) / (now - start_time)) > rate_limit) {
  45.             microsleep(2000);
  46.             now = now_microseconds();
  47.         }
  48.     }
  49.     {
  50.         uint64_t stop_time = now_microseconds();
  51.         int total_delta = (int)(stop_time - start_time);
  52.         printf("PRODUCER - Message count: %d\n", message_count);
  53.         printf("Total time, milliseconds: %d\n", total_delta / 1000);
  54.         printf("Overall messages-per-second: %g\n",
  55.                (message_count / (total_delta / 1000000.0)));
  56.     }
  57. }
  58. int main(int argc, char const *const *argv) {
  59.     // char const *hostname;
  60.     // int port, status;
  61.     // int rate_limit;
  62.     // int message_count;
  63.     amqp_socket_t *socket = NULL;
  64.     amqp_connection_state_t conn;
  65.     // if (argc < 5) {
  66.     //     fprintf(stderr,
  67.     //             "Usage: amqp_producer host port rate_limit message_count\n");
  68.     //     return 1;
  69.     // }
  70.     // hostname = argv[1];
  71.     // port = atoi(argv[2]);
  72.     // rate_limit = atoi(argv[3]);
  73.     // message_count = atoi(argv[4]);
  74.     char const *hostname = "localhost";// 直接定义主机名,这里假设为localhost
  75.     int port = 5672; // 直接定义端口号,RabbitMQ默认端口是5672
  76.     int rate_limit = 100; // 直接定义速率限制,这里假设为100
  77.     int message_count = 1000; // 直接定义消息数量,这里假设为1000
  78.     int status;
  79.     conn = amqp_new_connection();
  80.     socket = amqp_tcp_socket_new(conn);
  81.     if (!socket) {
  82.         die("creating TCP socket");
  83.     }
  84.     status = amqp_socket_open(socket, hostname, port);
  85.     if (status) {
  86.         die("opening TCP socket");
  87.     }
  88.     die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  89.                                  "guest", "guest"),
  90.                       "Logging in");
  91.     amqp_channel_open(conn, 1);
  92.     die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  93.     send_batch(conn, "test queue", rate_limit, message_count);
  94.     die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  95.                       "Closing channel");
  96.     die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  97.                       "Closing connection");
  98.     die_on_error(amqp_destroy_connection(conn), "Ending connection");
  99.     return 0;
  100. }
复制代码
  1. ​
复制代码
3.结果


回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表