如果成功完成,请停止Quartz作业

问题描述 投票:0回答:1

如果满足某些条件,如何停止工作?

这是一个每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);
    }
}

怎么做到这一点?

UPDATE

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

如果有人有更好的想法,请提交。

c# jobs quartz.net job-scheduling
1个回答
1
投票

您可以暂停一个Job,如:

jobScheduler.PauseJob(new JobKey(<JobKey>, <Group>));
© www.soinside.com 2019 - 2024. All rights reserved.