2

Is it possible to configure Quartz through a mixture of properties held in a config file (either quartz.properties or app.config / web.config) and also some via the StdSchedulerFactory constructor?

I would like to pass the AdoJobStore connectionstring via the constructor, as it is dynamic depending on the environment, but the rest of the settings are static so would be better placed in a config file.

I've tried passing in only the quartz.dataSource.myDS.connectionString property via the constructor, whilst having the rest of the properties in a quartz.config in the working directory. However, I get the error:

Provider not specified for DataSource: myDS

So I guess this means that if you use the constructor that accepts the NameValueCollection, then it doesn't bother checking the config file(s).

I know that the quartz.config file is in the right place, because if I put the connectionstring in there and use the default constructor, it all works

Simon Green
  • 1,131
  • 1
  • 10
  • 28

1 Answers1

3

In the end, they are all simply named-value pairs.

You can have "most" of them in an .xml file...then "add in" the ones you want via code.

Or have all of them in code.

See the UnitTests for the source code, and you'll see this fairly clearly.

Something like this:

    NameValueCollection config = (NameValueCollection)ConfigurationManager.GetSection("quartz");

    config.Add("MyCodedUpKey", "MyCodedUpValue");

If you have a "collision" (a "key" in the config file that you want to override..apply some simple name-valued-pair "update existing key" logic"

Check if Key Exists in NameValueCollection

Community
  • 1
  • 1
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Thanks, I've gone and implemented something like this (although I did note that the NameValueCollection that gets returned from the ConfigurationManager is read only, so you have to create a new collection, initialised with this read only one, and then I add the dynamic value – Simon Green Feb 27 '14 at 21:52
  • So, to confirm the answer to my question is that the StdSchedulerFactory will not automatically merge properties it finds in the quartz.config file and those passed in the constructor - you have to manually merge them and pass them all in the constructor :-) – Simon Green Feb 27 '14 at 21:54
  • No, I don't think so. You have to manually intervene. Which kinda makes sense...in the case of collisions. If they coded it up as "config wins" somebody would complain about that. If they coded it up as "code wins", somebody would complain about that. – granadaCoder Feb 27 '14 at 22:26
  • Hmmm, it's a fair point. But personally I would be happy to accept an exception in a collision, in order to get the extra convenience (in my particular case) – Simon Green Feb 28 '14 at 07:41
  • If they threw an exception, somebody would complain about that (me thinks) :) ........ can't win for lose sometimes. Here is one of those situations........ in the Sql Server world : http://connect.microsoft.com/SQLServer/feedback/details/382007/in-sqlcmd-scripts-setvar-should-have-a-lower-precedence-than-command-line-variable-assignments – granadaCoder Feb 28 '14 at 14:28
  • The loaded `NameValueCollection` is readonly and will throw at the `Add()` line. Construct new collection from the loaded one instead `var editableConfig = new NameValueCollection(config);` – wondra Oct 29 '18 at 14:49