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