第七个工具类:DBServicePool
是针对前文提到的DBService的一个管理器
用于多线程环境下,可能有需求需要创建多个DBService,使得每个线程有机会可以独享一个DBService
所以有了这样一个DBService的管理器
提供Start,Stop,用来运行/停止所有DBService对象
提供Ping,用来维持所有DBService对象的心跳
提供一个虚接口Service,子类可以实现管理自己的继承自DBService的子类对象
比如:
实现一个MyDBService : public DBService
实现一个MyDBServicePool : public DBServicePool
DBServicePool的Service接口,实现的是创建一个DBService对象,返回其shared_ptr
MyDBServicePool 的Service接口,可以自行实现创建一个MyDBService对象,返回其shared_ptr
2017.3.10修改:
改为模板实现
模板类型T,T要求为DBService或其子类
Start并入构造函数,构造时创建指定个数的T对象,可以指定为0个,即不创建任何DBService或其子类对象
虚接口Service删除,不再需要,创建时,直接new T(...)
GetDBService:获取一个DBService对象,当Pool中没有DBService对象时(构造时指定了个数为0),抛一个异常
修改后代码:
DBServicePool.h
#ifndef __DBServicePool_H__
#define __DBServicePool_H__
#include <boost/noncopyable.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include "DBService.h"
namespace common{
namespace db{
template <class T>
class DBServicePool : private boost::noncopyable
{
public:
DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname);
virtual ~DBServicePool();
// 停止所有DBService对象
void Stop();
DBService& GetDBService();
// 获取IOService对象个数
std::size_t Size();
// 维持链接心跳
void Ping();
private:
boost::mutex m_Lock;
// DBService列表
std::vector<boost::shared_ptr<DBService> > m_DBServices;
// 顺序使用的下一个DBService下标
unsigned int m_Next;
};
template <class T>
DBServicePool<T>::DBServicePool(unsigned int size,
const std::string& hostname,
unsigned int port,
const std::string& username,
const std::string& userkey,
const std::string& dbname)
{
for (unsigned int i = 0; i < size; i++)
{
boost::shared_ptr<DBService> dbService(new T());
if (NULL != dbService)
{
dbService->Start(hostname, port, username, userkey, dbname);
m_DBServices.push_back(dbService);
}
}
m_Next = 0;
}
template <class T>
DBServicePool<T>::~DBServicePool()
{
}
template <class T>
void DBServicePool<T>::Stop()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Stop();
}
}
template <class T>
DBService& DBServicePool<T>::GetDBService()
{
boost::mutex::scoped_lock lock(m_Lock);
if (0 < m_DBServices.size())
{
m_Next++;
if (m_Next >= m_DBServices.size())
{
m_Next = 0;
}
return *m_DBServices[m_Next];
}
else
{
throw std::string("DBServicePool size is 0");
}
}
template <class T>
std::size_t DBServicePool<T>::Size()
{
boost::mutex::scoped_lock lock(m_Lock);
return m_DBServices.size();
}
template <class T>
void DBServicePool<T>::Ping()
{
boost::mutex::scoped_lock lock(m_Lock);
for (std::size_t i = 0; i < m_DBServices.size(); ++i)
{
m_DBServices[i]->Ping();
}
}
}
}
#endif