如何处理Quartz.net作业执行事件?

我创建了一个通用的工作计划管理器类。我正在.net核心应用程序中使用Quartz.net 3.0.7。

using UnityEngine;
using UnityEngine.EventSystems;

public class JumpButton : MonoBehaviour {

private bool shouldJump = false;



// Update is called once per frame
void Update () {
    //Find the player
    var player = GameObject.FindGameObjectWithTag("CharacterController");
    //No player? exit out.
    if (player == null)
        return;
    //Is the jump button currently being pressed?
    if (shouldJump)
    {
        //Translate it upwards with time.
        player.transform.Translate(new Vector3(0,Time.deltaTime * 5,0));
        //Make sure the Rigidbody is kinematic,or gravity will pull us down again
        if (player.getcomponent<Rigidbody>().isKinematic == false)
            player.getcomponent<Rigidbody>().isKinematic = true;
    }
    //Not jumping anymore? reset the Rigidbody.
    else
        player.getcomponent<Rigidbody>().isKinematic = false;
}

//When the button is being pressed down,this function is called.
public void ButtonpressedDown(BaseEventData e)
{
    shouldJump = true;
}

//When the button is released again,this function is called.
public void ButtonpressedUp(BaseEventData e)
{
    shouldJump = false;
}
}

所以我在类似以下的应用程序中对此进行了逗趣:

public class ScheduleManagerManager<TJob> : IScheduleManager where TJob : IJob
{
    private readonly string _cronExpression;
    private readonly string _jobIdentity;
    private readonly string _triggerIdentity;
    private IScheduler _scheduler;
    private IJobDetail _jobDetail;
    private ITrigger _trigger;

    public ScheduleManagerManager(string cronExpression)
    {
        _cronExpression = cronExpression;
        _jobIdentity = Guid.NewGuid().ToString();
        _triggerIdentity = Guid.NewGuid().ToString();
    }

    public async Task Run()
    {
        try
        {
            _scheduler = await StdSchedulerFactory.GetDefaultScheduler();

            await _scheduler.Start();

            _jobDetail = JobBuilder.Create<TJob>()
                .WithIdentity(_jobIdentity)
                .Build();

            _trigger = TriggerBuilder.Create()
                .WithIdentity(_triggerIdentity)
                .StartNow()
                .WithCronSchedule(_cronExpression)
                .Build();

            await _scheduler.ScheduleJob(_jobDetail,_trigger);
        }
        catch (SchedulerException se)
        {
            await Console.Out.WriteLineAsync(se.Message);
        }
    }

    public void ShutDown()
    {
        _scheduler.Shutdown();
    }
}

我想添加一个事件处理程序以在执行作业后获取作业数据。

var manager1 = new ScheduleManagerManager<MailSender>();
var manager2 = new ScheduleManagerManager<SmsSender>();

但是Quartz.net作业或触发器中没有事件。我该怎么办?

Yushujun 回答:如何处理Quartz.net作业执行事件?

但是Quartz.net作业或触发器中没有事件。当然,它们被称为Listener

只需实施IJobListener

public class MyJobListener : IJobListener
{
    public string Name () => "MyListener";

    public Task JobToBeExecuted(IJobExecutionContext context){ }

    public Task JobExecutionVetoed(IJobExecutionContext context){ }

    public Task JobWasExecuted(IJobExecutionContext context,JobExecutionException jobException)
    {
        // Do after Job stuff....
    }
}

并将其添加到ScheduleManagerManager中的调度程序中:

var myJobListener = new MyJobListener();
_scheduler.ListenerManager.AddJobListener(myJobListener,GroupMatcher<JobKey>.AnyGroup());
本文链接:https://www.f2er.com/2990656.html

大家都在问