是否可以在单独的AppDomain中运行Quartz.NET作业?如果是这样,怎么能实现呢?
免责声明:我没有尝试过这个,这只是一个想法。甚至这些代码都没有被编译。
创建一个自定义作业工厂,为您的实际作业创建包装器。让这个包装器Execute通过创建一个新的应用程序域并在该应用程序域中运行原始作业来实现该方法。
更详细地说:创建一个新类型的工作,比如说IsolatedJob : IJob。让这个作业将它应该封装的作业类型作为构造函数参数:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要创建一个IJobExecutionContext从MarshalByRefObject原始context对象继承和代理调用的实现。鉴于IJobExecutionContext提供访问权限的其他对象的数量,我很想实现许多带有 a 的成员,NotImplementedException因为在作业执行期间大多数不需要。
接下来您需要自定义作业工厂。这一点更容易:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
最后,您需要指示 Quartz 使用这个作业工厂,而不是开箱即用的。使用IScheduler.JobFactory属性设置器并提供IsolatedJobFactory.
| 归档时间: |
|
| 查看次数: |
1078 次 |
| 最近记录: |