SpringFramework + Quartz

Programming 2008.04.28 17:54

quartz를 사용하기 위해 필요한 라이브러리

  1. spring.jar - http://www.springframework.org/download
  2. quartz.jar - http://www.opensymphony.com/quartz/download.action

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
    <bean name="exampleJob" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass">
        <value>example.ExampleJob</value>
      </property>
      <property name="jobDataAsMap">
        <map>
          <entry key="timeout"><value>5</value></entry>
        </map>
      </property>
    </bean>
<bean id="exampleBusinessObject" class="example.ExampleBusinessObject"/>
<bean id="methodInvokingJobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    <property name="targetObject"><ref bean="exampleBusinessObject"/></property>
    <property name="targetMethod"><value>doIt</value></property>
    <property name="concurrent"><value>false</value></property>
</bean>
<bean id="simpleTrigger" class="org.springframework.scheduling.quartz.SimpleTriggerBean">
  <property name="jobDetail">
    <!-- see the example of method invoking job above -->  
    <ref bean="methodInvokingJobDetail"/>
  </property>
  <property name="startDelay">
    <!-- 10 seconds -->
    <value>10000</value>
  </property>
  <property name="repeatInterval">
    <!-- repeat every 50 seconds -->
    <value>50000</value>
  </property>
</bean>

<bean id="cronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail">
    <ref bean="exampleJob"/>
  </property>
  <property name="cronExpression">
    <!-- run every morning at 6 AM -->
    <value>10 * * * * ?</value>
  </property>
</bean>

<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
    <list>
      <ref local="cronTrigger"/>
      <ref local="simpleTrigger"/>
    </list>
  </property>
</bean>   

<!--
  <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">

    <property name="quartzProperties">
      <props>
        <prop key="org.quartz.scheduler.instanceName">RiskienhallintaScheduler</prop>
        <prop key="org.quartz.scheduler.instanceId">instance_1</prop>
        <prop key="org.quartz.scheduler.rmi.export">false</prop>
        <prop key="org.quartz.scheduler.rmi.proxy">false</prop>
       
        <prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
        <prop key="org.quartz.threadPool.threadCount">3</prop>
       
        <prop key="org.quartz.jobStore.class">org.quartz.simpl.RAMJobStore</prop>

      </props>
    </property>
    <property name="triggers">
    <list>
      <ref local="cronTrigger"/>
      <ref local="simpleTrigger"/>
    </list>
  </property>
  </bean>
  -->  
</beans>


QuartzJobBean.java

package example;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class ExampleJob extends QuartzJobBean {

  private int timeout;
 
  /**
   * Setter called after the ExampleJob is instantiated
   * with the value from the JobDetailBean (5)
   */
  public void setTimeout(int timeout) {
    this.timeout = timeout;
  }
 
  protected void executeInternal(JobExecutionContext ctx)
  throws JobExecutionException {
      System.out.println("job called. timeout is : "+timeout);
  }
}
  


ExampleBusinessObject.java

package example;

public class ExampleBusinessObject {
     
  // properties and collaborators
 
  public void doIt() {
    System.out.println("doIt method called in ExampleBusinessObject class");
   
  }
}

결과

doIt method called in ExampleBusinessObject class
job called. timeout is : 5

이 글은 스프링노트에서 작성되었습니다.

신고

'Programming' 카테고리의 다른 글

Quartz properties  (0) 2008.05.27
SpringFramework + Quartz  (0) 2008.04.28
wget.java  (0) 2008.03.17
simple java wget  (0) 2008.03.17


티스토리 툴바