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

发布于 2024-10-10 03:58:40 字数 2236 浏览 4 评论 0原文

我正在创建一个使用 Quartz.NET 的应用程序,该应用程序在 Windows 服务中使用。还有一个用 ASP.NET 编写的管理后端,管理员可以在其中添加作业并监视调度程序的状态。然而,我在 ASP.NET 后端遇到问题。

连接是在 Global.asax 文件中建立的,该文件一开始似乎工作正常 - 当用户登录到主仪表板时,它工作正常。问题是当用户单击不同的页面时,它会说调度程序“schedService”已经存在。

这是我的 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 代码:

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;
}

然后我在每个 ASP.NET 页面中使用它:

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

I am creating an application that uses Quartz.NET, which is utilised within a Windows service. There is also an administration backend written in ASP.NET, where administrators can add jobs and monitor the state of the scheduler. I am however having issues with the ASP.NET backend.

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. The problem is when the user clicks onto a different page, where it then says the scheduler 'schedService' already exists.

Here is my Windows Service code:

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:

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:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你好,陌生人 2024-10-17 03:58:40

你应该将 IScheduler 编写为单例

you should write your IScheduler as a singleton

仄言 2024-10-17 03:58:40

我对“调度程序已经存在”有类似的问题。我创建了一个单例类,并且能够解决我的问题。

如果任何人都可以使用的话,单例代码看起来像这样。该锁是为了避免以前当有更多线程尝试同时获取同一实例时出现的问题。

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 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;
        }
      }
    }
  }
}
初心未许 2024-10-17 03:58:40

我找到了另一个解决方案。通过给每个调度程序一个不同的名称,我能够解决这个问题。我发现这是比单例更好的解决方案。

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文