Dependency Injection In Job

214 views
Skip to first unread message

Basil Babu

unread,
Mar 10, 2020, 3:08:34 AM3/10/20
to Quartz.NET
Hi,

Can someone help me in adding dependency injection in jobs? 

Is there any way? 

Phil Boyd

unread,
Mar 10, 2020, 10:49:20 AM3/10/20
to quar...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "Quartz.NET" group.
To unsubscribe from this group and stop receiving emails from it, send an email to quartznet+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quartznet/d7e43c44-db17-42a0-b14a-bcfb2b183c6f%40googlegroups.com.

Marty Wasznicky

unread,
Mar 10, 2020, 4:44:37 PM3/10/20
to Quartz.NET
I ended up using Simple Injector.  

within my custom Scheduler class I create the container reference:

        private Container _container = new Container();


within my Start() method of the Scheduler:

            _container.Options.AllowOverridingRegistrations = true;

            
            ISchedulerFactory schedulerFactory = new StdSchedulerFactory(schedulerProperties);
            _scheduler = await schedulerFactory.GetScheduler();
            IJobFactory jobFactory = new PeregrineJobFactory(_container);   // my custom job factory
            _scheduler.JobFactory = jobFactory;

             // these are objects I can grab within the job factory class
            _container.RegisterInstance(_scheduler);
            _container.RegisterInstance(_log);
            _container.RegisterInstance(_configuration);
            _container.RegisterInstance(_logconfig);

            // use reflection to get all the jobs to register. They all have to implement IPeregrineJob interface
            var path = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "PeregrineJobs");


            var jobAssemblies =
                from file in new DirectoryInfo(path).GetFiles()
                where file.Extension.ToLower() == ".dll"
                select Assembly.Load(AssemblyName.GetAssemblyName(file.FullName));

            _container.Collection.Register<IPeregrineJob>(jobAssemblies);


After that...my JobFactory class is simple:


