본문 바로가기

개발일지

C# 에서 Quartz.NET 테스트 확인

728x90

주로 java 에서 스케쥴러를 사용하였는데 이제 c#에서도 스케쥴러를 이용하여 작업이 필요하여 테스트를 해봅니다.

아래 사이트를 참고하여 개발 테스트를 진행합니다.

https://www.quartz-scheduler.net/documentation/quartz-3.x/quick-start.html#download-and-install

 

Quartz.NET Quick Start Guide | Quartz.NET

Welcome to the Quick Start Guide for Quartz.NET. As you read this guide, expect to see details of: Downloading Quartz.NET Installing Quartz.NET Configuring Quartz to your own particular needs Starting a sample application Download and Install You can eithe

www.quartz-scheduler.net

Install-Package Quartz

1. NuGet Package 설치

2. Job class 작성

using Quartz;
using System;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class HelloJob : IJob
    {
        /// <summary>
        /// Called by the <see cref="IScheduler" /> when a
        /// <see cref="ITrigger" /> fires that is associated with
        /// the <see cref="IJob" />.
        /// </summary>
        public virtual Task Execute(IJobExecutionContext context)
        {
            // Say Hello to the World and display the date/time
            var timestamp = DateTime.Now;
            Console.WriteLine($"Hello World! - {timestamp:yyyy-MM-dd HH:mm:ss.fff}");
            return Task.CompletedTask;
        }
    }
}

3. 스케쥴러 작성

public static async Task start()
{
	// construct a scheduler factory
	StdSchedulerFactory factory = new StdSchedulerFactory();

	// get a scheduler
	IScheduler scheduler = await factory.GetScheduler();
	await scheduler.Start();

	// define the job and tie it to our HelloJob class
	IJobDetail job = JobBuilder.Create<HelloJob>()
		.WithIdentity("myJob", "group1")
		.Build();

	// Trigger the job to run now, and then every 40 seconds
	ITrigger trigger = TriggerBuilder.Create()
		.WithIdentity("myTrigger", "group1")
		.StartNow()
		.WithSimpleSchedule(x => x
		.WithIntervalInSeconds(10)
		.RepeatForever())
		.Build();

	// ICronTrigger sample
	/* ICronTrigger crontrigger = (ICronTrigger)TriggerBuilder.Create()
		.WithIdentity("trigger1", "group1")
		.WithCronSchedule("0/20 * * * * ?")
		.Build();	*/

	await scheduler.ScheduleJob(job, trigger);
}

간단하게 작업 테스트 합니다. 

728x90