How to run Quartz.NET Jobs in a separate AppDomain?

Can I run Quartz.NET jobs in a separate AppDomain application? If so, how can this be achieved?

+4
source share
1 answer

Disclaimer: I have not tried this, it is just an idea. And not one of this code was even compiled.

Create your own factory job, which creates a wrapper for your real jobs. Ask this shell to implement the Execute method by creating a new application domain and running the original job in this application domain.

More: Create a new type of work, say IsolatedJob : IJob . Apply this job as a constructor parameter to the type of job that it should encapsulate:

 internal class IsolatedJob: IJob { private readonly Type _jobType; public AutofacJob(Type jobType) { if (jobType == null) throw new ArgumentNullException("jobType"); _jobType = jobType; } public void Execute(IJobExecutionContext context) { // Create the job in the new app domain System.AppDomain domain = System.AppDomain.CreateDomain("Isolation"); var job = (IJob)domain.CreateInstanceAndUnwrap("yourAssembly", _jobType.Name); job.Execute(context); } } 

You may need to create an implementation of IJobExecutionContext , which inherits from MarshalByRefObject and proxy calls to the original context object. Given the number of other objects that the IJobExecutionContext provides access to, I will be tempted to implement many members using a NotImplementedException , since most of them will not be needed during the execution of the task.

Then you need a custom factory job. This bit is simpler:

 internal class IsolatedJobFactory : IJobFactory { public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler) { return NewJob(bundle.JobDetail.JobType); } private IJob NewJob(Type jobType) { return new IsolatedJob(jobType); } } 

Finally, you will need to instruct Quartz to use this factory task, not out of the box. Use the IScheduler.JobFactory property IScheduler.JobFactory and specify a new instance of IsolatedJobFactory .

+4
source

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


All Articles