public static void Main() { ILog log = log4net.LogManager.GetLogger(typeof(Program)); var rabbitMQServer = RabbitMQManager.GetRabbitMQServer("rabbitMQ177"); var worker = new Worker("ChangePriceQueue.LogToSql", false, rabbitMQServer); Task workerTask = new Task(() => { string connectToSQL = @"Data Source=172.22.30.86,1455;Initial Catalog=QT_2;Persist Security Info=True;User ID=qt_vn;Password=@F4sJ=l9/ryJt9MT;connection timeout=200"; CrawlerProductAdapter crawlerProductAdapter = new CrawlerProductAdapter(new SqlDb(connectToSQL)); log.Info("Start consumer!"); worker.JobHandler = (downloadImageJob) => { log.Info("Get job from MQ"); try { Encoding enc = new UTF8Encoding(true, true); string strData = enc.GetString(downloadImageJob.Data); JobRabbitChangePrice job = JsonConvert.DeserializeObject <JobRabbitChangePrice>(strData); crawlerProductAdapter.SaveLog(job.ProductID, job.NewPrice, job.OldPrice); log.Info(string.Format("Log for {0}", strData)); return(true); } catch (Exception ex01) { log.Error("Exception:", ex01); return(true); } }; worker.Start(); }); workerTask.Start(); Console.ReadLine(); }
private void Init() { base.JobHandler = (downloadImageJob) => { try { if (_token.IsCancellationRequested) { return(false); } var job = JsonConvert.DeserializeObject <JobRabbitChangePrice>(_enc.GetString(downloadImageJob.Data)); _crawlerProductAdapter.SaveLog(job.ProductID, job.NewPrice, job.OldPrice); _log.Info(string.Format("Saved job: {0}", _enc.GetString(downloadImageJob.Data))); return(true); } catch (Exception ex) { _log.Error(ex); Thread.Sleep(1000); return(false); } }; }