gpt4 book ai didi

c# - Quartz.net 导致 .NET CLR LocksAndThreads

转载 作者:行者123 更新时间:2023-11-30 18:03:54 25 4
gpt4 key购买 nike

我们一直在使用 Quartz.net 项目来控制我们的一项 Windows 服务中的计划任务。我们已经使用它一段时间了,没有任何问题,但我们最近注意到 .NET CLR LocksAndThreads 存在问题。

请参阅此用 C# 编写的示例命令行应用程序。

using System;
using Quartz;
using Quartz.Impl;

namespace QuartzMemTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Quartz Test");
Console.WriteLine("-----------");
Console.WriteLine(Environment.NewLine);



ScheduleHelper.ScheduleJob(typeof(MyTask), "MyJobName", "MyTriggerName", "0 0/01 * 1/1 * ? *");

Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
}


public class ScheduleHelper
{
/// <summary>
/// Adds a job to the Quartz schedule
/// </summary>
/// <param name="job">The Job class which inherits from the Quartz.IJob interface</param>
/// <param name="jobName">A name to give to the job</param>
/// <param name="triggerName">A name to give to the trigger</param>
/// <param name="triggerCronExpression">CRON expression to determine the job run interval</param>
public static DateTime ScheduleJob(Type job, string jobName, string triggerName, string triggerCronExpression)
{
// Start the scheduler to run our ImportOpportunitiesJob
ISchedulerFactory schedFact = new StdSchedulerFactory();
IScheduler sched = schedFact.GetScheduler();
sched.Start();

// Create an instance of our job
JobDetail jobDetail = new JobDetail(jobName, null, job);

// Create a CRON trigger which determines the firing interval
CronTrigger trigger = new CronTrigger();
trigger.Name = triggerName;
trigger.StartTimeUtc = DateTime.UtcNow;
trigger.CronExpressionString = triggerCronExpression;

// Add job and trigger to the schedule
return sched.ScheduleJob(jobDetail, trigger);
}
}


public class MyTask : IStatefulJob
{

public void Execute(JobExecutionContext job)
{
Console.WriteLine("MyTask: Doing something....");
}

}
}

我们注意到,如果我们先启动性能监视器,然后运行上面的示例代码,我们可以看到 .NET CLR LocksAndThreads 突然开始增加,并持续增加,直到应用程序停止。我的一位同事在我们的一个实时服务器因资源耗尽而崩溃后首先注意到了这一点。

是 Quarts 导致了这个还是我在做什么蠢事?如果是 Quartz,我能做些什么来解决这个问题?

最佳答案

我们在我从事的一个 quartz.net 项目中遇到了类似的问题。我们发现,在某些情况下,线程池有时会泄漏资源并且无法正确清理自身。我最终实现了一个新的线程池,它具有更好的锁定和作业实际完成时间的通知。这为我们解决了这个问题。

关于c# - Quartz.net 导致 .NET CLR LocksAndThreads,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6734207/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com