amqp_sendstring.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * ***** BEGIN LICENSE BLOCK *****
  3. * Version: MIT
  4. *
  5. * Portions created by Alan Antonuk are Copyright (c) 2012-2013
  6. * Alan Antonuk. All Rights Reserved.
  7. *
  8. * Portions created by VMware are Copyright (c) 2007-2012 VMware, Inc.
  9. * All Rights Reserved.
  10. *
  11. * Portions created by Tony Garnock-Jones are Copyright (c) 2009-2010
  12. * VMware, Inc. and Tony Garnock-Jones. All Rights Reserved.
  13. *
  14. * Permission is hereby granted, free of charge, to any person
  15. * obtaining a copy of this software and associated documentation
  16. * files (the "Software"), to deal in the Software without
  17. * restriction, including without limitation the rights to use, copy,
  18. * modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is
  20. * furnished to do so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be
  23. * included in all copies or substantial portions of the Software.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. * ***** END LICENSE BLOCK *****
  34. */
  35. #include <stdint.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <amqp.h>
  40. #include <amqp_tcp_socket.h>
  41. #include "utils.h"
  42. int main(int argc, char const *const *argv) {
  43. char const *hostname;
  44. int port, status;
  45. char const *exchange;
  46. char const *routingkey;
  47. char const *messagebody;
  48. amqp_socket_t *socket = NULL;
  49. amqp_connection_state_t conn;
  50. if (argc < 6) {
  51. fprintf(
  52. stderr,
  53. "Usage: amqp_sendstring host port exchange routingkey messagebody\n");
  54. return 1;
  55. }
  56. hostname = argv[1];
  57. port = atoi(argv[2]);
  58. exchange = argv[3];
  59. routingkey = argv[4];
  60. messagebody = argv[5];
  61. conn = amqp_new_connection();
  62. socket = amqp_tcp_socket_new(conn);
  63. if (!socket) {
  64. die("creating TCP socket");
  65. }
  66. status = amqp_socket_open(socket, hostname, port);
  67. if (status) {
  68. die("opening TCP socket");
  69. }
  70. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  71. "guest", "guest"),
  72. "Logging in");
  73. amqp_channel_open(conn, 1);
  74. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  75. {
  76. amqp_basic_properties_t props;
  77. props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
  78. props.content_type = amqp_cstring_bytes("text/plain");
  79. props.delivery_mode = 2; /* persistent delivery mode */
  80. die_on_error(amqp_basic_publish(conn, 1, amqp_cstring_bytes(exchange),
  81. amqp_cstring_bytes(routingkey), 0, 0,
  82. &props, amqp_cstring_bytes(messagebody)),
  83. "Publishing");
  84. }
  85. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  86. "Closing channel");
  87. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  88. "Closing connection");
  89. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  90. return 0;
  91. }