int main () { try { // AMQP amqp("123123:akalend@localhost/private"); AMQP amqp("123123:akalend@localhost:5673/private"); AMQPQueue * qu2 = amqp.createQueue("q2"); qu2->Declare(); qu2->Bind( "e", ""); qu2->setConsumerTag("tag_123"); qu2->addEvent(AMQP_MESSAGE, onMessage ); qu2->addEvent(AMQP_CANCEL, onCancel ); qu2->Consume(AMQP_NOACK);// } catch (AMQPException e) { std::cout << e.getMessage() << std::endl; } return 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(); } }
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; }
int main () { try { // AMQP amqp("123123:akalend@localhost/private"); AMQP amqp("123123:akalend@localhost:5673/private"); AMQPQueue * qu2 = amqp.createQueue("q2"); qu2->Declare(); while ( 1 ) { qu2->Get(AMQP_NOACK); AMQPMessage * m= qu2->getMessage(); cout << "count: "<< m->getMessageCount() << endl; if (m->getMessageCount() > -1) { uint32_t j = 0; cout << "message\n"<< m->getMessage(&j) << "\nmessage key: "<< m->getRoutingKey() << endl; cout << "exchange: "<< m->getExchange() << endl; cout << "Content-type: "<< m->getHeader("Content-type") << endl; cout << "Content-encoding: "<< m->getHeader("Content-encoding") << endl; } else break; } } catch (AMQPException e) { std::cout << e.getMessage() << std::endl; } return 0; }
int main(int argc, char *argv[]) try { log4cplus::PropertyConfigurator::doConfigure("log4cplus.properties"); std::string host = "localhost"; int port = AMQP_PROTOCOL_PORT; std::string exchange = ""; size_t count = 1; size_t wait = 0; struct option long_opt[] = { {"host", required_argument, nullptr, 'h'}, {"port", required_argument, nullptr, 'p'}, {"exchange", required_argument, nullptr, 'e'}, {"count", required_argument, nullptr, 'c'}, {"wait", required_argument, nullptr, 'w'}, {nullptr, no_argument, nullptr, 0} }; while(true) { int c = getopt_long(argc, argv, "h:p:e:c:w:", long_opt, nullptr); if (c == -1) break; switch(c) { case 'h': host = optarg; break; case 'p': port = atoi(optarg); break; case 'e': exchange = optarg; break; case 'c': count = atol(optarg); break; case 'w': wait = atol(optarg); break; default: usage(); return EXIT_FAILURE; } } if (argc < optind + 2) { usage(); return EXIT_FAILURE; } const std::string routing_key(argv[optind]); const std::string message(argv[optind + 1]); std::ostringstream connection_str; connection_str << "guest:guest@" << host << ":" << port << "/"; AMQP amqp(connection_str.str()); AMQPExchange * ex = amqp.createExchange(exchange); for(size_t i = 0; i < count; i++) { std::ostringstream oss; oss << i + 1 << " - " << message; ex -> setHeader("Content-type", "text/plain"); ex -> setHeader("Content-encoding", "UTF-8"); ex -> setHeader("Delivery-mode", 2); ex -> Publish(oss.str(), routing_key); LOG4CPLUS_INFO_FMT(logger, "Send: %s", oss.str().c_str()); if (wait) { std::this_thread::sleep_for(std::chrono::milliseconds(wait)); } } return EXIT_SUCCESS; } catch (const std::exception& e) { LOG4CPLUS_ERROR(logger, e.what()); return EXIT_FAILURE; } catch(...) { LOG4CPLUS_ERROR(logger, "Unknown exceprion"); return EXIT_FAILURE; }