Exemple #1
0
        protected override void OnStart(string[] args)
        {
            InitializeComponent();
            log.Info("Start service");
            Article art = new Article();

            try
            {
                string rabbitMQServerName = ConfigurationManager.AppSettings["rabbitMQServerName"];
                workers                 = new Worker[workerCount];
                rabbitMQServer          = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName);
                articleConnectionString = ConfigurationManager.AppSettings["ConnectionString"];
                for (int i = 0; i < workerCount; i++)
                {
                    worker     = new Worker(jobNameUpdateArticle, false, rabbitMQServer);
                    workers[i] = worker;
                    Task workerTask = new Task(() =>
                    {
                        worker.JobHandler = (Job) =>
                        {
                            try
                            {
                                long articleID = BitConverter.ToInt64(Job.Data, 0);
                                art            = ArticleRespository.GetArticleFromDb(articleID, articleConnectionString);
                                if (art != null)
                                {
                                    ArticleRespository.InsertArticleIntoCache(art);
                                    ArticleShortInfoRespository.InsertArticleShortInfoIntoCache(art);
                                    log.InfoFormat("insert complete:{0}", articleID);
                                    return(true);
                                }
                                else
                                {
                                    log.InfoFormat("{0}: null", articleID);
                                    return(true);
                                }
                            }
                            catch (Exception ex01)
                            {
                                log.Error(ex01);
                                return(true);
                            }
                        };
                        worker.Start();
                    });
                    workerTask.Start();
                }
            }
            catch (Exception ex)
            {
                log.Error("Start error", ex);
                throw;
            }
        }
Exemple #2
0
        public void Run()
        {
            Article art = new Article();

            try
            {
                jobNameUpdateArticle = "Article.Update";
                cancelTokenSource    = new CancellationTokenSource();
                string rabbitMQServerName = "rabbitMQ177";
                workers        = new Worker[workerCount];
                rabbitMQServer = RabbitMQManager.GetRabbitMQServer(rabbitMQServerName);
                for (int i = 0; i < workerCount; i++)
                {
                    var worker = new Worker(jobNameUpdateArticle, false, rabbitMQServer);
                    workers[i] = worker;
                    var  token      = this.cancelTokenSource.Token;
                    Task workerTask = new Task(() =>
                    {
                        worker.JobHandler = (Job) =>
                        {
                            try
                            {
                                token.ThrowIfCancellationRequested();
                                long articleID = BitConverter.ToInt64(Job.Data, 0);
                                art            = ArticleRespository.GetArticleFromDb(articleID, "server=172.22.1.82;uid=wss_news;pwd=HzlRt4$$axzG-*UlpuL2gYDu;database=ReviewsCMS");
                                ArticleRespository.InsertArticleIntoCache(art);
                                ArticleShortInfoRespository.InsertArticleShortInfoIntoCache(art);

                                return(true);
                            }
                            catch (Exception ex01)
                            {
                                return(true);
                            }
                        };
                        worker.Start();
                    }, token);
                    workerTask.Start();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                throw;
            }
        }
Exemple #3
0
 private IArticleRespository GetRespository()
 {
     return(ArticleRespository.GetInstance());
 }