rabbitmq技術的一些感悟(二)

上一節文章主要是說了一下rabbitmq的安裝以及搭建好環境的一些命令,以及常用的api調用,其實自從google被封掉之後,我之前收藏的很多技術連接都已經被禁止訪問了,這個是多麼可悲的一件事情啊,說多了都是淚。

     首先,我先寫一段消費者的模塊,建立連接,初始化amq以及銷燬連接:

[cpp] view plain copy
  1. Comsumer::Comsumer(){  
  2.   
  3. }  
  4. void Comsumer::init(){  
  5.     conn = amqp_new_connection();  
  6.     int sockfd = amqp_open_socket(g_logSrv.m_conf.m_hostname.c_str(), g_logSrv.m_conf.m_port);  
  7.     amqp_set_sockfd(conn,sockfd);  
  8.     LogServer::die_on_amqp_error(amqp_login(conn, g_logSrv.m_conf.m_vhosts.c_str(), 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,  g_logSrv.m_conf.m_username.c_str(),  g_logSrv.m_conf.m_psw.c_str()),"Logging in");  
  9.     amqp_channel_open(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel);  
  10.     LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn), "Opening channel");  
  11.     for(list<ramqserverInfo>::iterator it = g_logSrv.m_conf.getAmqserverInfo.begin(); it != g_logSrv.m_conf.getAmqserverInfo.end(); it++){  
  12.         amqp_exchange_declare(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->exchange.c_str()), amqp_cstring_bytes("direct"), 0, 0, amqp_empty_table);  
  13.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring exchange");  
  14.         amqp_queue_declare(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->queue.c_str()), 0, 1, 0, 1, amqp_empty_table); // durable && auto-delete   
  15.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn), "Declaring queue");  
  16.           
  17.         amqp_queue_bind(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->queue.c_str()), amqp_cstring_bytes(it->exchange.c_str()), amqp_cstring_bytes(it->route.c_str()), amqp_empty_table);    
  18.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn), "Binding");   
  19.   
  20.         amqp_basic_consume(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->queue.c_str()), amqp_empty_bytes, 0, 1, 0, amqp_empty_table);  
  21.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn), "Consuming");  
  22.         connList.push_back(*it);  
  23.     }  
  24. }  
  25. Comsumer::~Comsumer(){  
  26.     if(conn == NULL){  
  27.         return;  
  28.     }  
  29.     LogServer::die_on_amqp_error(amqp_channel_close(conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, AMQP_REPLY_SUCCESS), "Closing channel");  
  30.     LogServer::die_on_amqp_error(amqp_connection_close(conn, AMQP_REPLY_SUCCESS), "Closing connection");      
  31.     LogServer::die_on_error(amqp_destroy_connection(conn), "Ending connection");  
  32. }  


 然後再寫一段消費者的隊列,他也是可持久化的隊列:
[cpp] view plain copy
  1. Producer::Producer(){  
  2. }  
  3. void Producer::init(){  
  4.     conn.conn = amqp_new_connection();  
  5.     int sockfd = amqp_open_socket(g_logSrv.m_conf.m_hostname.c_str(), g_logSrv.m_conf.m_port);  
  6.     amqp_set_sockfd(conn.conn,sockfd);  
  7.     LogServer::die_on_amqp_error(amqp_login(conn.conn, g_logSrv.m_conf.m_vhosts.c_str(), 0, 131072, 0, AMQP_SASL_METHOD_PLAIN,  g_logSrv.m_conf.m_username.c_str(),  g_logSrv.m_conf.m_psw.c_str()),"Logging in");  
  8.     amqp_channel_open(conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel);  
  9.     LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn.conn), "Opening channel");  
  10.     for(list<ramqserverInfo>::iterator it = g_logSrv.m_conf.commonAmqserver.begin(); it != g_logSrv.m_conf.commonAmqserver.end(); it++){  
  11.         amqp_exchange_declare(conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->exchange.c_str()), amqp_cstring_bytes("direct"), 0, 0, amqp_empty_table);  
  12.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn.conn), "Declaring exchange");  
  13.         amqp_queue_declare(conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->queue.c_str()), 0, 1, 0, 1, amqp_empty_table); // durable && auto-delete   
  14.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn.conn), "Declaring queue");  
  15.         amqp_queue_bind(conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(it->queue.c_str()), amqp_cstring_bytes(it->exchange.c_str()), amqp_cstring_bytes(it->route.c_str()), amqp_empty_table);    
  16.         LogServer::die_on_amqp_error(amqp_get_rpc_reply(conn.conn), "Binding");       
  17.         connList.push_back(*it);  
  18.     }  
  19.     conn.props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;  
  20.     conn.props.content_type = amqp_cstring_bytes("text/plain");  
  21.     conn.props.delivery_mode = 2;  
  22. }  
  23. Producer::~Producer(){  
  24.     if(conn.conn == NULL){  
  25.         return;  
  26.     }  
  27.     LogServer::die_on_amqp_error(amqp_channel_close(conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, AMQP_REPLY_SUCCESS), "Closing channel");  
  28.     LogServer::die_on_amqp_error(amqp_connection_close(conn.conn, AMQP_REPLY_SUCCESS), "Closing connection");  
  29.     LogServer::die_on_error(amqp_destroy_connection(conn.conn), "Ending connection");  
  30. }  
