核心:Quartz
Scheduler:调度器,所有执行操作由scheduler统一管理
Trigger:设置时间,间隔,次数
JobDetail:任务的实例
Job:任务业务逻辑
依赖包
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
使用方法
页面设置时间之后,将数据发送到后台
Controller层
package com.nec.controller;
import com.nec.bean.Employee;
import com.nec.bean.Msg;
import com.nec.bean.MsgInfo;
import com.nec.service.MsgService;
import com.nec.utlis.MyJob;
import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;
@Controller
public class MsgController {
@Autowired
private MsgService msgService;
@RequestMapping(value = "/msg")
@ResponseBody
public MsgInfo sendmsg(Msg msg , HttpSession session){
MsgInfo msgInfo = new MsgInfo();
//创建定时任务
//创建Scheduler工厂
StdSchedulerFactory factory = new StdSchedulerFactory();
//获取Scheduler
try {
Scheduler scheduler = factory.getScheduler();
//Trigger触发器
Trigger trigger = TriggerBuilder.newTrigger()
.withIdentity("triger1")
.withSchedule(SimpleScheduleBuilder.simpleSchedule())
.startAt(msg.getMsgtime())
.build();
//任务jobDetail
JobDetail jobDetail = JobBuilder.newJob(MyJob.class)
.withIdentity("jobDetail1")
.build();
//传递数据到job,就是MyJob实现的那个job接口
//用户已经登录,session域中已经有了employee
Employee employee = (Employee)session.getAttribute("employee");
//msg表中sendp对应的是employee的eid,就是发件人
msg.setSendp(employee.getEid());
//麻痹的这个mark是干嘛的?
msg.setMark(0);
//将数据存入map集合传递给job
jobDetail.getJobDataMap().put("msg",msg);
//将触发器和任务注册到Scheduler
scheduler.scheduleJob(jobDetail,trigger);
//启动Scheduler
if(!scheduler.isShutdown()){
scheduler.start();
}
} catch (SchedulerException e) {
e.printStackTrace();
}
return msgInfo;
}
}
需要新建一个MyJob类,实现job接口,其中使用jdbc来做数据库的写入
package com.nec.utlis;
import com.nec.bean.Msg;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import java.sql.*;
public class MyJob implements Job {
@Override
public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
Msg msg = (Msg) jobExecutionContext.getMergedJobDataMap().get("msg");
Connection connection = null;
//注册驱动
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/oamanager?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false","root","root");
PreparedStatement statement = connection.prepareStatement(
"insert into `oamanager`.`msg`(`sendp`,`recvp`,mark,`msgcontent`,`msgtime`) values (?,?,?,?,?)");
//?只是一个占位符,赋值
statement.setInt(1,msg.getSendp());
statement.setInt(2,msg.getRecvp());
statement.setInt(3,msg.getMark());
statement.setString(4,msg.getMsgcontent());
statement.setDate(5,new Date(msg.getMsgtime().getTime()));
int i = statement.executeUpdate();
connection.close();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
}