现在的项目就是创建一个服务器,开启线程去队列读取日志数据,再返回来插入mysql 数据库,不过现在的每一条线程每次去队列读取数据总是一条一条读取,也就说一条线程去队列读取一条日志数据,再返回来插入数据库。每一次只读一条数据回来。创建了一个线程池,总共开了300多条线程。
接下来劳资登场了,面试实习生, 第一天的时候就跟我说,有一些西给你修改修改,因为觉得上面所说的效率低,每一次只能读取一条数据并返回来,线程数也太多了,现在希望 每条线程去队列读取日志数据的时候能一次读取100条,并返回来批量插入数据库。
从队列取回来的日志数据主要有两个类型:一种 create 数据, 一种是updata 数据。 要求在线程一次读取回来的100条数据中根据这两个类型分开,create 数据的就一次性 插入数据库,updata 数据的就一条一条插入,报错便回滚。把一些代码发上来,各位看看怎么改,急急急!!
服务代码如下:
static int Timespan = int.Parse(System.Configuration.ConfigurationManager.AppSettings["TimeSpan"]);
//一次执行读取多少日志
static int ExecNumber = int.Parse(System.Configuration.ConfigurationManager.AppSettings["ExecNumber"]);
//mysql服务器ip
static string DBHost = System.Configuration.ConfigurationManager.AppSettings["Server"];
//HTTPSQS日志队列获取地址
static string SqsHost = System.Configuration.ConfigurationManager.AppSettings["SQSURL"].Replace("http://", "").Split(':')[0];
System.Timers.Timer timer = new System.Timers.Timer(Timespan);
protected override void OnStart(string[] args)
{
Thread.Sleep(1000);
try
{
LogWrapper logWrapper = new LogWrapper();
//写入日志文件,本地
logWrapper.BuildLogFile("ServiceStart------------------------------", "Pause 1s", SqsHost, DBHost, "", "", "", "", "", "", "", "", "", "", "");
}
catch
{
}
// 通过委托条用 Commit 方法
timer.Elapsed += new System.Timers.ElapsedEventHandler(this.Commit);
timer.AutoReset = true;
timer.Enabled = true;
timer.Start();
}
protected override void OnStop()
{
timer.Enabled = false;
timer.AutoReset = false;
timer.Stop();
timer.Dispose();
}
protected void Commit(object sender, ElapsedEventArgs e)
{
try
{
if (DateTime.Now.Hour != 3)
{
// 线程池
ThreadPool.SetMaxThreads(350, 250);
using (Ping p = new Ping())
{
//服务器ip