Ejemplo n.º 1
0
bool Channel::BasicGet(Envelope::ptr_t &envelope, const std::string &queue, bool no_ack)
{
    const boost::array<boost::uint32_t, 2> GET_RESPONSES = { { AMQP_BASIC_GET_OK_METHOD, AMQP_BASIC_GET_EMPTY_METHOD } };
    m_impl->CheckIsConnected();

    amqp_basic_get_t get = {};
    get.queue = amqp_cstring_bytes(queue.c_str());
    get.no_ack = no_ack;

    amqp_channel_t channel = m_impl->GetChannel();
    amqp_frame_t response = m_impl->DoRpcOnChannel(channel, AMQP_BASIC_GET_METHOD, &get, GET_RESPONSES);

    if (AMQP_BASIC_GET_EMPTY_METHOD == response.payload.method.id)
    {
        m_impl->ReturnChannel(channel);
        m_impl->MaybeReleaseBuffersOnChannel(channel);
        return false;
    }

    amqp_basic_get_ok_t *get_ok = (amqp_basic_get_ok_t *)response.payload.method.decoded;
    boost::uint64_t delivery_tag = get_ok->delivery_tag;
    bool redelivered = (get_ok->redelivered == 0 ? false : true);
    std::string exchange((char *)get_ok->exchange.bytes, get_ok->exchange.len);
    std::string routing_key((char *)get_ok->routing_key.bytes, get_ok->routing_key.len);

    BasicMessage::ptr_t message = m_impl->ReadContent(channel);
    envelope = Envelope::Create(message, "", delivery_tag, exchange, redelivered, routing_key, channel);

    m_impl->ReturnChannel(channel);
    m_impl->MaybeReleaseBuffersOnChannel(channel);
    return true;
}
Ejemplo n.º 2
0
void publish() {
    try {
        reconnects++;
        std::cout << "Connecting:" << reconnects << "..." << std::endl;

        srand((unsigned)time(0));
        std::stringstream ss;
        ss << "guest:guest@localhost:" << ports[rand() % 3];

        AMQP amqp(ss.str());

        AMQPExchange * ex = amqp.createExchange("hello-exchange");
        ex->Declare("hello-exchange", "direct");

        AMQPQueue * queue = amqp.createQueue("hello-queue");
        queue->Declare();
        queue->Bind( "hello-exchange", "hola");

        std::cout << "Connected." << std::endl;
        reconnects = 0;

        ex->setHeader("Content-type", "text/text");
        ex->setHeader("Content-encoding", "UTF-8");

        std::string routing_key("hola");

        int counter = 0;
        ISynchronizedQueue<protocol_t>* pQ=(ISynchronizedQueue<protocol_t>*)m_q;
        while(true)
        {
            boost::this_thread::sleep( boost::posix_time::milliseconds(50) );

            protocol_t  protocol;
            while (pQ->get(protocol)) {
                ex->Publish(protocol, routing_key);
                counter++;
                std::cout << protocol << std::endl;
                /*
                if (0 == counter % 1000) {
                    cout << protocol << endl;
                }
                */
            }
        }

    } catch (AMQPException e) {
        std::cout << e.getMessage() << std::endl;
        boost::this_thread::sleep( boost::posix_time::milliseconds(3000) );
        publish();
    }
}