How to get the name of an executable job?

In my application, I have a static class (singleton) that needs to be initialized with some environment variables that are used through my layers, I call it my applicationContext. This, in turn, has client and user contexts.

As each task completes, it changes these client and user contexts depending on the situation. The problem is that when 2 tasks are triggered at the same time, they can rewrite each other’s contexts, so I need to maintain several user and client contexts for each task to be performed, and I need to be able to choose the appropriate context in order to somehow see what it is current work.

Is there any way to get information about the current work being done by quartz.net?

I imagine something like this where "currentQuartzJob.Name" is composed and is the part that I am missing:

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

}

edit:

I do not think it is possible to do what I wanted to be able to get the name of the executable job in a class that does not know about Quartz.Net.

What I really need is a way to keep a different context for each job. I managed to do this by looking at the identifier of the executable thread, since they seem to be different for each task that is performed.

+4
source share
3 answers

Try the following:

 public void Execute(IJobExecutionContext context) { var yourJobName = context.JobDetail.Key.Name; } 
+10
source

Given your statement above: “The problem is that when two tasks are performed simultaneously, they can overwrite each other’s contexts,” you can reduce the complexity of your application by making sure that the tasks do not work simultaneously. This can be achieved by implementing the IStatefulJob interface instead of the regular IJob interface: http://quartznet.sourceforge.net/tutorial/lesson_3.html

Otherwise, if this is not an option, you can request the Scheduler object for currently running jobs using the Ischeduler.GetCurrentlyExecutingJobs () method. This method returns the IList of these jobs, but when using this method (from version 1.0.3 of the API), pay attention to the following notes:

  • This method is not known to clusters. That is, it will only return jobs that are running in the current instance of Scheduler, and not through the entire cluster.
  • Note that the returned list is a snap-shot snapshot, and as soon as it returns, the true list of completed tasks may vary. Also read the document related to JobExecutionContext, especially if you are using remote access.
+2
source

I'm not quite sure what you are doing with your application, but I will say that the name of the job currently being executed is definitely available during the job.

Inside the IJob Execute () method:

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

Is this what you are looking for?

+1
source

Source: https://habr.com/ru/post/1335811/


All Articles