amqp_consumer.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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 <assert.h>
  42. #include "utils.h"
  43. #define SUMMARY_EVERY_US 1000000
  44. static void run(amqp_connection_state_t conn) {
  45. uint64_t start_time = now_microseconds();
  46. int received = 0;
  47. int previous_received = 0;
  48. uint64_t previous_report_time = start_time;
  49. uint64_t next_summary_time = start_time + SUMMARY_EVERY_US;
  50. amqp_frame_t frame;
  51. uint64_t now;
  52. for (;;) {
  53. amqp_rpc_reply_t ret;
  54. amqp_envelope_t envelope;
  55. now = now_microseconds();
  56. if (now > next_summary_time) {
  57. int countOverInterval = received - previous_received;
  58. double intervalRate =
  59. countOverInterval / ((now - previous_report_time) / 1000000.0);
  60. printf("%d ms: Received %d - %d since last report (%d Hz)\n",
  61. (int)(now - start_time) / 1000, received, countOverInterval,
  62. (int)intervalRate);
  63. previous_received = received;
  64. previous_report_time = now;
  65. next_summary_time += SUMMARY_EVERY_US;
  66. }
  67. amqp_maybe_release_buffers(conn);
  68. ret = amqp_consume_message(conn, &envelope, NULL, 0);
  69. if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
  70. if (AMQP_RESPONSE_LIBRARY_EXCEPTION == ret.reply_type &&
  71. AMQP_STATUS_UNEXPECTED_STATE == ret.library_error) {
  72. if (AMQP_STATUS_OK != amqp_simple_wait_frame(conn, &frame)) {
  73. return;
  74. }
  75. if (AMQP_FRAME_METHOD == frame.frame_type) {
  76. switch (frame.payload.method.id) {
  77. case AMQP_BASIC_ACK_METHOD:
  78. /* if we've turned publisher confirms on, and we've published a
  79. * message here is a message being confirmed.
  80. */
  81. break;
  82. case AMQP_BASIC_RETURN_METHOD:
  83. /* if a published message couldn't be routed and the mandatory
  84. * flag was set this is what would be returned. The message then
  85. * needs to be read.
  86. */
  87. {
  88. amqp_message_t message;
  89. ret = amqp_read_message(conn, frame.channel, &message, 0);
  90. if (AMQP_RESPONSE_NORMAL != ret.reply_type) {
  91. return;
  92. }
  93. amqp_destroy_message(&message);
  94. }
  95. break;
  96. case AMQP_CHANNEL_CLOSE_METHOD:
  97. /* a channel.close method happens when a channel exception occurs,
  98. * this can happen by publishing to an exchange that doesn't exist
  99. * for example.
  100. *
  101. * In this case you would need to open another channel redeclare
  102. * any queues that were declared auto-delete, and restart any
  103. * consumers that were attached to the previous channel.
  104. */
  105. return;
  106. case AMQP_CONNECTION_CLOSE_METHOD:
  107. /* a connection.close method happens when a connection exception
  108. * occurs, this can happen by trying to use a channel that isn't
  109. * open for example.
  110. *
  111. * In this case the whole connection must be restarted.
  112. */
  113. return;
  114. default:
  115. fprintf(stderr, "An unexpected method was received %u\n",
  116. frame.payload.method.id);
  117. return;
  118. }
  119. }
  120. }
  121. } else {
  122. amqp_destroy_envelope(&envelope);
  123. }
  124. received++;
  125. }
  126. }
  127. int main(int argc, char const *const *argv) {
  128. char const *hostname;
  129. int port, status;
  130. char const *exchange;
  131. char const *bindingkey;
  132. amqp_socket_t *socket = NULL;
  133. amqp_connection_state_t conn;
  134. amqp_bytes_t queuename;
  135. if (argc < 3) {
  136. fprintf(stderr, "Usage: amqp_consumer host port\n");
  137. return 1;
  138. }
  139. hostname = argv[1];
  140. port = atoi(argv[2]);
  141. exchange = "amq.direct"; /* argv[3]; */
  142. bindingkey = "test queue"; /* argv[4]; */
  143. conn = amqp_new_connection();
  144. socket = amqp_tcp_socket_new(conn);
  145. if (!socket) {
  146. die("creating TCP socket");
  147. }
  148. status = amqp_socket_open(socket, hostname, port);
  149. if (status) {
  150. die("opening TCP socket");
  151. }
  152. die_on_amqp_error(amqp_login(conn, "/", 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,
  153. "guest", "guest"),
  154. "Logging in");
  155. amqp_channel_open(conn, 1);
  156. die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");
  157. {
  158. amqp_queue_declare_ok_t *r = amqp_queue_declare(
  159. conn, 1, amqp_empty_bytes, 0, 0, 0, 1, amqp_empty_table);
  160. die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue");
  161. queuename = amqp_bytes_malloc_dup(r->queue);
  162. if (queuename.bytes == NULL) {
  163. fprintf(stderr, "Out of memory while copying queue name");
  164. return 1;
  165. }
  166. }
  167. amqp_queue_bind(conn, 1, queuename, amqp_cstring_bytes(exchange),
  168. amqp_cstring_bytes(bindingkey), amqp_empty_table);
  169. die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding queue");
  170. amqp_basic_consume(conn, 1, queuename, amqp_empty_bytes, 0, 1, 0,
  171. amqp_empty_table);
  172. die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming");
  173. run(conn);
  174. die_on_amqp_error(amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS),
  175. "Closing channel");
  176. die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS),
  177. "Closing connection");
  178. die_on_error(amqp_destroy_connection(conn), "Ending connection");
  179. return 0;
  180. }