Quartz.NET 在执行期间跳过步骤 [英] Quartz.NET skipping steps during execution

查看:57
本文介绍了Quartz.NET 在执行期间跳过步骤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要您对 QUARTZ.NET 的宝贵建议.我正在使用此计划在每 10 秒后运行我的函数.实际上我的代码是从 json 文件中获取数据并将其与数据库 SQL 服务器进行比较.如果 id 与数据库匹配,那么它不会做任何事情,否则它会发布产品.一些时间代码成功运行我没有重复的条目.但有时它会在数据库中插入重复值,并且只能跳过exist = (int)cmd.ExecuteScalar();请告诉我应该怎么做.因为我不能增加超过 10 秒的调度时间.以及如何停止错过其中的单行.下面是我的代码.这对我有很大帮助..请帮忙.谢谢

I need your valuable advice on QUARTZ.NET . I am using this scheduling to run my function after every 10 seconds. Actually my code is getting data from json files and compare it with database SQL server. If the id match the database then it will not do anything otherwise it will tweet the product. Some time code run successfully I got not duplicate entries. But sometime it insert the duplicate values in the database and it is only possible to skip the exist = (int)cmd.ExecuteScalar(); Please tell me what should do. Because i cannot increase the time of schedular from more than 10seconds. And how to stop missing the single line in that. Below is my code. It will be great help to me .. Please help. Thanks

IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
                scheduler.Start();
                // scheduler.ResumeAll();

                IJobDetail job = JobBuilder.Create<SampleJob>()
                               .WithIdentity("currencyJob", "group1")
                               .Build();

                //  IJobDetail job = JobBuilder.Create<SampleJob>().Build();
                ITrigger trigger = TriggerBuilder.Create()
                         .WithIdentity("trigger1", "group1")
                         .WithSimpleSchedule(s => s.WithIntervalInSeconds(15).RepeatForever())
                         .Build();

                //ITrigger trigger = TriggerBuilder.Create()
                //              .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).RepeatForever())
                //              .Build();

                scheduler.ScheduleJob(job, trigger);

和 Sample.cs 在下面

