程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Spring+Quartz定時任務

Spring+Quartz定時任務

編輯:關於JAVA

最近研究站內搜索,因為要定時的更新索引庫,看了看Spring+Quartz定時任務,用它完成,定時創建索引的任務!!

給大家分享一下helloworld的簡單例子,大家可以根據實際情況變化使用

業務方法類

Java代碼

package com.task;

/**
  * 業務方法
  *
  */
public class TestJob {
  public void execute() {
  try {
   System.out.println("我的業務方法被調用了---------!");
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  }
}

配置文件beans.xml

Java代碼

<?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:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <!-- 普通的業務Bean -->
  <bean id="testJob" class="com.task.TestJob" />
  <!-- 觸發器 -->
  <bean
  class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
  <property name="triggers">
   <list>
   <ref bean="testTrigger" />
   </list>
  </property>
  <property name="autoStartup" value="true" />
  </bean>
  <bean id="testTrigger"
  class="org.springframework.scheduling.quartz.CronTriggerBean">
  <property name="jobDetail" ref="testJobDetail" />
  <!-- 每隔1秒鐘觸發一次 -->
  <property name="cronExpression" value="*/1 * * * * ?" />
  </bean>
  <!-- 計劃 -->
  <bean id="testJobDetail"
  class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
  <property name="targetObject" ref="testJob" />
  <property name="targetMethod" value="execute" />
  <!-- 是否允許任務並發執行。當值為false時,表示必須等到前一個線程處理完畢後才再啟一個新的線程 -->
  <property name="concurrent" value="false" />
  </bean>
</beans>

測試方法

Java代碼

package com.task;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
  * 測試類
  *
  */

public class Test {
  public static void main(String[] args) {
  try {
   AbstractApplicationContext context = new ClassPathXmlApplicationContext("com/task/beans.xml");
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println("Main方法執行開始了! 定時器伴隨著Spring的初始化執行了。。。");
  System.out.println("Main方法執行結束了!");
  }
}

我在運行調試的時候遇到了一些錯誤,比如:

Java代碼

Error creating bean with name 'testJobDetail' defined in class path resource [com/task/beans.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: org/apache/commons/collections/SetUtils

這個錯誤是因為工程中缺少必要的commons-collections-3.2.1.jar包引起的,另外必須的jar包還有quartz-1.6.4.jar,spring.jar,commons-logging.jar

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved