int main() { char* szBroker = getenv("AMQP_BROKER"); Channel::ptr_t channel; if (szBroker != NULL) channel = Channel::Create(szBroker); else channel = Channel::Create(); channel->DeclareQueue("alanqueue"); channel->BindQueue("alanqueue", "amq.direct", "alankey"); BasicMessage::ptr_t msg_in = BasicMessage::Create(); msg_in->Body("This is a small message."); channel->BasicPublish("amq.direct", "alankey", msg_in); channel->BasicConsume("alanqueue", "consumertag"); BasicMessage::ptr_t msg_out = channel->BasicConsumeMessage("consumertag")->Message(); std::cout << "Message text: " << msg_out->Body() << std::endl; }
void receive(int id) { Channel::ptr_t channel; channel = Channel::Create("192.168.8.102"); std::string e_name = "tieto-chatroom"; //preparation channel->DeclareExchange(e_name, Channel::EXCHANGE_TYPE_FANOUT); std::string q_name = channel->DeclareQueue("", false, true, false); channel->BindQueue(q_name, e_name, "chat"); channel->BasicConsume(q_name, ""); Envelope::ptr_t env; while (true) { if (channel->BasicConsumeMessage(env, -1)) { std::cout << "Received by " << id << ": " << env->Message()->Body() << " [" << env->Exchange() << "]" << std::endl; } else { std::cout << "queue empty" << std::endl; break; } } }
int main() { const std::string EXCHANGE_NAME = "SimpleAmqpClientEnvelopeTest"; const std::string ROUTING_KEY = "SACRoutingKey"; const std::string CONSUMER_TAG = "SACConsumerTag"; try { Channel::ptr_t channel = Channel::Create(); channel->DeclareExchange(EXCHANGE_NAME, Channel::EXCHANGE_TYPE_FANOUT); std::string queue = channel->DeclareQueue(""); channel->BindQueue(queue, EXCHANGE_NAME, ROUTING_KEY); channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody")); channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody2")); channel->BasicPublish(EXCHANGE_NAME, ROUTING_KEY, BasicMessage::Create("MessageBody3")); channel->BasicConsume(queue, CONSUMER_TAG); Envelope::ptr_t env; for (int i = 0; i < 3; ++i) { if (channel->BasicConsumeMessage(env, 0)) { std::cout << "Envelope received: \n" << " Exchange: " << env->Exchange() << "\n Routing key: " << env->RoutingKey() << "\n Consumer tag: " << env->ConsumerTag() << "\n Delivery tag: " << env->DeliveryTag() << "\n Redelivered: " << env->Redelivered() << "\n Body: " << env->Message()->Body() << std::endl; } else { std::cout << "Basic Consume failed.\n"; } } } catch (AmqpResponseServerException& e) { std::cout << "Failure: " << e.what(); } return 0; }