amqp_sendstring.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 *routingkey;
  15. char const *messagebody;
  16. amqp_socket_t *socket = NULL;
  17. amqp_connection_state_t conn;
  18. if (argc < 6) {
  19. fprintf(
  20. stderr,
  21. "Usage: amqp_sendstring host port exchange routingkey messagebody\n");
  22. return 1;
  23. }
  24. hostname = argv[1];
  25. port = atoi(argv[2]);
  26. exchange = argv[3];
  27. routingkey = argv[4];
  28. messagebody = argv[5];
  29. conn = amqp_new_connection();
  30. socket = amqp_tcp_socket_new(conn);
  31. if (!socket) {
  32. die("creating TCP socket");
  33. }
  34. status = amqp_socket_open(socket, hostname, port);
  35. if (status) {
  36. die("opening TCP socket");
  37. }
  38. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  39. "guest", "guest"),
  40. "Logging in");
  41. amqp_channel_open(conn, 1);
  42. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  43. {
  44. amqp_basic_properties_t props;
  45. props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
  46. props.content_type = amqp_cstring_bytes("text/plain");
  47. props.delivery_mode = 2; /* persistent delivery mode */
  48. die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange),
  49. amqp_cstring_bytes(routingkey), 0, 0,
  50. &props, amqp_cstring_bytes(messagebody)),
  51. "Publishing");
  52. }
  53. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  54. "Closing channel");
  55. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  56. "Closing connection");
  57. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  58. return 0;
  59. }