Debian Codegraph @

examples/amqp_consumer.c

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