Get an instance of the scheduler that is being run on a Windows service

拈花ヽ惹草 提交于 2019-11-29 08:26:43

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" />

Your Windows app can then accesss 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 master contains really good examples that you won't find in the documentation.

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

As for adding and removing jobs. You won't get the instance of the Windows service's scheduler. There are two ways to work around it.

  1. Define a WCF contract and host a WCF service in your Windows service. It's pretty straightforward. You don't need IIS nor HTTP. I recommend the TCP binding in this case.
  2. Since you're already using a ADO job store, you can set up both Windows app and Windows service as a Quartz cluster:

Add

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

to both app and web configs. If I remember correctly, no additional code is required. Additionally you prevent the Windows app form executing a job by setting up a zero-size thread pool:

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

Now, you instantiate a scheduler in your Windows app and use it to add and remove jobs. The jobs will be stored in the ADO job store and picked up by the Windows service. Both app and service must have the same ADO job setore configured, obviously, and the Windows app must have access to the sqlite db.

One more thing. Using the second approach you won't be able to interrupt a running job form the Windows app.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!