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

问题描述 投票:2回答:1

我正在尝试使用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有关的问题,但无法弄清楚如何解决这个问题。有任何想法吗 ?

提前致谢。

windows-services quartz.net
1个回答
0
投票

可以使用http://topshelf-project.com/来托管Scheduler,它将提供hostFactory并使用它可以通过HttpSelfHostServer架设主机,http更好,因为你可以通过控制器调用作业。示例代码如下

希望这可以帮助。

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);
            });
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.