Create a Quartz.NET Job with several constructor parameters

3,391 views
Skip to first unread message

David Banks

unread,
Aug 6, 2015, 12:54:53 PM8/6/15
to Quartz.NET, David Banks

I have a job which needs to kick off some methods on another object. I'd like to be able to pass these into the job in its constructor.

Looking around, it seems that the only way to achieve this is to use one of IoC frameworks. Whilst this method will be a solution for me in the future, right now I need a vanilla solution, not requiring any IoC.

I am aware of the JobDataMap but the Best Practices documentation advises against this due to serialization. The object is multi-threaded and statefull, so serializing would be code suicide anyhow.

How can I create a job similar to below:


public class MyJob : IJob
{
    private readonly IFoo _foo;

        public StopMonitoring(IFoo foo)
        {
            _foo = foo;
        }

        public void Execute(IJobExecutionContext context)
        {
            foo.GetCurrentState();
        }
    }
}

Jay Vilalta

unread,
Aug 6, 2015, 1:09:04 PM8/6/15
to quar...@googlegroups.com, David Banks
Hi David

You don't need to use an ioc container but you'll have to implement your own JobFactory. The job factory that is included by default only supports parameterless constructors. You only have to override that NewJob method and then pass the parameters in to your job. I suppose you could subclass the one included in quartz.net and check the jobtype. If it's a MyJob type of then pass in your object.

Here's a post that might be useful although it is geared towards using an ioc container:
http://jayvilalta.com/blog/2012/07/23/creating-a-custom-quartz-net-jobfactory/
J

--
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 post to this group, send email to quar...@googlegroups.com.
Visit this group at http://groups.google.com/group/quartznet.
For more options, visit https://groups.google.com/d/optout.

David Banks

unread,
Aug 6, 2015, 1:50:24 PM8/6/15
to Quartz.NET, david...@computershare.co.uk
Thanks Jay,

I had a look at that, making the factory seems pretty trivial.  I assume It'd be something like:

public class MyJobFactory : IJobFactory
{
    private IFoo _foo;

    public MyJobFactory(IFoo foo)
   
{
        _foo
= foo;
   
}
   
   
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
   
{
       
return new MyJob(_foo);
   
}

   
public void ReturnJob(IJob job)
   
{
       
return new MyJob(_foo);
   
}
}

The bit I'm not getting is (probably so simple that I'm not even noticing it) is, if I have several JobFactories for different jobs which have different construction parameters, how do I go about using them?

So, creating a job is currently:

var myJob= JobBuilder.Create<MyJob>().WithIdentity("myJob").Build();

what do I need to change to get the builder to use my factory?

Cheers


David Banks

unread,
Aug 6, 2015, 1:58:02 PM8/6/15
to Quartz.NET, david...@computershare.co.uk
OK, I think it just clicked.  I can only have one factory, and I need to handle whatever needs doing to construct something in there!

So, in my case, I need some ugly type sniffing until I implement IoC.

Jay Vilalta

unread,
Aug 6, 2015, 2:10:28 PM8/6/15
to quar...@googlegroups.com, David Banks
Yep. Only one job factory is supported AFAIK.
Reply all
Reply to author
Forward
0 new messages