最後就是如何向rabbitmq中寫消息和取消息了:

[cpp] view plain copy
  1. void LogServer::WriteToAmqQueue(bool isQuick, string str){  
  2.     if(isQuick){  
  3.         uint8_t size = m_produce.connQuickList.size();  
  4.         uint8_t index = rand()%size;  
  5.         LogServer::die_on_error(amqp_basic_publish(m_produce.connQuick.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(m_produce.connQuickList[index].exchange.c_str()), amqp_cstring_bytes(m_produce.connQuickList[index].route.c_str()), 0, 0, &m_produce.connQuick.props, amqp_cstring_bytes(str.c_str())), "Publishing");  
  6.     }else{  
  7.         uint8_t size = m_produce.connList.size();  
  8.         uint8_t index = rand()%size;  
  9.         LogServer::die_on_error(amqp_basic_publish(m_produce.conn.conn, (amqp_channel_t)g_logSrv.m_conf.m_channel, amqp_cstring_bytes(m_produce.connList[index].exchange.c_str()), amqp_cstring_bytes(m_produce.connList[index].route.c_str()), 0, 0, &m_produce.conn.props, amqp_cstring_bytes(str.c_str())), "Publishing");  
  10.     }     
  11. }  
  12. void LogServer::runAmqConsumer(){  
  13.     m_produce.init();  
  14.     m_consumer.init();  
  15.     uint32_t tickTime = time(0);  
  16.     while(1){  
  17.         amqp_rpc_reply_t ret;  
  18.         amqp_envelope_t envelope;  
  19.         memset(&envelope,0,sizeof(envelope));  
  20.         envelope.message.body.bytes={0};  
  21.         amqp_maybe_release_buffers(m_consumer.conn);  
  22.         ret = amqp_consume_message(m_consumer.conn, &envelope, NULL, 0);  
  23.   
  24.         if (ret.reply_type == AMQP_RESPONSE_NORMAL ){  
  25.             char* str = (char*)envelope.message.body.bytes;  
  26.             if(str == NULL){  
  27.                 return;  
  28.             }  
  29.             str[envelope.message.body.len-1] = '\0';  
  30.             LOG(DEBUG)("recv from amq:len:%d,%s\n",envelope.message.body.len,str);  
  31.             vector<string> vectStr;  
  32.             Util::strSplit(str, "|", vectStr);            
  33.             if(vectStr.size() > 8 && (g_logSrv.m_conf.quickWords.find(vectStr[8]) != g_logSrv.m_conf.quickWords.end())){  
  34.                 WriteToAmqQueue(true,str);  
  35.             }  
  36.             else  
  37.             {  
  38.                 WriteToAmqQueue(false,str);  
  39.             }  
  40.             amqp_destroy_envelope(&envelope);  
  41.         //  pushMsgQueue(str);  
  42.             str = NULL;  
  43.         }  
  44.         else{  
  45.             amqp_destroy_envelope(&envelope);  
  46.             return;  
  47.         }  
  48.   
  49.         if(tickTime + FAMETICK < time(0) && m_conf.loadConfig() != 0){  
  50.             tickTime = time(0);  
  51.             if(LogServer::m_stop == true){  
  52.                 m_produce.init();  
  53.                 m_consumer.init();  
  54.                 stop();  
  55.             }  
  56.             continue;  
  57.         }  
  58.     }  
  59. }  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章