Quartz.net没有开火而且没有抛出错误

时间:2014-11-18 01:44:28

标签: quartz.net

我正在尝试使用HttpModules运行Quartz.net调度程序,因为我们无法访问Global.ascx。直到执行作业的代码似乎工作正常,但是当涉及到作业本身时,它不会触发,也不会抛出任何错误。我甚至已经把这项工作变得像重定向一样简单,以防在代码中只有一些不起作用的东西,但也没有。 Quartz.net以这种方式工作的人都有成功吗?

这是HTTPModule:

public void Init(HttpApplication application)
    {
        application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        application.EndRequest += (new EventHandler(this.Application_EndRequest));
    }

    // Your BeginRequest event handler.
    private void Application_BeginRequest(Object source, EventArgs e)
    {
        try
        {
            Auction123CSVScheduler.Start();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    // Your EndRequest event handler.
    private void Application_EndRequest(Object source, EventArgs e)
    {

    }

    public void Dispose()
    {
    }

以下是调度程序:

public class Auction123CSVScheduler
{
    public Auction123CSVScheduler()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static void Start()
    {
        try
        {
            IScheduler scheduler = StdSchedulerFactory.GetDefaultScheduler();
            scheduler.Start();

            IJobDetail job = JobBuilder.Create<Auction123CSVJob>().Build();

            ITrigger trigger = TriggerBuilder.Create()
                .StartNow().WithSimpleSchedule(x => x.WithIntervalInSeconds(120).RepeatForever()).Build();

            scheduler.ScheduleJob(job, trigger);

        }
        catch (SchedulerException se)
        {
            throw se;
        }
    }
}

这是我们的工作:

public class Auction123CSVJob : IJob
{
    public void Execute(IJobExecutionContext context)
    {
        try
        {
            VehiclesDataSet vds = new VehiclesDataSet();

            DataSet ds = vds.Auction123Info();

            CreateCSVFile(ds);
        }
        catch (Exception e)
        {
            throw new JobExecutionException(e);
        }
    }


    //create csv file format from dataset passed in and save to the file system
    private void CreateCSVFile(DataSet ds)
    {

        if (ds.Tables.Count > 0)
        {
            //make the dataset into a datatable for use with streamwriter
            DataTable dt = ds.Tables[0];

            try
            {
                //identify file path. set to overwrite
                StreamWriter sw = new StreamWriter("c://inetpub/wwwroot/customer/myfile.txt", false);

                //write headers
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    sw.Write(dt.Columns[c]);
                    if (c < dt.Columns.Count - 1)
                    {
                        sw.Write("|");
                    }
                }

                sw.Write(sw.NewLine);

                //write content items
                foreach (DataRow dr in dt.Rows)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        if (!Convert.IsDBNull(dr[i]))
                        {
                            sw.Write(dr[i].ToString());
                        }

                        if (i < dt.Columns.Count - 1)
                        {
                            sw.Write("|");
                        }
                    }

                    sw.Write(sw.NewLine);
                }

                sw.Close();
            }
            catch (Exception ex)
            {
                throw new JobExecutionException(ex);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您没有维护对调度程序的引用。一旦start方法退出,调度程序就可能被垃圾收集,因此它的运行时间不足以运行该作业。在您的情况下,您应该在应用程序启动时创建调度程序,并在不超出范围的地方维护对它的引用。但这不是一个理想的场景。理想情况下,您应该将Quartz设置为单独的服务,因为IIS可以在任何时间点回收应用程序,从而终止您的调度程序。