簡體   English   中英

Quartz.NET遠程處理 - 調度程序已經存在

[英]Quartz.NET remoting - scheduler already exists

我正在創建一個使用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();

你應該把你的IScheduler寫成一個單身人士

“調度程序已經存在”,我遇到了類似的問題。 我創建了一個單例類,我能夠超越我的問題。

如果有人可以使用它,單例代碼看起來像這樣。 鎖定是為了避免以前有多個線程試圖同時獲取同一個實例的問題。

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

我發現了另一個解決方 通過給每個調度程序一個不同的名稱,我能夠解決這個問題。 我覺得這比單身人士更好。

  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