amqp_exchange_declare.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 *exchangetype;
  15. amqp_socket_t *socket = NULL;
  16. amqp_connection_state_t conn;
  17. if (argc < 5) {
  18. fprintf(stderr,
  19. "Usage: amqp_exchange_declare host port exchange exchangetype\n");
  20. return 1;
  21. }
  22. hostname = argv[1];
  23. port = atoi(argv[2]);
  24. exchange = argv[3];
  25. exchangetype = argv[4];
  26. conn = amqp_new_connection();
  27. socket = amqp_tcp_socket_new(conn);
  28. if (!socket) {
  29. die("creating TCP socket");
  30. }
  31. status = amqp_socket_open(socket, hostname, port);
  32. if (status) {
  33. die("opening TCP socket");
  34. }
  35. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  36. "guest", "guest"),
  37. "Logging in");
  38. amqp_channel_open(conn, 1);
  39. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  40. amqp_exchange_declare(conn, 1, amqp_cstring_bytes(exchange),
  41. amqp_cstring_bytes(exchangetype), 0, 0, 0, 0,
  42. amqp_empty_table);
  43. die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange");
  44. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  45. "Closing channel");
  46. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  47. "Closing connection");
  48. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  49. return 0;
  50. }