[C#] quartz 모듈 사용하기

2023. 1. 28. 19:33프로그래밍

728x90

 

 

 

What happens when quartz sheduler becomes out of scope?

What happens when quartz sheduler becomes out of scope? Will Garbage Collector kills it or it will keep running? Samle code: namespace Quartz_test { public class SimpleJob : IJob {

stackoverflow.com

 

예제 샘플은 대략 아래와 같습니다.

버전은 2.6 이며, 비주얼스투디어 2012에서 확인 하였음.

using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Quartz_test
{
    public class SimpleJob : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            Console.WriteLine("hello there");
        }
    }

    class Program
    {

        private static void GetAllJobs(IScheduler scheduler)
        {
            IList<string> jobGroups = scheduler.GetJobGroupNames();
            // IList<string> triggerGroups = scheduler.GetTriggerGroupNames();
            Console.Write("START ===== \n is start? " + scheduler.IsStarted);
            Console.WriteLine("is stop? " + scheduler.IsShutdown);
            foreach (string group in jobGroups)
            {
                var groupMatcher = GroupMatcher<JobKey>.GroupContains(group);
                var jobKeys = scheduler.GetJobKeys(groupMatcher);
                Console.WriteLine();
                foreach (var jobKey in jobKeys)
                {
                    var detail = scheduler.GetJobDetail(jobKey);
                    var triggers = scheduler.GetTriggersOfJob(jobKey);
                    foreach (ITrigger trigger in triggers)
                    {
                        Console.WriteLine("group : " + group);
                        Console.WriteLine("name : " + jobKey.Name);
                        Console.WriteLine("desc : " + detail.Description);
                        Console.WriteLine("trg name : " + trigger.Key.Name);
                        Console.WriteLine("trg group : " + trigger.Key.Group);
                        Console.WriteLine("trg type : " + trigger.GetType().Name);
                        Console.WriteLine("trg state : " + scheduler.GetTriggerState(trigger.Key));
                        DateTimeOffset? nextFireTime = trigger.GetNextFireTimeUtc();
                        if (nextFireTime.HasValue)
                        {
                            Console.WriteLine("next trigger? : " + nextFireTime.Value.LocalDateTime.ToString());
                        }

                        DateTimeOffset? previousFireTime = trigger.GetPreviousFireTimeUtc();
                        if (previousFireTime.HasValue)
                        {
                            Console.WriteLine("previous trigger? : " + previousFireTime.Value.LocalDateTime.ToString());
                        }
                    }
                }
                Console.WriteLine();
            }
            Console.WriteLine("===== END \n");
        }

        static void ScheduleTask() 
        {
            IScheduler scheduler = null;
            IJobDetail job = null;
            ITrigger trigger = null;

            try
            {
                // construct a scheduler factory
                ISchedulerFactory schedFact = new StdSchedulerFactory();
                // get a scheduler
                scheduler = schedFact.GetScheduler();
                scheduler.Start();

                job = JobBuilder.Create<SimpleJob>()
                    .WithIdentity("myJob", "group1")
                    .Build();
                trigger = TriggerBuilder.Create()
                    .WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
                    .Build();

                scheduler.ScheduleJob(job, trigger);

                GetAllJobs(scheduler);

            }
            catch (Exception e)
            {
                Console.WriteLine(e.InnerException.ToString());
            }
            finally
            {
                GetAllJobs(scheduler);

                TimeSpan spn = TimeSpan.FromSeconds(100L);
                Thread.Sleep(spn);


                scheduler.Shutdown(true);

            }

        }

        static void Main(string[] args)
        {
            ScheduleTask();
            //link to scheduler is lost here
            //how to stop SimpleJob execution?
            //Console.ReadLine();
        }
    }
}
 

순서는 우선 스케줄러를 시작하고

// construct a scheduler factory
ISchedulerFactory schedFact = new StdSchedulerFactory();
// get a scheduler
scheduler = schedFact.GetScheduler();
scheduler.Start();
 

원하는 Job 을 집어 넣고, trigger 를 사용 해서 주기를 설정 해 줍니다.

job = JobBuilder.Create<SimpleJob>()
	.WithIdentity("myJob", "group1")
	.Build();
trigger = TriggerBuilder.Create()
	.WithSimpleSchedule(x => x.WithIntervalInSeconds(10).WithRepeatCount(3))
	.Build();

scheduler.ScheduleJob(job, trigger);
 

이러한 Job 들은 IJob 의 인터페이스를 구현 해야 합니다.

public class SimpleJob : IJob
{
	public void Execute(IJobExecutionContext context)
	{
		Console.WriteLine("hello there");
	}
}
 

이런 Job 들의 리스트는 다음과 같이 스케줄러에서 가져 올 수 있네요

IList<string> jobGroups = scheduler.GetJobGroupNames();
 

이상.

728x90