amqp_bind.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. int main(int argc, char const *const *argv) {
  11. char const *hostname;
  12. int port, status;
  13. char const *exchange;
  14. char const *bindingkey;
  15. char const *queue;
  16. amqp_socket_t *socket = NULL;
  17. amqp_connection_state_t conn;
  18. if (argc < 6) {
  19. fprintf(stderr, "Usage: amqp_bind host port exchange bindingkey queue\n");
  20. return 1;
  21. }
  22. hostname = argv[1];
  23. port = atoi(argv[2]);
  24. exchange = argv[3];
  25. bindingkey = argv[4];
  26. queue = argv[5];
  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. amqp_queue_bind(conn, 1, amqp_cstring_bytes(queue),
  42. amqp_cstring_bytes(exchange), amqp_cstring_bytes(bindingkey),
  43. amqp_empty_table);
  44. die_on_amqp_error(amqp_get_rpc_reply(conn), "Unbinding");
  45. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  46. "Closing channel");
  47. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  48. "Closing connection");
  49. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  50. return 0;
  51. }