Exemple #1
0
void Channel::BasicAck(const Envelope::ptr_t& message)
{
  m_impl->CheckIsConnected();
  // Delivery tag is local to the channel, so its important to use
  // that channel, sadly this can cause the channel to throw an exception
  // which will show up as an unrelated exception in a different method
  // that actually waits for a response from the broker
  amqp_channel_t channel = message->DeliveryChannel();
  if (!m_impl->IsChannelOpen(channel))
  {
    throw std::runtime_error("The channel that the message was delivered on has been closed");
  }

	m_impl->CheckForError(amqp_basic_ack(m_impl->m_connection, channel,
    message->DeliveryTag(), false));
}
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;
}