標籤:http io 使用 ar art sp on c ef
實現任務類:
package com.vnetoo.nec.base.quartz;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
/**
* 定時發送資訊
*
* @author lingpy
* @since 1.0
*/
@Component
@Lazy(false)
public class SendMessageJob{
private static long time = 0;
@Scheduled(cron="0 0/2 * * * ? ") //兩分鐘一次
public void work(){
time++;
System.out.println("SendMessageJob : send message " + time + ",current :" + System.currentTimeMillis());
}
}
spring.xml 配置
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
">
<task:annotation-driven/><!-- task任務掃描註解 -->
spring.xml可以添加註解掃描的配置
xmlns:context="http://www.springframework.org/schema/context
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<context:component-scan base-package="com.vnetoo.nec" />
主要事項:
在Spring配置和Quartz整合內容時,有兩點需要注意
1、在<Beans>中不能夠設定default-lazy-init="true",否則定時任務不觸發,如果不明確指明default-lazy-init的值,預設是false。可使用@Lazy(false)顯示確定
2、在<Beans>中不能夠設定default-autowire="byName"的屬性,否則後台會報 org.springframework.beans.factory.BeanCreationException錯誤,這樣就不能通過Bean名稱自 動注入,必須通過明確引用注入
quartz執行個體以及主要事項(註解)