Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get executing job's name?

Tags:

quartz.net

In my application I have a static class (singleton) that needs to be initialized with some environmental variables that's used through out my layers, I'm calling it my applicationContext. That in turn has customer and user contexts.

As each job runs it modifies these customer and user contexts depending on the situation. The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts, therefor I need to keep multiple user and customer contexts alive for each running job and I need to be able to pick the right context by somehow being able to see what the current job is.

Is it possible to somehow get information about the current executing quartz.net job?

I'm envisioning something like this where "currentQuartzJob.Name" is made up and is the part I'm missing:

public class MyContextImpl : IApplicationContext {
private Dictionary<string,subContexts> allCustomerContexts;
public string CurrentContext
{
    get { return allCustomerContexts[currentQuartzJob.Name] };
}

}

edit:

I don't think it's possible to do what I wanted, that is to be able to get the executing job's name in a class that doesn't know about Quartz.Net.

What I really needed was a way to keep a different context for each job. I managed to do that by looking at the executing thread's ID as they seem to differ for each running job.

like image 577
ds99jove Avatar asked Jan 17 '11 10:01

ds99jove


3 Answers

Try this:

public void Execute(IJobExecutionContext context)
{
    var yourJobName = context.JobDetail.Key.Name;
}
like image 198
Andrew Usikov Avatar answered Nov 07 '22 02:11

Andrew Usikov


Given your statement above: "The problem I have is that when 2 jobs fires concurrently they might overwrite each others contexts", you may want to reduce the complexity of your application by making sure that jobs do not fire concurrently. This can be achieved by implementing the IStatefulJob interface instead of the usual IJob interface: http://quartznet.sourceforge.net/tutorial/lesson_3.html

Alternatively if that is not an option you can query the Scheduler object for the currently executing jobs via the Ischeduler.GetCurrentlyExecutingJobs() method. This method returns an IList of those jobs but note the following remarks when using that method (from version 1.0.3 API):

  • This method is not cluster aware. That is, it will only return Jobs currently executing in this Scheduler instance, not across the entire cluster.
  • Note that the list returned is an 'instantaneous' snap-shot, and that as soon as it's returned, the true list of executing jobs may be different. Also please read the doc associated with JobExecutionContext- especially if you're using remoting.
like image 2
Andrew Thompson Avatar answered Nov 07 '22 04:11

Andrew Thompson


I'm not entirely sure of what you're doing with your application, but I will say that the name of the currently executing job is definitely available during job execution.

Inside the IJob Execute() method:

// implementation in IJob
public void Execute(JobExecutionContext context)
{
   // get name here
   string jobName = context.JobDetail.Name;

}

Is that what you're looking for?

like image 1
warriorpostman Avatar answered Nov 07 '22 04:11

warriorpostman