|
| 1 | +#include <string.h> |
| 2 | +#include <iostream> |
| 3 | + |
| 4 | +#include <amqp.h> |
| 5 | +#include <amqp_tcp_socket.h> |
| 6 | + |
| 7 | +int main(int argc, char const *const *argv) |
| 8 | +{ |
| 9 | + amqp_connection_state_t conn = amqp_new_connection(); |
| 10 | + amqp_socket_t *socket = amqp_tcp_socket_new(conn); |
| 11 | + amqp_socket_open(socket, "localhost", AMQP_PROTOCOL_PORT); |
| 12 | + amqp_login(conn, "/", 0, AMQP_DEFAULT_FRAME_SIZE, 0, AMQP_SASL_METHOD_PLAIN, "guest", "guest"); |
| 13 | + const amqp_channel_t KChannel = 1; |
| 14 | + amqp_channel_open(conn, KChannel); |
| 15 | + |
| 16 | + amqp_bytes_t queueName(amqp_cstring_bytes("hello")); |
| 17 | + amqp_queue_declare(conn, KChannel, queueName, false, false, false, false, amqp_empty_table); |
| 18 | + |
| 19 | + amqp_confirm_select(conn, KChannel); |
| 20 | + amqp_basic_publish(conn, KChannel, amqp_empty_bytes, /* routing key*/ queueName, false, false, nullptr, amqp_cstring_bytes("Hello World!")); |
| 21 | + amqp_basic_publish(conn, KChannel, amqp_empty_bytes, /* routing key*/ queueName, false, false, nullptr, amqp_cstring_bytes("Hello World!")); |
| 22 | + |
| 23 | + amqp_frame_t frame; |
| 24 | + amqp_simple_wait_frame(conn, &frame); |
| 25 | + if (frame.channel == KChannel) |
| 26 | + { |
| 27 | + if (frame.payload.method.id == AMQP_BASIC_ACK_METHOD) |
| 28 | + { |
| 29 | + amqp_basic_ack_t *ack = (amqp_basic_ack_t *)frame.payload.method.decoded; |
| 30 | + if (ack->multiple) |
| 31 | + std::cout << "Sucessfully sent messages up to delivery tag: " << ack->delivery_tag << std::endl; |
| 32 | + else |
| 33 | + std::cout << "Sucessfully sent message with delivery tag: " << ack->delivery_tag << std::endl; |
| 34 | + } |
| 35 | + else if (frame.payload.method.id == AMQP_BASIC_RETURN_METHOD) |
| 36 | + { |
| 37 | + // message wasn't routed to a queue, but returned |
| 38 | + amqp_message_t returned_message; |
| 39 | + amqp_read_message(conn, 1, &returned_message, 0); |
| 40 | + amqp_destroy_message(&returned_message); |
| 41 | + |
| 42 | + amqp_simple_wait_frame(conn, &frame); |
| 43 | + if (frame.payload.method.id == AMQP_BASIC_ACK_METHOD) |
| 44 | + std::cout << "Message returned" << std::endl; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + amqp_channel_close(conn, KChannel, AMQP_REPLY_SUCCESS); |
| 49 | + amqp_connection_close(conn, AMQP_REPLY_SUCCESS); |
| 50 | + amqp_destroy_connection(conn); |
| 51 | + return 0; |
| 52 | +} |
0 commit comments