and Sample.cs is below

   public void Execute(IJobExecutionContext context)
        {


            tweetmonitor();
        }

        public void tweetmonitor()
        {
            ////////////////////////////////URLS And Proxy tables data get//////////////////////////////////
            List<UrlKeyword> employee = new List<UrlKeyword>();
            List<Proxy> proxy = new List<Proxy>();
            List<ProductsJsonModel> json = new List<ProductsJsonModel>();
            ProductsJsonModel json2 = new ProductsJsonModel();
            SqlConnection conn = new SqlConnection(strConnString);
            //SqlConnection conn = new SqlConnection(strConnString);

            SqlCommand customer = new SqlCommand("select * from customer", conn);
            SqlCommand cmdproxy = new SqlCommand("select * FROM ProxyTable", conn);

            customer.CommandType = CommandType.Text;
            try { 
            conn.Open();


            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            SqlDataReader rdr = customer.ExecuteReader();

            while (rdr.Read())
            {

                UrlKeyword emp = new UrlKeyword();

                emp.url = rdr["ContactName"].ToString();
                emp.keywords = rdr["CompanyName"].ToString();

                employee.Add(emp);
            }
            rdr.Close();


            SqlDataReader rdrproxy = cmdproxy.ExecuteReader();

            while (rdrproxy.Read())
            {
                Proxy pr = new Proxy();

                pr.IP = rdrproxy["IP"].ToString();
                pr.Port = Convert.ToInt32(rdrproxy["Port"]);
                pr.UserName = rdrproxy["UserName"].ToString();
                pr.PassWord = rdrproxy["PassWord"].ToString();
                proxy.Add(pr);

            }
            rdrproxy.Close();

            }
            catch (Exception ex)
            {

            }
            ////////////////////////////////////////////////////////////////
            ///////////////////Checking from database/////////////////////////

            ///////////////////Checking from database/////////////////////////


            foreach (var x in employee)
            {

                foreach (var m in proxy)
                {


                    try
                    {
                        string url = x.url;
                        string keyword = x.keywords;
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(x.url);
                        WebProxy myproxy = new WebProxy(m.IP, m.Port);
                        if (m.UserName != null && m.PassWord != null && m.UserName != "" && m.PassWord != "")

                        {
                            myproxy.Credentials = new NetworkCredential(m.UserName, m.PassWord);
                        }
                        myproxy.BypassProxyOnLocal = false;

                        request.Proxy = myproxy;
                        request.Method = "GET";
                        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                        if (response != null)
                        {
                            Stream receiveStream = response.GetResponseStream();
                            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
                            string mr = readStream.ReadToEnd();

                            /////////////////JSON Data///////////////////
                            if (x.url.Contains("json"))
                            {
                                //JSON files to twitter
                                try
                                {
                                    ProductsJsonModel Data = JsonConvert.DeserializeObject<ProductsJsonModel>(mr);
                                    List<Product> ProductsFromUrl = Data.products;
                                    int countofloop;
                                    countofloop = ProductsFromUrl.Count;

                                    SqlCommand cmd = new SqlCommand();
                                    SqlDataAdapter da = new SqlDataAdapter();
                                    SqlCommandBuilder cb = new SqlCommandBuilder(da);
                                    DataSet ds = new DataSet();

                                    if (conn.State != ConnectionState.Open)
                                    {
                                        conn.Open();
                                    }

                                    cmd.CommandText = "select IdJson from  dbo.JsonDataPrimary";
                                    cmd.Connection = conn;

                                    da.SelectCommand = cmd;

                                    da.Fill(ds, "JsonDataPrimary");

                                    for (int s = 0; s < countofloop; s++)
                                    {
                                        int exist = 0;
                                       // SqlCommand cmd = new SqlCommand();
                                        cmd.CommandText = @"SELECT count(*) FROM dbo.JsonDataPrimary WHERE IdJson= '" + ProductsFromUrl[s].id + "'";
                                        cmd.CommandType = System.Data.CommandType.Text;
                                        cmd.Connection = conn;
                                        exist = (int)cmd.ExecuteScalar();

                                        //////////////////////////duplicate check////////////////////////////////////


                                        //////////////////////////////////////////////////////

                                        //   if (exist > 0)
                                        if (checkDuplicateTitle(ds.Tables["JsonDataPrimary"].Rows, ProductsFromUrl[s].id.ToString()) && exist > 0)
                                        {
                                            ///exit from here
                                        }
                                        else
                                        {

                                            string logstweetout = published_at + Environment.NewLine + product_title + Environment.NewLine + newurlimage;

                                            string logsdata = published_at + Environment.NewLine + product_title + Environment.NewLine + newurlimage + Environment.NewLine + "Variants(According to size)" + Environment.NewLine + variantscheck + Environment.NewLine;
                                               cmd.CommandText = @"INSERT INTO dbo.JsonDataPrimary VALUES('" + ProductsFromUrl[s].id + "','" + ProductsFromUrl[s].published_at + "','','' ,'" + test + "')";
                                            cmd.CommandType = System.Data.CommandType.Text;
                                        if (conn.State != ConnectionState.Open)
                                        {
                                            conn.Open();
                                        }
                                        cmd.Connection = conn;



                                        }

                                        cmd.ExecuteNonQuery();

                                    }

                                }
                                catch (Exception ex)
                                {

                                }
                                conn.Close();
                            }

推荐答案

根据您的代码,可能有 2 个作业同时执行 => 从而产生重复.您可以将 [DisallowConcurrentExecution] 属性添加到您的作业类以防止:

Accordingly to your code it is possible that 2 jobs are executing in the same time => and so produce dublications. You may add [DisallowConcurrentExecution] attribute to your job class to prevent that:

将 Job 类标记为不能同时执行多个实例的注解

An annotation that marks a Job class as one that must not have multiple instances executed concurrently

这篇关于Quartz.NET 在执行期间跳过步骤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