amqp_listen.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <assert.h>
  10. #include "utils.h"
  11. int main(int argc, char const *const *argv) {
  12. char const *hostname;
  13. int port, status;
  14. char const *exchange;
  15. char const *bindingkey;
  16. amqp_socket_t *socket = NULL;
  17. amqp_connection_state_t conn;
  18. amqp_bytes_t queuename;
  19. if (argc < 5) {
  20. fprintf(stderr, "Usage: amqp_listen host port exchange bindingkey\n");
  21. return 1;
  22. }
  23. hostname = argv[1];
  24. port = atoi(argv[2]);
  25. exchange = argv[3];
  26. bindingkey = argv[4];
  27. conn = amqp_new_connection();
  28. socket = amqp_tcp_socket_new(conn);
  29. if (!socket) {
  30. die("creating TCP socket");
  31. }
  32. status = amqp_socket_open(socket, hostname, port);
  33. if (status) {
  34. die("opening TCP socket");
  35. }
  36. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  37. "guest", "guest"),
  38. "Logging in");
  39. amqp_channel_open(conn, 1);
  40. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  41. {
  42. amqp_queue_declare_ok_t *r = amqp_queue_declare(
  43. conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table);
  44. die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue");
  45. queuename = amqp_bytes_malloc_dup(r->queue);
  46. if (queuename.bytes == NULL) {
  47. fprintf(stderr, "Out of memory while copying queue name");
  48. return 1;
  49. }
  50. }
  51. amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange),
  52. amqp_cstring_bytes(bindingkey), amqp_empty_table);
  53. die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue");
  54. amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0,
  55. amqp_empty_table);
  56. die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming");
  57. {
  58. for (;;) {
  59. amqp_rpc_reply_t res;
  60. amqp_envelope_t envelope;
  61. amqp_maybe_release_buffers(conn);
  62. res = amqp_consume_message(conn, &envelope, NULL, 0);
  63. if (AMQP_RESPONSE_NORMAL != res.reply_type) {
  64. break;
  65. }
  66. printf("Delivery %u, exchange %.*s routingkey %.*s\n",
  67. (unsigned)envelope.delivery_tag, (int)envelope.exchange.len,
  68. (char *)envelope.exchange.bytes, (int)envelope.routing_key.len,
  69. (char *)envelope.routing_key.bytes);
  70. if (envelope.message.properties._flags & AMQP_BASIC_CONTENT_TYPE_FLAG) {
  71. printf("Content-type: %.*s\n",
  72. (int)envelope.message.properties.content_type.len,
  73. (char *)envelope.message.properties.content_type.bytes);
  74. }
  75. printf("----\n");
  76. amqp_dump(envelope.message.body.bytes, envelope.message.body.len);
  77. amqp_destroy_envelope(&envelope);
  78. }
  79. }
  80. amqp_bytes_free(queuename);
  81. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  82. "Closing channel");
  83. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  84. "Closing connection");
  85. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  86. return 0;
  87. }