简体   繁体   中英

.Net Quartz Windows Service resolve job dependencies

I've just started using Quartz.Net - basically I want to run it as a Windows Service and schedule jobs that are in different assemblies using the IJob interface. To do this by default I need to put the assemblies containing the Jobs (and all their dependencies) in the root folder with the Quartz.exe. All good - I've got this working. However I'd like to put the different jobs from different assemblies into specific folders under the root directory. When I do this Quartz cant resolve the dependencies - I realise I can create my own assembly resolver however I have no idea where to put it or what should instantiate it - any help would be great.

I tried creating my own JobFactory and resolving the dependecies in there based on a supplied path. However it dosnt work - for some reason the JobFactory fails because it cant find the Job dependecies - which is puzzling because the Job is only created in the JobFactory.NewJob method as below:

  public class MyQuartzJobFactory : IJobFactory

{ //Does this line then fails because it cant find dependency private static readonly ILog log = LogManager.GetLogger(typeof(MyQuartzJobFactory));

public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
{
  IJobDetail jobDetail = bundle.JobDetail;
  Type jobType = jobDetail.JobType;
  try
  {
    if (log.IsDebugEnabled)
    {
      log.Debug(string.Format(CultureInfo.InvariantCulture, "Producing instance of Job '{0}', class={1}", jobDetail.Key, jobType.FullName));
    }

    string path = "";
    if (jobDetail.JobDataMap.ContainsKey("custPath"))
    {
      path = jobDetail.JobDataMap["custPath"].ToString();
    }

    return ObjectUtils.InstantiateType<IJob>(jobType);
  }
  catch (Exception e)
  {
    SchedulerException se = new SchedulerException(string.Format(CultureInfo.InvariantCulture, "Problem instantiating class '{0}'", jobDetail.JobType.FullName), e);
    throw se;
  }
}

/// <summary>
/// Allows the job factory to destroy/cleanup the job if needed. 
/// No-op when using SimpleJobFactory.
/// </summary>
public void ReturnJob(IJob job)
{
  var disposable = job as IDisposable;
  if (disposable != null)
  {
    disposable.Dispose();
  }
}

}

If you have the assemblies in folders under where the executable is, it might be easier to use the probing element in the configuration file. Or, take a look at how the server example installs the service and create your own service that sets your assembly dependencies in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM