Get an instance of the scheduler that runs on a Windows service

Suppose I provisioned my Quartz.NET as a Windows service and it is currently running (from ADOJobStore

running to Sqlite

). I need to take control of this service in my windows application so that I can stop it, start it, add and remove jobs from it, etc. How can I get an instance of this scheduler?

Sorry if this sounds like a simple question to you, but the documentation on Quartz.NET seems nowhere to be sufficient. There are only a few people who know about this and they already have a life to live.

Update: My service quartz.config file

# You can configure your scheduler in either <quartz> configuration section
# or in quartz properties file
# Configuration section has precedence

quartz.threadPool.type = Quartz.Simpl.SimpleThreadPool, Quartz
quartz.threadPool.threadCount = 10
quartz.threadPool.threadPriority = Normal

# job initialization plugin handles our xml reading, without it defaults are used
quartz.plugin.xml.type = Quartz.Plugin.Xml.XMLSchedulingDataProcessorPlugin, Quartz
quartz.plugin.xml.fileNames = ~/quartz_jobs.xml

quartz.scheduler.exporter.type = Quartz.Simpl.RemotingSchedulerExporter, Quartz
quartz.scheduler.exporter.port = 555
quartz.scheduler.exporter.bindName = QuartzScheduler
quartz.scheduler.exporter.channelType = tcp
quartz.scheduler.exporter.channelName = httpQuartz

      

The code I'm using in my program to get the scheduler:

NameValueCollection properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteClient";

// set thread pool info
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "10";
properties["quartz.threadPool.threadPriority"] = "Normal";

// set remoting expoter
properties["quartz.scheduler.proxy"] = "true";
properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:555/QuartzScheduler";

ISchedulerFactory sf = new StdSchedulerFactory(properties);
IScheduler sched = sf.GetScheduler();

      

My service is installed and in "Start" state, and it logs in as "Local System Account" and can interact with the desktop.

+3


source to share


2 answers


Your service can expose the scheduler by modifying the config file:

<add key="quartz.scheduler.exporter.type" value="Quartz.Simpl.RemotingSchedulerExporter, Quartz" />
<add key="quartz.scheduler.exporter.port" value="555" />
<add key="quartz.scheduler.exporter.bindName" value="QuartzScheduler" />
<add key="quartz.scheduler.exporter.channelType" value="tcp" />
<add key="quartz.scheduler.exporter.channelName" value="httpQuartz" />

      

Then your Windows application can access it with the appropriate settings:



        //you can put these in a config file too.
        NameValueCollection properties = new NameValueCollection();
        properties["quartz.scheduler.instanceName"] = "RemoteClient";

        // set thread pool info
        properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
        properties["quartz.threadPool.threadCount"] = "5";
        properties["quartz.threadPool.threadPriority"] = "Normal";

        // set remoting expoter
        properties["quartz.scheduler.proxy"] = "true";
        properties["quartz.scheduler.proxy.address"] = "tcp://127.0.0.1:555/QuartzScheduler";

        ISchedulerFactory sf = new StdSchedulerFactory(properties);
        IScheduler sched = sf.GetScheduler();

      

The Quartz.net wizard contains some really good examples that you won't find in the documentation.

+5


source


Starting and stopping the Windows service is not related to Quartz. There seems to be a .NET API for this , but I'm not familiar with it.

As for adding and removing tasks. You will not get an instance of the Windows Service Scheduler. There are two ways to get around this.

  • Define a WCF contract and start the WCF service on your Windows service. It's pretty straight forward . You don't need IIS and HTTP. I recommend TCP binding in this case.
  • Since you are already using ADO Job Store, you can configure both a Windows application and a Windows service as a quartz cluster:

Add to

<add key="quartz.jobStore.clustered" value="true"/>

      



for applications and web configurations. If I remember correctly, no additional code is required. Also, you disallow the creation of the Windows application form by setting the thread pool to zero size:

<add key="quartz.threadPool.type" value="Quartz.Simpl.ZeroSizeThreadPool, Quartz"/>

      

Now you create a scheduler in your Windows app and use it to add and remove tasks. The seats will be stored in the ADO Job Store and loaded by the Windows service. Obviously, the application and service must have the same ADO job job configured, and the windows application must have access to the sqlite db.

One more thing. Using the second approach, you cannot interrupt a running task from a Windows application.

+2


source







All Articles