53 lines
1.4 KiB
C++
53 lines
1.4 KiB
C++
#include <rabbitmq-c/amqp.h>
|
|
#include <rabbitmq-c/tcp_socket.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
int main() {
|
|
amqp_connection_state_t conn = amqp_new_connection();
|
|
amqp_socket_t *socket = amqp_tcp_socket_new(conn);
|
|
|
|
if (!socket) {
|
|
fprintf(stderr, "Erreur création socket\n");
|
|
return 1;
|
|
}
|
|
|
|
if (amqp_socket_open(socket, "localhost", 5672)) {
|
|
fprintf(stderr, "Impossible d'ouvrir la connexion\n");
|
|
return 1;
|
|
}
|
|
|
|
amqp_rpc_reply_t reply;
|
|
|
|
reply = amqp_login(conn, "/", 0, 131072, 60,
|
|
AMQP_SASL_METHOD_PLAIN, "guest", "guest");
|
|
if (reply.reply_type != AMQP_RESPONSE_NORMAL) {
|
|
fprintf(stderr, "Erreur login\n");
|
|
return 1;
|
|
}
|
|
|
|
amqp_channel_open(conn, 1);
|
|
amqp_get_rpc_reply(conn);
|
|
|
|
const char *message = "Hello AMQP!";
|
|
amqp_bytes_t message_bytes = amqp_cstring_bytes(message);
|
|
|
|
amqp_basic_publish(conn,
|
|
1,
|
|
amqp_cstring_bytes(""),
|
|
amqp_cstring_bytes("test.queue"),
|
|
0,
|
|
0,
|
|
NULL,
|
|
message_bytes);
|
|
|
|
printf("Message envoyé !\n");
|
|
|
|
amqp_channel_close(conn, 1, AMQP_REPLY_SUCCESS);
|
|
amqp_connection_close(conn, AMQP_REPLY_SUCCESS);
|
|
amqp_destroy_connection(conn);
|
|
|
|
return 0;
|
|
}
|