public class PeregrineJobFactory : IJobFactory
    {
        private readonly IServiceProvider serviceProvider;
        private ILog log;
        private LogConfiguration logconfig;
        private static Dictionary<string, ILog> jobLogs = new Dictionary<string, ILog>();
        private IScheduler scheduler;

        /// <summary>
        /// The serviceProvider is the container being passed to the constructor by the Peregrine Scheduler Service
        /// </summary>
        /// <param name="serviceProvider"></param>
        public PeregrineJobFactory(IServiceProvider serviceProvider)
        {
            this.serviceProvider = serviceProvider;
        }

        /// <summary>
        /// Creates a new Peregrine Scheduler Job, by using Simple Injection (i.e. GetService(type) )
        /// off of the container passed by the Peregrine Scheduler Service. 
        /// This handles logging and creating a log specific to the Peregrine Scheduler Job Type and then 
        /// injecting the log as well as the several parameters from the Quartz JobDataMap into the 
        /// instance of the job created. 
        /// 
        /// if an error occurs, its logged and a SchedulerException() is thrown.
        /// </summary>
        /// <param name="bundle"></param>
        /// <param name="scheduler"></param>
        /// <returns>Quartz.NET Scheudler Job</returns>
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            IJobDetail jobDetail = bundle.JobDetail;

            //Retreive the log and LogConfiguration object from the container. originally set the Peregrine Scheduler Service
            this.log = this.serviceProvider.GetService(typeof(ILog)) as ILog;
            this.logconfig = this.serviceProvider.GetService(typeof(LogConfiguration)) as LogConfiguration;
            this.scheduler = this.serviceProvider.GetService(typeof(IScheduler)) as IScheduler;
            var configuration = this.serviceProvider.GetService(typeof(ESBConfiguration)) as ESBConfiguration;
            try
            {
                
   
                Type jobType = jobDetail.JobType;
                
                if (this.log.IsDebugEnabled)
                    this.log.Debug($"Received request to create instance of Job '{jobDetail.Key}', class='{jobType.FullName}'");

                // Retreive the parameters to inject into the Peregrine Job that's created. The Job will be able to use and update
                // these.
                var requestMessage = bundle.JobDetail.JobDataMap.GetString("peregrine.requestMessage");
                var responseMessage = bundle.JobDetail.JobDataMap.GetString("peregrine.responseMessage");
                var lastexecutionTime = bundle.JobDetail.JobDataMap.GetString("peregrine.lastExecutionTimeUTC");

                // copy the jobdatamap to a serialized dictionary
                SerializableDictionary<string, object> jobDataMapDictionary = new SerializableDictionary<string, object>();
                foreach (var item in bundle.JobDetail.JobDataMap)
                {
                    jobDataMapDictionary.Add(item.Key, item.Value);
                    if (this.log.IsInfoEnabled)
                        this.log.Info($"Job Data Map key '{item.Key}' with value of '{item.Value}' added to Job Dictionary");
                }

                if (this.log.IsDebugEnabled)
                {
                    this.log.Debug($"Retrieved Request Message to pass to scheduled instance of Job '{jobDetail.Key}', class='{jobType.FullName}'. Request Message={requestMessage}");
                    this.log.Debug($"Retrieved the last Response Message to pass to scheduled instance of Job '{jobDetail.Key}', class='{jobType.FullName}'. Response Message={responseMessage}");
                    this.log.Debug($"Retrieved the Last Execution Timestamp to pass to scheduled instance of Job '{jobDetail.Key}', class='{jobType.FullName}'. Last Execution Timestamp UTC={lastexecutionTime}");
                }

                // Create an instance of the job using the container service injected
                var job = serviceProvider.GetService(jobType) as IPeregrineJob;
                if (job == null)
                    throw new ArgumentNullException($"job", "The service provider returned a NULL instance of Job '{jobDetail.Key}', class='{jobType.FullName}'");

                if (this.log.IsDebugEnabled)
                    this.log.Debug($"Instance of Job '{jobDetail.Key}', class='{jobType.FullName}' returned and properties being set.");

                // Set the properties of the Job with the Parameters from the JobDataMap
                job.RequestMessage = requestMessage;
                job.ResponseMessage = responseMessage;
                job.LastExecutionTimeUTC = lastexecutionTime;
                job.Configuration = configuration;
                job.JobDataDictionary = jobDataMapDictionary;
                job.Key = bundle.JobDetail.Key;

                // set log object specific to the job type. Each job type will have its own unique log file generated.
                if (jobLogs.ContainsKey(jobType.FullName))
                    job.Log = jobLogs[jobType.FullName];
                else
                {
                    var jobLog = logconfig.ConfigureLogger("PeregrineScheduler", "Default", "MyApplicationName", System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), "EndpointHost", "PeregrineScheduler", jobType);
                    jobLogs.Add(jobType.FullName, jobLog);
                    job.Log = jobLog;
                }

                if (this.log.IsInfoEnabled)
                    this.log.Info($"Instance of Job '{jobDetail.Key}', class='{jobType.FullName}' was created successfully!");

                return job as IJob;
            }
            catch (Exception ex)
            {
                this.log.Error($"An error occurred creating the Job '{jobDetail.Key}' class='{jobDetail.JobType.FullName}'. {ex.Message}", ex);
                throw new SchedulerException($"An error occurred creating the Job '{jobDetail.Key}' class='{jobDetail.JobType.FullName}'", ex);
            }
        }

        public void ReturnJob(IJob job)
        {
 
            var disposable = job as IDisposable;
            disposable?.Dispose();
        }
    }

Basil Babu

unread,
Mar 11, 2020, 2:24:31 AM3/11/20
to Quartz.NET
I used the dependency injection in this way. but got errors in the binding of the interface with the class.

ca you please help with it.


On Tuesday, March 10, 2020 at 8:19:20 PM UTC+5:30, Phil Boyd wrote:

Does this help?

https://knightcodes.com/.net/2016/08/15/dependency-injection-for-quartz-net.html

 

 

From: quar...@googlegroups.com <quar...@googlegroups.com> On Behalf Of Basil Babu
Sent: Tuesday, March 10, 2020 03:09
To: Quartz.NET <quar...@googlegroups.com>
Subject: [quartznet:4346] Dependency Injection In Job

 

Hi,

 

Can someone help me in adding dependency injection in jobs? 


Is there any way? 

--
You received this message because you are subscribed to the Google Groups "Quartz.NET" group.

To unsubscribe from this group and stop receiving emails from it, send an email to quar...@googlegroups.com.

Marty Wasznicky

unread,
Mar 11, 2020, 9:14:16 AM3/11/20
to Quartz.NET
you're going to have to give me more than that :)

Basil Babu

unread,
Mar 11, 2020, 9:22:05 AM3/11/20
to quar...@googlegroups.com
I have a job that uses Dependency Injection. But the injecting service also uses Dependency Injection for its operations. 

This creates some errors. 
To unsubscribe from this group and stop receiving emails from it, send an email to quartznet+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quartznet/eb0623ec-f35a-4844-9420-d18307ec664c%40googlegroups.com.
--
Regards,
Basil Babu
Mobile : +91 9895780415 (India)

https://twitter.com/basiliandme  www.linkedin.com/in/hariprasad-vnair
Reply all
Reply to author
Forward
0 new messages