如果满足某些条件,如何停止工作?
下面是一个示例,该示例每5秒运行一次作业并生成1到100之间的随机数。现在我想让生成的数字为50时停止工作。
class Program
{
static void Main(string[] args)
{
JobScheduler jobScheduler = new JobScheduler();
jobScheduler.Start();
Console.ReadLine();
}
}
public class JobScheduler
{
public async void Start()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();
string id_job1 = Guid.NewGuid().ToString();
// define the job and tie it to our HelloJob class
IJobDetail job1 = JobBuilder.Create<HelloJob>()
.WithIdentity(Guid.NewGuid().ToString(), "group1")
.Build();
// Trigger the job to run now, and then every 5 seconds
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity(id_job1, "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
var listener = new CountJobListener();
scheduler.ListenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(job1.Key));
await scheduler.ScheduleJob(job1, trigger1);
}
}
public class HelloJob : IJob
{
SomethingToDo obj = new SomethingToDo();
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("HelloJob is executing.");
int num = obj.CreateRandomNumbers();
Console.WriteLine(num);
}
}
public class CountJobListener : IJobListener
{
public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
}
public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
}
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
{
}
public string Name { get { return "CountJobListener"; } }
}
public class SomethingToDo
{
public int CreateRandomNumbers()
{
return new Random().Next(1, 101);
}
}
如何实现?
更新
class Program
{
static void Main(string[] args)
{
JobScheduler jobScheduler = new JobScheduler();
jobScheduler.Start();
Console.ReadLine();
}
}
public class JobScheduler
{
public async void Start()
{
ISchedulerFactory schedulerFactory = new StdSchedulerFactory();
IScheduler scheduler = await schedulerFactory.GetScheduler();
await scheduler.Start();
string id_job1 = Guid.NewGuid().ToString();
// define the job and tie it to our HelloJob class
IJobDetail job1 = JobBuilder.Create<HelloJob>()
.WithIdentity(Guid.NewGuid().ToString(), "group1")
.Build();
// Trigger the job to run now, and then every 5 seconds
ITrigger trigger1 = TriggerBuilder.Create()
.WithIdentity(id_job1, "group1")
.StartNow()
.WithSimpleSchedule(x => x
.WithIntervalInSeconds(5)
.RepeatForever())
.Build();
var listener = new CountJobListener();
scheduler.ListenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(job1.Key));
await scheduler.ScheduleJob(job1, trigger1);
}
}
public class HelloJob : IJob
{
SomethingToDo obj = new SomethingToDo();
public async Task Execute(IJobExecutionContext context)
{
await Console.Out.WriteLineAsync("Job is executing.");
int num = obj.CreateRandomNumbers();
context.JobDetail.JobDataMap["GeneratedNumber"] = num; // Store some value in JobMap to be available for further processing if necessary
Console.WriteLine(num);
if (num == 5)
await context.Scheduler.PauseJob(context.JobDetail.Key);
}
}
public class CountJobListener : IJobListener
{
public async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
}
public async Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
}
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
{
int retVal = Convert.ToInt32(context.JobDetail.JobDataMap["GeneratedNumber"].ToString());
if (retVal == 50)
{
await Console.Out.WriteLineAsync("Succesfully completed!");
}
}
public string Name { get { return "CountJobListener"; } }
}
public class CountJobListener2 : IJobListener
{
public string Name => throw new NotImplementedException();
public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException, CancellationToken cancellationToken = default(CancellationToken))
{
throw new NotImplementedException();
}
}
public class SomethingToDo
{
public int CreateRandomNumbers()
{
return new Random().Next(1, 101);
}
}
如果有人有更好的主意,请提交。
答案 0 :(得分:1)
您可以像这样暂停作业
jobScheduler.PauseJob(new JobKey(<JobKey>, <Group>));