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) {
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
.
source share