달력

3

« 2024/3 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2010. 4. 8. 14:21

CronTrigger (잡 스케쥴링) I.lib()/I.lib(Spring)2010. 4. 8. 14:21

.. .. ..

public void crontrigger() throws Exception{
  Scheduler scheduler;

  scheduler = new StdSchedulerFactory().getScheduler();
  scheduler.start();
  //스케쥴러 스타트
  JobDetail jobdetail = new JobDetail("messageJob",Scheduler.DEFAULT_GROUP,crontrigger_job.class);
  // 절대 org.quartz.Job 을 implements 한 class를 두어야 한다.
  ClassPathXmlApplicationContext context =
                      new ClassPathXmlApplicationContext("/Application/resources/job/NeossJob_7_2.xml");
  Trigger trigger = (Trigger) context.getBean("cronTrigger");
  // Bean의 트리거를 받아온다.
  
  //String cronExpression = "0/5 * * * * ?";
  //Trigger trigger = new CronTrigger("cronTrigger",Scheduler.DEFAULT_GROUP,cronExpression);
  // Bean에서 트리거를 받아오지 않을시 이같은 방식으로 설정해준다.

  scheduler.scheduleJob(jobdetail,trigger);
 }


 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
                                          http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">

 <bean id="job" class="org.springframework.scheduling.quartz.JobDetailBean">
       <property name="jobClass" value="Infrastructure.com.crontrigger_job"/>
       <property name="jobDataAsMap">
             <map>
                  <entry key="message" value="[SM]this is a message from the Spring config file!"/>
             </map>
       </property>
 </bean>
 
 <bean id="trigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
         <property name="jobDetail" ref="job"/>
  <!-- <property name="startDelay" value="1000"/>  시작 지연 1초
         <property name="repeatInterval" value="3000"/> 반복 간격 3초-->
         <property name="jobDataAsMap">
                <map>
                        <entry key="triggerMessage" value="[SM] Trigger message from the Spring config file!"/>
               </map>
        </property>
 </bean>

//아래는 cronTriggerBean이다. 자주 쓰이는 것이다.
<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
        <property name="jobDetail" ref="job"/>
        <property name="cronExpression" value="0/1 * * * * ?"/>
 </bean>
 
 <bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
         <property name="triggers">
          <list>
               <ref local="trigger"/>
         </list>
         </property>
 </bean>

</beans>


두가지 방식의 스케쥴러를 확인할수 있다.
두번째방식의 스케쥴러를 실행시킬때는 main 에서
ApplicationContext ctx = new FileSystemXmlApplicationContext("src/Application/resources/quartz-sample.xml")
만 실행시켜주면 자동으로 cron이 돌게 된다.
.
:
Posted by .07274.