Quartz.NET skipping steps during execution

徘徊边缘 提交于 2020-01-05 03:48: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);

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();
                            }

回答1:


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:

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



来源:https://stackoverflow.com/questions/42055006/quartz-net-skipping-steps-during-execution

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!