1
我需要您对QUARTZ.NET提出的宝贵建议。我正在使用此计划在每10秒钟后运行我的功能。其实我的代码是从json文件获取数据并将其与数据库SQL服务器进行比较。如果id与数据库匹配,那么它将不会执行任何操作,否则它会发送产品。一些时间代码成功运行我没有重复的条目。但有时它会在数据库中插入重复值,并且只能跳过exists =(int)cmd.ExecuteScalar();请告诉我该怎么做。因为我不能从超过10秒的时间增加schedular的时间。以及如何停止在那个单线。以下是我的代码。这对我很有帮助..请帮助。感谢执行期间Quartz.NET跳过步骤
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();
}
三江源非常多设置一个注解。您解决了我问题...上帝保佑你 –
@ArslanAhmad高兴地帮助,你可以接受答案然后) – Set
嗨设置我想问一个问题。由于[DisallowConcurrentExecution],我的应用程序在30秒内完成所有工作,但是我在5秒钟间隔后设置了定时器。可以并行运行线程吗?意思是什么时候触发器会启动Sample job类,5秒后它会再次触发Sample作业,两者都会做他们的工作? –