quartz 정시 작업에서의 응용

최근 프로젝트에서 정해진 시간에 맞추어 장부에 대한 수요를 만났다. 원래는 Timer를 사용하려고 했는데 나중에 수요를 고려하여 더욱 세분화될 수 있다. 예를 들어 최초의 매일 한 쌍에서 시간당 한 쌍으로 바뀌는 등 상대적으로 성숙한 프레임워크quartz를 사용하는 것이 좋다고 생각했다.다음은 자신의 사용 과정 중의 경험을 간단하게 기록하는 것이다.
 
1. 마븐의pom.xml 파일에quartz 패키지 의존 추가
        org.quartz-scheduler    quartz    1.8.0        org.quartz-scheduler    quartz-oracle    1.8.0  
그리고 mvn install, 해당 패키지 다운로드
2.src/main/resouces 아래에quartz를 추가합니다.properties 파일입니다.
프로필의 상세한 설정은 말할 것도 없고, 특히 프로필에 작업에 대한 지속성을 추가했다
지원:
   
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.oracle.OracleDelegate
#org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.HSQLDBDelegate
org.quartz.jobStore.useProperties=false
org.quartz.jobStore.dataSource=XXX
org.quartz.jobStore.tablePrefix=QRTZ_
org.quartz.jobStore.isClustered=true
org.quartz.jobStore.clusterCheckinInterval=15000

3. 스프링 프로필에 작업 bean.
우선quartz가 정해진 임무를 수행하는 방식을 말씀드리겠습니다.quartz가 사용자에게 노출된 역할은 대략 세 가지가 있는데 그것이 바로 Scheduler, Trigger,Task이다.
Task는 바로 실현할 작업입니다. 사용자 정의task는QuartzJobBean 인터페이스를 실현해야 합니다.Trigger는 트리거로서 특정한 트리거 조건을 제정하여task를 트리거한다.Scheduler는 트리거와 작업을 연결하는 스케줄러입니다.물론 Trigger 자체에도 Job 매개변수가 있을 수 있습니다.또한 Scheduler는 런타임 시 task를 동적으로 스케줄링할 수 있습니다.
 
다음은 내가 구성한 스케줄링입니다.
 
<bean id="a" class="org.springframework.scheduling.quartz.JobDetailBean">
 		<property name="jobClass">
			<value>
				com.××
			</value>
		</property>
 		<property name="jobDataAsMap">
 			<map>
 				<entry key="c">
 					<ref bean="C" />
 				</entry>
 			</map>
 		</property>
 	</bean>
  
 	<bean id="b"
		class="org.springframework.scheduling.quartz.SimpleTriggerBean">
		<property name="jobDetail">
			<ref bean="a" />
		</property>
		<property name="startDelay">
			<value>9100</value>
		</property>
		<property name="repeatInterval">
			<value>9100</value>
		</property>
	</bean>
	 
	 <bean  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
	  <property name="configLocation" value="classpath:quartz.properties"/> 
	    <property name="startupDelay" value="30"/>  
		<property name="triggers">
		<list>
		<ref local="b" />
		</list>
		</property>
	</bean>

 
이렇게 하면 임무에 자신의 업무 코드만 쓰면 된다.
그리고 이런 식으로 여러 개의 scheduler를 설정하는 경우quartz에서properties가 스레드 탱크를 설정했습니다
   org.quartz.threadPool.threadCount = 8, 모든 scheduler는 8개의 단독 라인으로 작업을 실행할 수 있습니다.
그러나 현재 프로젝트에서 동기화를 고려하여 매번 이 job를 실행하는 라인만 있기를 바랍니다.그래서 모든 Job은 StatefulJob 인터페이스를 실현하고,
이 인터페이스의 의미는 다음과 같습니다.
     
/**
 * <p>
 * A marker interface for <code>{@link org.quartz.JobDetail}</code> s that
 * wish to have their state maintained between executions.
 * </p>
 * 
 * <p>
 * <code>StatefulJob</code> instances follow slightly different rules from
 * regular <code>Job</code> instances. The key difference is that their
 * associated <code>{@link JobDataMap}</code> is re-persisted after every
 * execution of the job, thus preserving state for the next execution. The
 * other difference is that stateful jobs are not allowed to execute
 * concurrently, which means new triggers that occur before the completion of
 * the <code>execute(xx)</code> method will be delayed.

 
대체로 두 가지 작용만 있다.
    1. Job의 실행 과정에서 JobDataMap의 대상의 상태를 유지하여 그 안의 대상이 매번 실행된 후에 미리 지속되도록 한다.
다음 실행 사용
    2. 여러 개의 라인이 한꺼번에 어떤 job을 실행하는 것을 방지합니다.

좋은 웹페이지 즐겨찾기