简体   繁体   English

Quartz.NET远程处理 - 调度程序已经存在

[英]Quartz.NET remoting - scheduler already exists

I am creating an application that uses Quartz.NET, which is utilised within a Windows service. 我正在创建一个使用Quartz.NET的应用程序,它在Windows服务中使用。 There is also an administration backend written in ASP.NET, where administrators can add jobs and monitor the state of the scheduler. 还有一个用ASP.NET编写的管理后端,管理员可以在其中添加作业并监视调度程序的状态。 I am however having issues with the ASP.NET backend. 但是我遇到了ASP.NET后端的问题。

The connection is made in the Global.asax file, which seems to work at first - when the user logs on to the main dashboard, it works fine. 连接是在Global.asax文件中进行的,该文件似乎首先起作用 - 当用户登录主仪表板时,它可以正常工作。 The problem is when the user clicks onto a different page, where it then says the scheduler 'schedService' already exists. 问题是当用户点击不同的页面时,表示调度程序'schedService'已经存在。

Here is my Windows Service code: 这是我的Windows服务代码:

NameValueCollection properties = new NameValueCollection();

properties["quartz.scheduler.instanceName"] = "schedService";
properties["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz";
properties["quartz.threadPool.threadCount"] = threadcount;
properties["quartz.threadPool.threadPriority"] = "Normal";
properties["quartz.jobStore.misfireThreshold"] = "60000";
properties["quartz.jobStore.type"] = "Quartz.Impl.AdoJobStore.JobStoreTX, Quartz";
properties["quartz.jobStore.useProperties"] = "false";
properties["quartz.jobStore.dataSource"] = "default";
properties["quartz.jobStore.tablePrefix"] = "QRTZ_";

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

//
// if running MS SQL Server we need thisl
properties["quartz.jobStore.lockHandler.type"] = "Quartz.Impl.AdoJobStore.UpdateLockRowSemaphore, Quartz";

// config file
properties["quartz.dataSource.default.connectionString"] = connection;
properties["quartz.dataSource.default.provider"] = "SqlServer-20";

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

ASP.NET Global.asax code: ASP.NET Global.asax代码:

public static IScheduler Sched()
{
    NameValueCollection properties = new NameValueCollection();

    properties["quartz.scheduler.instanceName"] = "schedMaintenanceService";

    properties["quartz.scheduler.proxy"] = "true";
    properties["quartz.scheduler.proxy.address"] = "tcp://localhost:555/QuartzScheduler";

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

Then I use this in each ASP.NET page: 然后我在每个ASP.NET页面中使用它:

public static IScheduler sched = Project.Global.Sched();

你应该把你的IScheduler写成一个单身人士

I had a similar issue with the "scheduler already exists". “调度程序已经存在”,我遇到了类似的问题。 I created a singleton class and I was able move past my issue. 我创建了一个单例类,我能够超越我的问题。

The singleton code looks like this if anybody can use it. 如果有人可以使用它,单例代码看起来像这样。 The lock is to avoid the issue from before when having more threads trying to get the same instance at once. 锁定是为了避免以前有多个线程试图同时获取同一个实例的问题。

using Quartz;
using Quartz.Impl;

namespace MyProject.CommonObjects.Utilities
{
  public static class QuartzFactory
  {
    private static object _locker = new object();
    private static IScheduler _schedulerInstance;

    public static IScheduler SchedulerInstance
    {
      get
      {
        lock (_locker)
        {
          if (_schedulerInstance == null)
            _schedulerInstance = StdSchedulerFactory.GetDefaultScheduler();

          return _schedulerInstance;
        }
      }
    }
  }
}

I found another solution. 我发现了另一个解决方 By giving each scheduler a different name I was able to work around the issue. 通过给每个调度程序一个不同的名称,我能够解决这个问题。 I find this a better solution than the singleton one. 我觉得这比单身人士更好。

  NameValueCollection properties = new NameValueCollection
  {
    [StdSchedulerFactory.PropertySchedulerInstanceName] = "MyNewScheduler1"
  };
  StdSchedulerFactory fac = new StdSchedulerFactory(properties);
  _scheduler = fac.GetScheduler();
  _scheduler.Start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM