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; }
void PostOffice::Activity() { { std::lock_guard<std::mutex> lock(pimpl_->activityFuturesMutex); for (auto i = pimpl_->activityFutures.begin(), end = pimpl_->activityFutures.end(); i != end; ) { if (i->wait_for(std::chrono::seconds(0)) == std::future_status::ready) { i = pimpl_->activityFutures.erase(i); end = pimpl_->activityFutures.end(); } else { ++i; } } } auto handle = std::async(std::launch::async, [this]() { try { AMQP amqp(connectString); AMQPExchange* exchange = amqp.createExchange(); DeclareExchange(exchange); exchange->Publish("", pimpl_->activityRoutingKey); } catch (std::exception& e) { std::lock_guard<std::mutex> lock(pimpl_->errorLogMutex); pimpl_->errorLog.SetError(e.what()); } }); std::lock_guard<std::mutex> lock(pimpl_->activityFuturesMutex); pimpl_->activityFutures.push_back(std::move(handle)); }
void PostOffice::Pimpl::RunSenderThread(const std::string& connectString) { bool error = false; while (!killed) { try { AMQP amqp(connectString); AMQPExchange* exchange = amqp.createExchange(); DeclareExchange(exchange); senderOk = true; if (error) { error = false; std::lock_guard<std::mutex> lock(errorLogMutex); errorLog.SetError("Sender now ok"); } auto nextStatusMessageTime = std::chrono::steady_clock::now(); double maxSecondsToWait = 0.0; while (!killed) { slaim::Message msg; if (sendBuffer.pop_front(msg, maxSecondsToWait)) { exchange->Publish(msg.m_text, msg.m_type); sendThroughput.AddThroughput(msg.GetSize()); } const auto now = std::chrono::steady_clock::now(); if (now >= nextStatusMessageTime) { slaim::Message statusMessage = GetStatusMessage(); exchange->Publish(statusMessage.m_text, statusMessage.m_type); nextStatusMessageTime += std::chrono::seconds(1); maxSecondsToWait = (std::max)(0.0, std::chrono::duration_cast<std::chrono::microseconds>(nextStatusMessageTime - now).count() * 1e-6); } } } catch (std::exception& e) { senderOk = false; if (!error) { error = true; std::lock_guard<std::mutex> lock(errorLogMutex); errorLog.SetError("Sender: " + std::string(e.what())); } std::this_thread::sleep_for(std::chrono::seconds(1)); } } }
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(); } }