使用Quartz.Net嵌入到Windows服务中

时间:2011-05-19 15:49:18

标签: windows-services quartz.net

我正在尝试使用Quartz.Net,以便在我开发的Windows服务中安排作业。

我在 Onstart 方法中包含以下代码, scheduler 是一个Class属性 private readonly IScheduler scheduler;

logger = LogManager.GetLogger(typeof (TelegestionService));
scheduler = new StdSchedulerFactory().GetScheduler();
var job = new JobDetail("job1", "group1", typeof (HelloJob));
var trigger = new SimpleTrigger("trigger1", "group1", runTime);
scheduler.ScheduleJob(job, trigger);

这对我来说很好。我让约伯跑了。

现在我试图使调度程序可以远程访问,基于Quartz源示例中的Example12(控制台服务器/客户端工作正常)。

var properties = new NameValueCollection();
properties["quartz.scheduler.instanceName"] = "RemoteServer";
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = "5";
properties["quartz.threadPool.threadPriority"] = "Normal";
properties["quartz.scheduler.exporter.type"] = "Quartz.Simpl.RemotingSchedulerExporter, Quartz";
properties["quartz.scheduler.exporter.port"] = "555";
properties["quartz.scheduler.exporter.bindName"] = "QuartzScheduler";
properties["quartz.scheduler.exporter.channelType"] = "tcp";

scheduler = new StdSchedulerFactory(properties).GetScheduler();

服务正常启动,调度程序也是如此,但我无法使用控制台/ Winform客户端远程安排作业(拒绝连接)。

我使用SysInternals TcpView检查了服务器上的LISTENING端口,但找不到上面指定的555端口。

我怀疑与.Net Remoting有关的问题,但无法弄清楚如何解决这个问题。 有什么想法吗?

提前致谢。

1 个答案:

答案 0 :(得分:0)

可以使用http://topshelf-project.com/来托管Scheduler,后者将提供hostFactory并使用可以通过HttpSelfHostServer托管主机的主机,http更好,因为您可以通过控制器调用job。示例代码如下

希望这会有所帮助。

using System;
using System.Configuration;
using System.Web.Http;
using System.Web.Http.SelfHost;
using Topshelf;
     class Program
    {
        static void Main(string[] args)
        {
            HostFactory.Run(hostConfigurator =>
            {
                if (!Uri.TryCreate("http://localhost:8080", UriKind.RelativeOrAbsolute, out var hostname))
                {
                    throw new ConfigurationErrorsException($"Could not uri");
                }

                var serviceName = "my service";

                var hostConfiguration = new HttpSelfHostConfiguration(hostname);
                hostConfiguration.Routes.MapHttpRoute("API", "{controller}/{action}/{id}", (object)new
                {
                    id = RouteParameter.Optional
                });

                var httpSelfHostServer = new HttpSelfHostServer(hostConfiguration);

                // Impl.Scheduler would be your implementation of scheduler
                hostConfigurator.Service<Impl.Scheduler>(s =>
                {
                    s.ConstructUsing(name => new Impl.Scheduler());
                    s.WhenStarted(tc =>
                    {
                        tc.Start();

                        httpSelfHostServer.OpenAsync().Wait();
                    });
                    s.WhenStopped(tc =>
                    {
                        tc.Stop();
                        //dispose scheduler implementation if using IOC container
                        httpSelfHostServer.CloseAsync().Wait();
                    });
                });
                hostConfigurator.RunAsLocalSystem();

                hostConfigurator.SetDescription(serviceName);
                hostConfigurator.SetDisplayName(serviceName);
                hostConfigurator.SetServiceName(serviceName);
            });
        }
    }