#include "amqp.h"
#include "amqp_tcp_socket.h"
#include "amqp_framing.h"
amqp_connection_state_t m_conn;
amqp_socket_t *m_socket;
amqp_basic_properties_t m_props;
一:初始化
CVideoLabelsConn::CVideoLabelsConn(void):
m_bConnSucc(false)
{
m_props._flags = AMQP_BASIC_CONTENT_TYPE_FLAG | AMQP_BASIC_DELIVERY_MODE_FLAG;
m_props.content_type = amqp_cstring_bytes("text/plain");
m_props.delivery_mode = DELIVER_MODE;
}
二:连接rabbitmq
HPR_INT32 CVideoLabelsConn::Connnect()
{
HPR_Guard cGuard(&m_cLock);
if(m_bConnSucc)
{
return HPR_OK;
}
HPR_INT32 iRet = HPR_OK;
do
{
m_conn = amqp_new_connection();
m_socket = amqp_tcp_socket_new(m_conn);
if(!m_socket)
{
PLUGIN_LOG_ERROR("rabbitmq connect amqp_tcp_socket_new fail");
iRet = HPR_ERROR;
break;
}
std::string strHostName = CRabbitMqConfig::instance()->GetIp();
int iPort = CRabbitMqConfig::instance()->GetPort();
int status = amqp_socket_open(m_socket, strHostName.c_str(), iPort);
if(AMQP_STATUS_OK != status)
{
PLUGIN_LOG_ERROR("rabbitmq connect amqp_socket_open fail");
iRet = HPR_ERROR;
break;
}
std::string strPwd = CRabbitMqConfig::instance()->GetPwd();
std::string strUserName = CRabbitMqConfig::instance()->GetUserName();
std::string strVhost = CRabbitMqConfig::instance()->GetVhost();
if(HPR_OK != die_on_amqp_error(amqp_login(m_conn, strVhost.c_str(), 0, FRAME_MAX, 0, AMQP_SASL_METHOD_PLAIN,
strPwd.c_str(), strUserName.c_str()),
"Logging in"))
{
iRet = HPR_ERROR;
break;
}
//打开通道
amqp_channel_open(m_conn, MQ_CHANNEL_ID);
if(HPR_OK !=die_on_amqp_error(amqp_get_rpc_reply(m_conn), "Opening channel"))
{
iRet = HPR_ERROR;
break;
}
//声明交换机
/*std::string strExchange = CRabbitMqConfig::instance()->GetExchange();
amqp_exchange_declare(m_conn, MQ_CHANNEL_ID, amqp_cstring_bytes(strExchange.c_str()),amqp_cstring_bytes(MQ_EXCHANGE_TYPE),
0, 1, amqp_empty_table);
if(HPR_OK != die_on_amqp_error(amqp_get_rpc_reply(m_conn), "Declaring exchange"))
{
break;
}*/
iRet = HPR_OK;
m_bConnSucc = true;
} while (false);
return iRet;
}
三:发送数据
HPR_INT32 CVideoLabelsConn::Send( HPR_INT32 type, void *pData, int iLen, int nMsgCount )
{
HPR_Guard cGuard(&m_cLock);
if ((!m_bConnSucc)||NULL == pData || sizeof(TDA_PLATFORM_VACALARM_INFO) != iLen)
{
PLUGIN_LOG_ERROR("rabbit mq send failed");
return HPR_ERROR;
}
TDA_PLATFORM_VACALARM_INFO* pVideoLabelsData = (TDA_PLATFORM_VACALARM_INFO*)pData;
std::string strMsg = pVideoLabelsData->stVacAlarmInfo.szAlarmJsonInfo;
//rabbitmq amqp协议
std::string strExchange = CRabbitMqConfig::instance()->GetExchange();
std::string strRoutingKey = CRabbitMqConfig::instance()->GetRoutingKey();
die_on_error(amqp_basic_publish(m_conn, 1, amqp_cstring_bytes(strExchange.c_str()),
amqp_cstring_bytes(strRoutingKey.c_str()), 0, 0,
&m_props, amqp_cstring_bytes(strMsg.c_str())),
"Publishing");
return HPR_OK;
}
四:释放资源
HPR_INT32 CVideoLabelsConn::Close()
{
HPR_Guard cGuard(&m_cLock);
die_on_amqp_error(amqp_channel_close(m_conn, 1, AMQP_REPLY_SUCCESS),
"Closing channel");
die_on_amqp_error(amqp_connection_close(m_conn, AMQP_REPLY_SUCCESS),
"Closing connection");
die_on_error(amqp_destroy_connection(m_conn), "Ending connection");
PLUGIN_LOG_INFO("rabbitmq close succ");
return HPR_OK;
}
2146



被折叠的 条评论
为什么被折叠?



