@Scheduled適用與監聽任務較少的,而Quartz適合較多的,為確保可伸縮性,Quartz采用了基於多線程的架構。啟動時,框架初始化一套worker線程,這套線程被調度器用來執行預定的作業。這就是Quartz怎樣能並發運行多個作業的原理。Quartz依賴一套松耦合的線程池管理部件來管理線程環境。
定時任務調度
一:基於spring 自帶的注解調度
1 首先引入
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
2 然后進行配置文件配置
<?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:task="http://www.springframework.org/schema/task" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--spring 自帶的定時器--> <task:annotation-driven/> <context:annotation-config/> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <context:component-scan base-package="com.food.wodo.springquartz"/><!-- 下面XzcfYjTask類在的包名 -->
</beans>
然后在spring 中引入
3基於注解任務類編寫 要有3個注解@Component @EnableSceduling @Scheduling cron 為設置時間, 0 為每 * 任意 ? 未指定
import com.food.wodo.dao.XzcfYjDao;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import java.text.SimpleDateFormat; import java.util.*; @Component @EnableScheduling public class XzcfYjTask { @Autowired private XzcfYjDao xzcfYjDao; @Scheduled(cron = "0 0/5 * * * ?") public void sendMessage(){ } }
二:quartz 技術的定時調度任務
1 首先引入
<!--定時調度quartz技術--> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>quartz-jobs</artifactId> <version>2.2.2</version> </dependency>
日志包
<!--log4j && slf4j--> 90 <dependency> 91 <groupId>org.slf4j</groupId> 92 <artifactId>slf4j-api</artifactId> 93 <version>${slf4j.version}</version> 94 </dependency> 95 <dependency> 96 <groupId>org.slf4j</groupId> 97 <artifactId>slf4j-log4j12</artifactId> 98 <version>${slf4j.version}</version> 99 </dependency> 100 <dependency> 101 <groupId>org.slf4j</groupId> 102 <artifactId>jcl-over-slf4j</artifactId> 103 <version>${slf4j.version}</version> 104 <scope>runtime</scope> 105 </dependency>
2 然后進行配置文件配置
<?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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--掃描quartz類--> <context:component-scan base-package="com.food.*.quartz"/> <bean id="SpjyxkzdqYjJob" class="org.springframework.scheduling.quartz.JobDetailFactoryBean"> <property name="jobClass" value="com.food.wodo.quartz.SpjyxkzdqYjJob"/> <property name="jobDataAsMap"> <map> <entry key="timeout" value="5"/> </map> </property> </bean> <!-- ======================== 調度觸發器 ======================== --> <bean id="cronTriggerBean" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean"> <property name="jobDetail" ref="SpjyxkzdqYjJob"></property> <property name="cronExpression" value="0 0/5 * * * ?"></property> </bean> <!-- ======================== 調度工廠 ======================== --> <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean"> <property name="jobFactory" ref="jobFactory" /> <property name="triggers"> <list> <ref bean="cronTriggerBean" /> </list> </property> </bean> </beans>
3 類的創建
<!-- ======================== 調度工廠 ======================== -->
package com.food.wodo.quartz;
import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.stereotype.Service;
@Service("jobFactory")
public class JobFactory extends AdaptableJobFactory {
@Autowired
private AutowireCapableBeanFactory autowireCapableBeanFactory;
@Override
protected Object createJobInstance(TriggerFiredBundle bundle) throws Exception {
Object jobInstance= super.createJobInstance(bundle);
autowireCapableBeanFactory.autowireBean(jobInstance);
return jobInstance;
}
}
quartz類-
package com.food.wodo.quartz;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.util.Date;
public class SpjyxkzdqYjJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Date date=new Date();
System.out.println("定時調度任務"+date);
}
}