0

I have created a generic job schedule manager class. I am using Quartz.net 3.0.7 in .net core app.

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

So I amusing this in applications like following:

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

I want to add an event handler to get job data after executed the job.

var manager1 = new ScheduleManagerManager<MailSender>();
manager1.JobExecuted += OnJobExecuted();

But there is no event in Quartz.net jobs or triggers. How can I do it?

walen
  • 7,103
  • 2
  • 37
  • 58
barteloma
  • 6,403
  • 14
  • 79
  • 173
  • Is there any specific reason that you use multiple schedulers? – Rabban Dec 03 '19 at 15:38
  • No there is no reason, but I am new at quartz.net. Actually I want to create a JobViewModel in my wpf app. And I will show the all of the status of job to user. For example job started, last run at time, etc. – barteloma Dec 03 '19 at 18:27

1 Answers1

0

But there is no event in Quartz.net jobs or triggers. Of course they are, they are called Listener.

Just implement 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....
    }
}

And add it to your scheduler in your ScheduleManagerManager:

var myJobListener = new MyJobListener();
_scheduler.ListenerManager.AddJobListener(myJobListener, GroupMatcher<JobKey>.AnyGroup());
Rabban
  • 2,451
  • 1
  • 19
  • 21
  • OK. But I have a panel for every job in UI, so how can I understan which listener is executed for? I have multiple jobs. – barteloma Dec 03 '19 at 18:10