【问题标题】:Using Quartz.Net embedded into a Windows Service使用嵌入到 Windows 服务中的 Quartz.Net
【发布时间】:2019-04-29 23:18:43
【问题描述】:

我正在尝试使用 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);

这对我来说很好。我让 Job 运行起来了。

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

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 防火墙以确保可执行文件可以打开该端口?

标签: windows-services quartz.net


【解决方案1】:

可以使用http://topshelf-project.com/ 来托管提供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);
            });
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 2011-06-24
    • 2013-04-07
    • 2014-07-12
    • 1970-01-01
    • 2017-06-02
    • 2012-10-15
    相关资源
    最近更新 更多