我需要您对QUARTZ.NET的宝贵建议。我每隔10秒就使用这个调度来运行我的函数。实际上我的代码是从json文件获取数据并将其与数据库SQL服务器进行比较。如果id与数据库匹配,那么它将不会执行任何操作,否则它将发布产品。一些时间代码成功运行我没有重复的条目。但有时它会在数据库中插入重复的值,并且只能跳过exists =(int)cmd.ExecuteScalar();请告诉我应该怎么做。因为我无法从10秒以上增加调度时间。以及如何在此停止错过单行。以下是我的代码。这对我很有帮助..请帮忙。谢谢
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位于
之下 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();
}
答案 0 :(得分:0)
根据您的代码,可能有2个作业同时执行=&gt;因此产生出版物。您可以将[DisallowConcurrentExecution]
属性添加到作业类中以防止:
将Job类标记为不能同时执行多个实例的注释