コード例 #1
0
ファイル: example_publish.cpp プロジェクト: igoral5/amqpcpp
int main (int argc, char** argv) {



	try {
//		AMQP amqp;
//		AMQP amqp(AMQPDEBUG);
	
		AMQP amqp("guest:[email protected]:5672/");		// all connect string

		AMQPExchange * ex = amqp.createExchange("e");
		ex->Declare("e", "fanout");

		AMQPQueue * qu2 = amqp.createQueue("q2");
		qu2->Declare();
		qu2->Bind( "e", "");		

		std::string ss = "message 1 ";
		/* test very long message
		ss = ss+ss+ss+ss+ss+ss+ss;
		ss += ss+ss+ss+ss+ss+ss+ss;
		ss += ss+ss+ss+ss+ss+ss+ss;
		ss += ss+ss+ss+ss+ss+ss+ss;
		ss += ss+ss+ss+ss+ss+ss+ss;
*/

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

		ex->Publish(  ss , ""); // publish very long message
		
		ex->Publish(  "message 2 " , "");
		ex->Publish(  "message 3 " , "");
		
		
		if (argc==2) {
			AMQPQueue * qu = amqp.createQueue();			
			qu->Cancel(   amqp_cstring_bytes(argv[1]) );
		}												
						
	} catch (AMQPException e) {
		std::cout << e.getMessage() << std::endl;
	}

	return 0;

}
コード例 #2
0
ファイル: producer.cpp プロジェクト: FlavioFalcao/amqpcpp
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();
    }
}