Spring学习笔记(六)
文章目录
Quartz是一个成熟的任务调度系统,Spring对Quartz做了兼容,Spring提供了对Quartz的支持,对Quartz的核心类进行了封装,下面通过一个简单地项目来实现:
1.创建java项目,并导入相应的jar包:
2.创建实体类:
package entity;
public class JobTask {
private String Id;
private String Name;
private String Group;
private String Status;
private String cronExpression;
public String getId() {
return Id;
}
public void setId(String id) {
Id = id;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getGroup() {
return Group;
}
public void setGroup(String group) {
Group = group;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public String getCronExpression() {
return cronExpression;
}
public void setCronExpression(String cronExpression) {
this.cronExpression = cronExpression;
}
}
3.编写任务类:
package service;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import entity.JobTask;
public class PlanJob implements Job {
private RemindService remindService;
public void execute(JobExecutionContext context) throws JobExecutionException {
JobTask job=(JobTask)context.getMergedJobDataMap().get("JobTask");
System.out.println("任务名:"+job.getName());
remindService=new RemindService();
remindService.callClassMeeting();
System.out.println("任务运行完成");
}
}
4.编写服务类:
package service;
public class RemindService {
public void callClassMeeting() {
System.out.println("提醒:");
}
}
5.配置spring配置文件applicationContext:
<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 在SpringIOC容器中注册调度工厂
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean"/> -->
<!-- 配置任务的信息 -->
<bean id="JobTask" class="entity.JobTask">
<property name="Id" value="321"></property>
<property name="Name" value="起床"></property>
<property name="Group" value="第一组"></property>
<property name="Status" value="1"></property>
<property name="cronExpression" value="*/5 * * * * ?"></property>
</bean>
<!-- 配置jobDetail -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.JobDetailFactoryBean">
<!-- 配置计划任务 -->
<property name="jobClass" value="service.PlanJobForSpring"></property>
<!-- 配置jobDataAsMap -->
<property name="jobDataAsMap">
<map>
<entry key="JobTask">
<ref bean="JobTask"/>
</entry>
</map>
</property>
</bean>
<!-- 配置CronTrigger 类型触发器 -->
<bean id="cronTrigger"
class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<!-- 配置Cron表达式,表达式是在id为scheduleJobEntity的cronExpression属性中设置的 -->
<property name="cronExpression" value="#{JobTask.cronExpression}"/>
</bean>
<!--配置调度工厂 -->
<bean id="schedulerFactoryBean"
class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- 配置触发器 -->
<property name="triggers">
<list>
<ref bean="cronTrigger"/>
</list>
</property>
</bean>
</beans>
6.编写一个测试类:
package test;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test1 {
public static void main(String [] agrs) throws SchedulerException, InterruptedException {
//创建spring 的IOC容器对象
ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
Scheduler scheduler=(Scheduler) ctx.getBean("schedulerFactoryBean");
//计划任务在10秒之内有效
scheduler.start();
Thread.sleep(10000);
scheduler.shutdown();
System.out.println("结束");
}
}