简体   繁体   中英

Manually triggering a quartz job with arguments that is hosted in a windows service

I have created a Quartz server running in a windows service that has various scheduled jobs.

However, there is one job that I need to be triggered manually from an event in my web application UI.

Quartz.NET job:

public class IntensiveJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        // Get job parameters here... BUT HOW?!

        // Do some intensive processing here...
    }
}

Action that I need to trigger the job in:

public class SomeController : Controller
{
    [HttPost]
    public ActionResult Run()
    {
        // Need to be able to trigger the intensive job here... 
        // Ideally with some arguments too... E.g:
        var job = new IntensiveJob();
        job.Execute();

        return RedirectToAction("Index");
    }
}

Any suggestions on best way to implement this or alternative approaches would be great.

The solution that best fits your problem is to create a trigger manually that gets executed now by the scheduler. The problem that arises is if you need to have different data in your trigger each time it is created to be passed to the scheduler. The trigger factory that is created by quartz will always produce an identical object when asked for a new trigger. You would have to instantiate an instance of the Trigger Impl with the necessary details and pass that to the scheduler to run. An example in Java would look something like:

JobDetailImpl jdi = new JobDetailImpl();
jdi.setJobClass(SomeClassWithJob);//The class that contains the task to done.
SimpleTriggerImpl sti = new SimpleTriggerImpl();
sti.setStartTime(new Date(System.currentTimeMillis()));
sti.setRepeatInterval(1);
sti.setRepeatCount(0);
context.getScheduler().scheduleJob(jdi,sti); //pseudocode here

The key thing is to use quartz for the scheduling and not to just insert data into the database. Also, each of the items require a name and group.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM