程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> spring框架使用任務調度quartz的例子-Job and Trigger篇

spring框架使用任務調度quartz的例子-Job and Trigger篇

編輯:關於JAVA

MainJob.java

1package jobs;
2
3import org.apache.log4j.Logger;
4import org.quartz.JobExecutionContext;
5import org.quartz.JobExecutionException;
6import org.springframework.scheduling.quartz.QuartzJobBean;
7
8public class MainJob extends QuartzJobBean {
9  private Logger logger = Logger.getLogger(getClass());
10  @Override
11  protected void executeInternal(JobExecutionContext context)
12      throws JobExecutionException {
13    // TODO Auto-generated method stub
14    logger.debug("Just say hi.");
15  }
16
17}
18

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jee="http://www.springframework.org/schema/jee"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.0.xsd">
  <!-- 任務調度對象 -->
  <bean id="mainJob"
    class="org.springframework.scheduling.quartz.JobDetailBean">
    <!-- 運行的類 -->
    <property name="jobClass">
      <value>jobs.MainJob</value>
    </property>
    <!-- 需要用到的對象 -->
    <property name="jobDataAsMap">
      <map>
        <entry key="data">
          <value>data</value>
        </entry>
      </map>
    </property>
  </bean>
  <!-- 簡單的觸發器 -->
  <bean id="mainTrigger"
    class="org.springframework.scheduling.quartz.SimpleTriggerBean">
    <property name="jobDetail">
      <!-- 上面創建的任務調度對象 -->
      <ref bean="mainJob" />
    </property>
    <!-- 啟動60秒後執行任務調度的excute方法 -->
    <property name="startDelay">
      <value>6000</value>
    </property>
    <!-- 運行次數 -->
    <property name="repeatCount">
      <value>0</value>
    </property>
    <!-- 隔一個小時運行一次(貌似多余,不寫會報錯) -->
    <property name="repeatInterval">
      <value>3600000</value>
    </property>
  </bean>
  <!-- 任務調度工廠類 -->
  <bean
    class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    <!-- 這一部分的配置不用管 -->
    <property name="quartzProperties">
      <props>
        <prop key="org.quartz.threadPool.class">
          org.quartz.simpl.SimpleThreadPool
        </prop>
        <prop key="org.quartz.threadPool.threadCount">10</prop>
        <prop key="org.quartz.threadPool.threadPriority">
          5
        </prop>
        <prop
          key="org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread">
          true
        </prop>
      </props>
    </property>
    <!-- 觸發器,可以放一大堆觸發器 -->
    <property name="triggers">
      <list>
        <!-- 在這裡加 -->
         <ref bean="mainTrigger"/>
      </list>
    </property>
  </bean>
</beans>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved