public static void Main() { ILog log = log4net.LogManager.GetLogger(typeof(Program)); var rabbitMQServer = RabbitMQManager.GetRabbitMQServer("rabbitMQ177"); var worker = new Worker("ChangePriceQueue.LogToRedis", false, rabbitMQServer); Task workerTask = new Task(() => { 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); RedisPriceLogAdapter.PushMerchantProductPrice(job.ProductID, job.NewPrice, DateTime.Now); log.Info(string.Format("Saved for job:{0}", strData)); return(true); } catch (Exception ex01) { log.Error("Exception:", ex01); return(true); } }; worker.Start(); }); workerTask.Start(); Console.ReadLine(); }
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(); }
protected override void OnStart(string[] args) { log.Info("Start service"); try { InitializeComponent(); cancelTokenSource = new CancellationTokenSource(); string rabbitMQServerName = ConfigurationManager.AppSettings["rabbitMQServerName"]; workers = new Worker[workerCount]; rabbitMQServer = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName); Encoding enc = new UTF8Encoding(true, true); for (int i = 0; i < workerCount; i++) { log.InfoFormat("Start worker {i}", i.ToString()); var worker = new Worker(ChangePriceToRedisJobName, false, rabbitMQServer); workers[i] = worker; var token = this.cancelTokenSource.Token; Task workerTask = new Task(() => { worker.JobHandler = (downloadImageJob) => { try { token.ThrowIfCancellationRequested(); JobRabbitChangePrice job = JsonConvert.DeserializeObject <JobRabbitChangePrice>(enc.GetString(downloadImageJob.Data)); WebRequest client = WebRequest.Create(string.Format(@"http://172.22.1.108:8983/api/PriceDetector/price_detect.htm?pn={0}", Uri.EscapeDataString(job.Name))); string strData = (new StreamReader(client.GetResponse().GetResponseStream()).ReadToEnd()); MsCheckPrice msCheckPrice = WSS.Service.CheckPriceProduct.MsCheckPrice.FromJSON(strData); bool failPrice = false; if (job.NewPrice > 0) { if (msCheckPrice.price > 100000) { if (job.NewPrice > msCheckPrice.price * (decimal)2 || job.NewPrice < msCheckPrice.price / (decimal)2) { failPrice = true; } log.Info(string.Format("CompanyID: {0} ProductID: {1} OldPrice: {2} OldPrice: {3} FailProduct: {4} SuggestPrice: {5}", job.CompanyID, job.ProductID, job.OldPrice, job.NewPrice, failPrice, msCheckPrice.price)); } } return(true); } catch (OperationCanceledException opc) { log.Info("End worker"); return(false); } }; worker.Start(); }, token); workerTask.Start(); log.InfoFormat("Worker {0} started", i); } } catch (Exception ex) { log.Error("Start error", ex); throw; } }
private string GetCommandSql(JobRabbitChangePrice job) { if (job.OldPrice > 0) { return(string.Format("Update AdvProductHotdeal Set Price = {0}, PriceOld = {1} Where ProductId = {2}", job.NewPrice, job.OldPrice, job.ProductID)); } else { return(string.Format("Update AdvProductHotdeal Set Price = {0} Where ProductId = {1}", job.NewPrice, job.ProductID)); } }
public void PushQueueChangePriceLog(JobRabbitChangePrice job) { while (true) { try { this.PublishJob(new Job { Type = 0, Data = job.ToArrayByte() }); return; } catch (Exception ex01) { _log.Error("Ex when push change price log:", ex01); Thread.Sleep(10000); } } }