Beispiel #1
0
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;
}