protected override void OnStart(string[] args) { log.Info("Start service"); try { int numberQUeue = this.jobQueues.Count(); InitializeComponent(); cancelTokenSource = new CancellationTokenSource(); Server.LogConnectionString = ConfigurationManager.AppSettings["LogConnectionString"]; QT.Entities.Server.ConnectionString = connectionString; string rabbitMQServerName = ConfigurationManager.AppSettings["rabbitMQServerName"]; workers = new Worker[numberQUeue]; rabbitMQServer = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName); for (int i = 0; i < numberQUeue; i++) { int indexQueue = i; log.InfoFormat("Start worker {i}", i.ToString()); var worker = new Worker(this.jobQueues[indexQueue], false, rabbitMQServer); workers[i] = worker; var token = this.cancelTokenSource.Token; Task workerTask = new Task(() => { string queueName = this.jobQueues[indexQueue]; QT.Moduls.Notifycation.NotifycationAdapter adapter = new QT.Moduls.Notifycation.NotifycationAdapter(); worker.JobHandler = (updateDatafeedJob) => { try { string strMessage = System.Text.Encoding.UTF8.GetString(updateDatafeedJob.Data, 0, updateDatafeedJob.Data.Length); adapter.InsertMessage(queueName, strMessage, 0); log.Info(string.Format("MSS:{0}. Queue:{1}", strMessage, queueName)); return(true); } catch (OperationCanceledException opc) { log.Info("End worker"); return(true); } catch (Exception ex01) { log.Error(ex01); return(true); } }; worker.Start(); }, token); workerTask.Start(); log.InfoFormat("Worker {0} started", i); } } catch (Exception ex) { log.Error("Start error", ex); throw; } }
public void Run() { cancellationTokenSource = new CancellationTokenSource(); List <string> lstQueue = new List <string>(); lstQueue.Add("DataQueue.AllEmployee"); var _rabbitMQServer = RabbitMQManager.GetRabbitMQServer("rabbitMQ_Notification"); for (int i = 0; i < lstQueue.Count; i++) { string queueName = lstQueue[i]; Task.Factory.StartNew(() => { string queueNameData = queueName; QT.Moduls.Notifycation.NotifycationAdapter adapter = new QT.Moduls.Notifycation.NotifycationAdapter(); log.Info("Start a consumer"); Websosanh.NotificationSystem.Common.NotificationConsumer basicConsumer = new NotificationConsumer(_rabbitMQServer, queueName, true, "123456"); while (true) { try { BasicGetResult result = basicConsumer.GetMessage(); if (result != null && result.Body != null) { string strMessage = System.Text.Encoding.UTF8.GetString(result.Body, 0, result.Body.Length); adapter.InsertMessage(queueNameData, strMessage, 0); log.Info(string.Format("MSS:{0}. Queue:{1}", strMessage, queueNameData)); } else { log.Info("Not item"); } Thread.Sleep(1000); } catch (OperationCanceledException operationCanceled) { return; } catch (Exception ex01) { log.Error(ex01); Thread.Sleep(10000); } } }); } }