Testing Quartz CronTrigger trigger

前端 未结 6 1853
北荒
北荒 2020-12-30 06:04

Assuming that I have a CronTriggerBean similar to



        
6条回答
  •  难免孤独
    2020-12-30 06:24

    I found a cool documentation here about testing the CronExpression: http://www.nurkiewicz.com/2012/10/testing-quartz-cron-expressions.html

    The C# implementation will be something like this:

    void Run()
    {
        //var collection = findTriggerTimesRecursive(new CronExpression("0 0 17 L-3W 6-9 ? *"), DateTime.UtcNow);
        var collection = findTriggerTimesRecursive(new CronExpression("0 0/15 * 1/1 * ? *"), DateTime.UtcNow);
        Console.WriteLine(DateTime.UtcNow);
        foreach (var item in collection)
        {
            Console.WriteLine(item);
        }
    }
    
    public List findTriggerTimesRecursive(CronExpression expr, DateTimeOffset from, int max = 10)
    {
        var times = new List();
        var next = expr.GetNextValidTimeAfter(from);
    
        while (next != null && times.Count < max)
        {
            times.Add(next.Value);
            from = next.Value;
            next = expr.GetNextValidTimeAfter(from);
        }
    
        return times;
    }
    

    This is a cool demo. But at the end, I end using Simple Schedule.

    var trigger = TriggerBuilder.Create()
        .WithIdentity("trigger3", "group1")
        .WithSimpleSchedule(
            x =>
            {
                x.WithIntervalInMinutes(15);
                x.RepeatForever();
            }
        )
        .ForJob("myJob", "group1")
        .Build();
    

    Because this is executed immediately and then every x time.

提交回复
热议问题