1
votes

I have installed rabbitmq-server on my Linux machine. I have installed the AMQP-CPP client library given on the official website of RABBIRMQ.enter link description here

Now I want to connect to rabbitmq-server as a producer and want to publish a message in the queue. I have made a connection handler and the main file is as follow:

int main(int argc, char ** argv) {
    const std:: string exchange = "my-exchange";
    const std:: string routingKey = "my-routing-key";
    const char* message = "HELLO WORLD";

    MyTcpHandler myHandler;
    // address of the server
    cout << "TcpHandler object created.." << endl;
    AMQP:: Address address("amqp://guest:guest@localhost:5672");
    //("amqp://guest:guest@localhost/vhost");
    cout << "address object created.." << endl;
    // create a AMQP connection object
    AMQP:: TcpConnection connection(& myHandler, address);
    cout << "connection object created.." << endl;
    // and create a channel
    AMQP:: TcpChannel channel(& connection);
    cout << "channel object created.." << endl;

    // use the channel object to call the AMQP method you like
    channel.declareExchange("my-exchange", AMQP:: fanout);
    channel.declareQueue("my-queue");
    channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

    cout << "before publish.." << endl;

    // start a transaction
    channel.startTransaction();

    int pubVal = channel.publish(exchange, routingKey, message);

I am not able to connect to queue. Maybe I am not implementing the "monitor" method correctly in MyTcpHandler class: virtual void monitor(AMQP::TcpConnection *connection, int fd, int flags) Can anybody help, please?

1
Hi, Can anyone help me? please...amits
What is the error that you are getting?cantSleepNow
hi @cantSleepNow I am not able to publish a message from Producer side to the queue in my Broker. Even I am NOT able to get connected to the broker. Although my producer is not giving any error as such. I am using C++ client ehich I mentioned above. Please help me out. I have been facing this problem for long time. Thanks in advance.amits

1 Answers

0
votes

This is a little late for an answer. AMQP-CPP wrapper was always a nightmare for me too. Well if you are not looking for some very specific functionality you can use a different c++ wrapper instead https://github.com/alanxz/SimpleAmqpClient

//SimpleAMQPClient Producer    
Channel::ptr_t connection = Channel::Create("MQ server IP",5672,"username","password");
string producerMessage = "this is a test message";
connection->BasicPublish("exchange","routing key",BasicMessage::Create(producerMessage));

and done !!!

Now if you insist on using AMQP-CPP ,there are few things I would like to point out in above code.

AMQP::Address address("amqp://guest:guest@localhost:5672");

I'm not sure about this address , is it where your server is running,try entering the same in a browser with port as 15672 and see if it gets you a UI for server.

amqp://guest:guest@localhost:15672

// use the channel object to call the AMQP method you like
channel.declareExchange("my-exchange", AMQP::fanout);
channel.declareQueue("my-queue");
channel.bindQueue("my-exchange", "my-queue", "my-routing-key");

This code will declare an exchange and create a queue every time you run it, mostly it will run only first time ,then there will be a name collision every time ( if my assumptions are correct )

A better approach would be to create the queue from Rabbit-MQ server UI. Make sure you have installed rabbitMQ-server (RabbitMQ-C is the client) https://www.rabbitmq.com/download.html#server-installation-guides and send messages via c++ code.

Now all you need to do is open a browser and enter the following link

IP on which your server is running : 15672

for local it should be 127.0.0.1:15672 (mind the port number, its 15672) and enter username and password.

//SimpleAMQPClient Consumer    
string consumer_tag=connection->BasicConsume("queue","",true,false);
Envelope::ptr_t envelope = connection->BasicConsumeMessage(consumer_tag);
BasicMessage::ptr_t bodyBasicMessage=envelope->Message();
string messageBody=bodyBasicMessage->Body();

The above code for simpleAMQPClient is a working copy in my application.

Hope it helps.