基于quartz的定时任务
更新时间:2023-12-14 05:53:01 阅读量: 教育文库 文档下载
功能概述:
1 首页显示自己添加的定时任务
1.1 点击新增可以添加定时任务
1.2 任务状态分为:运行中、暂停、已失效,运行中的任务可以删除、编
辑、暂停,已失效的任务只可以删除、编辑(重新编辑后任务可以再次运行)
2 任性新增界面
2.1 2.2 2.3 2.4
编写任务标题
编写提醒内容(提醒内容无字数限制) 提醒时间可以选择详细、每日、每周、每月、cron(类似Linux的cron) 向谁提醒可以选择多人
功能实现:
1 定时任务持久化
如果定时任务不进行持久化配置用户设置的定时任务会保存在内存中,应用重启后定时任务就不存在了。 1.1 配置文件
各配置参数请自己百度
org.quartz.scheduler.instanceName = DefaultQuartzScheduler org.quartz.scheduler.rmi.export = false org.quartz.scheduler.rmi.proxy = false
org.quartz.scheduler.wrapJobExecutionInUserTransaction = false org.quartz.threadPool.class: org.quartz.simpl.SimpleThreadPool org.quartz.threadPool.threadCount: 10 org.quartz.threadPool.threadPriority: 5
org.quartz.threadPool.threadsInheritContextClassLoaderOfInitializingThread: false
#org.quartz.jobStore.misfireThreshold = 100000
org.quartz.jobStore.class:org.quartz.impl.jdbcjobstore.JobStoreTX org.quartz.jobStore.driverDelegateClass:org.quartz.impl.jdbcjobstore.StdJDBCDelegate
org.quartz.jobStore.useProperties: ture org.quartz.jobStore.dataSource:myDS org.quartz.jobStore.tablePrefix:QRTZ_ org.quartz.jobStore.isClustered:false #上线时需要修改数据库配置
org.quartz.dataSource.myDS.connectionProvider.class:tool.remind.MyPoolingconnectionProvider
#org.quartz.dataSource.myDS.driver: com.mysql.jdbc.Driver #org.quartz.dataSource.myDS.URL:
jdbc:mysql://localhost:3306/zcglpz1?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
org.quartz.dataSource.myDS.driver=net.sf.log4jdbc.DriverSpy org.quartz.dataSource.myDS.URL=jdbc:log4jdbc:mysql://localhost:3306/zcglpz1?characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull
org.quartz.dataSource.myDS.user:root org.quartz.dataSource.myDS.password:123654 org.quartz.dataSource.myDS.maxConnections:5
1.2 编写MyPoolingconnectionProvider.java类
package tool.remind; import java.sql.Connection; import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource; import org.quartz.SchedulerException; import org.quartz.utils.ConnectionProvider;
public class MyPoolingconnectionProvider implements ConnectionProvider { private BasicDataSource datasource; private String dbDriver; private String URL; private String dbUser; private String dbPassword; private int maxConnections;
private int maxStatementsPerConnection; private String dbValidationQuery; private boolean validateOnCheckout; private int idleValidationSeconds; private int maxIdleSeconds;
public MyPoolingconnectionProvider() {
}
private void initialize(String dbDriver, String URL, String dbUser,String dbPassword, int maxConnections, int maxStatementsPerConnection, String
dbValidationQuery,boolean validateOnCheckout,int idleValidationSeconds,int maxIdleSeconds) throws SQLException, SchedulerException { if (URL == null) {
throw new SQLException(
\ }
if (dbDriver == null) { throw new SQLException(
\ \ }
if (maxConnections < 0) { throw new SQLException(
\ \ }
datasource = new BasicDataSource(); datasource.setDriverClassName(dbDriver);
datasource.setUrl(URL);
datasource.setUsername(dbUser);
datasource.setPassword(dbPassword); datasource.setMaxActive(maxConnections); datasource.setMinIdle(1);
datasource.setMaxWait(maxIdleSeconds);
datasource.setMaxOpenPreparedStatements(maxStatementsPerConnection);
if (dbValidationQuery != null) {
datasource.setValidationQuery(dbValidationQuery); if(!validateOnCheckout)
datasource.setTestOnBorrow(true); else
datasource.setTestOnReturn(true);
datasource.setTimeBetweenEvictionRunsMillis(idleValidationSeconds); } }
public Connection getConnection() throws SQLException { return datasource.getConnection(); }
public void shutdown() throws SQLException { datasource.close(); }
public void initialize() throws SQLException {
// do nothing, already initialized during constructor call newInstance try {
this.initialize(this.dbDriver, this.URL, this.dbUser, this.dbPassword,
maxConnections, this.maxStatementsPerConnection, this.dbValidationQuery, validateOnCheckout, this.idleValidationSeconds, maxIdleSeconds); } catch (SchedulerException e) {
// TODO Auto-generated catch block throw new SQLException(e); } }
public void setDriver(String dbDriver) { this.dbDriver = dbDriver; }
public void setURL(String URL) { this.URL = URL; }
public String getURL() { return URL; }
public void setUser(String dbUser) { this.dbUser = dbUser; }
public void setPassword(String dbPassword) { this.dbPassword = dbPassword; }
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections; }
public void setMaxCachedStatementsPerConnection(int maxStatementsPerConnection) {
this.maxStatementsPerConnection = maxStatementsPerConnection; }
public void setMaxIdleSeconds(int maxIdleSeconds) { this.maxIdleSeconds = maxIdleSeconds; }
public void setValidationQuery(String dbValidationQuery) { this.dbValidationQuery = dbValidationQuery; }
public void setIdleConnectionValidationSeconds(int idleValidationSeconds) { this.idleValidationSeconds = idleValidationSeconds; }
public void setValidateOnCheckout(boolean validateOnCheckout) { this.validateOnCheckout = validateOnCheckout; }
public void setDiscardIdleConnectionsSeconds(int maxIdleSeconds) { this.maxIdleSeconds = maxIdleSeconds; }
public void setDatasource(BasicDataSource datasource) { this.datasource = datasource; }
protected BasicDataSource getDataSource() { return datasource; } }
1.3 工具类对定时任务进行操作
主要是添加、修改、删除定时任务
package tool.remind;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import static org.quartz.CronScheduleBuilder.cronSchedule; import org.quartz.CronTrigger; import org.quartz.Job; import org.quartz.JobDetail; import org.quartz.JobKey; import org.quartz.Scheduler;
import org.quartz.SchedulerException; import org.quartz.SchedulerFactory; import org.quartz.TriggerKey;
import org.quartz.impl.StdSchedulerFactory; public class QuartzManager {
private static SchedulerFactory sFactory = new StdSchedulerFactory(); private static String JOB_GROUP_NAME = \; private static String TRIGGER_GROUP_NAME = \;
public static boolean addJob(String jobName,Class extends Job>
try{
Scheduler scheduler = sFactory.getScheduler(); JobDetail job = newJob(cls).withIdentity(jobName,
cls,String cron){
JOB_GROUP_NAME).build();
}
}
job.getJobDataMap().put(\, jobName);
CronTrigger trigger = newTrigger().withIdentity(jobName, scheduler.scheduleJob(job, trigger); scheduler.start(); return true; return true;
TRIGGER_GROUP_NAME).withSchedule(cronSchedule(cron)).build();
}catch(Exception e){
public static void modifyJobTime(String jobName,String cron){ }
public static void removeJob(String jobName){
try{
Scheduler scheduler = sFactory.getScheduler(); TriggerKey triggerKey = new TriggerKey(jobName, scheduler.pauseTrigger(triggerKey); scheduler.unscheduleJob(triggerKey);
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); scheduler.deleteJob(jobKey); e.printStackTrace(); try{ }
Scheduler scheduler = sFactory.getScheduler(); TriggerKey triggerKey = new TriggerKey(jobName, CronTrigger trigger = if(trigger==null){ }
String oldCron = trigger.getCronExpression(); if(!oldCron.equalsIgnoreCase(cron)){ }
e.printStackTrace();
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); JobDetail jobDetail = scheduler.getJobDetail(jobKey); Class extends Job> jobClass = jobDetail.getJobClass(); removeJob(jobName);
addJob(jobName,jobClass,cron); return;
TRIGGER_GROUP_NAME);
(CronTrigger)scheduler.getTrigger(triggerKey);
}catch(Exception e){
TRIGGER_GROUP_NAME);
}catch(Exception e){
}
}
}
public static boolean resumeJob(String jobName){ }
public static boolean pauseJob(String jobName){ }
Scheduler scheduler; try { }
scheduler = sFactory.getScheduler();
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); scheduler.pauseJob(jobKey); return true;
e.printStackTrace(); return false; Scheduler scheduler; try { }
scheduler = sFactory.getScheduler();
JobKey jobKey = new JobKey(jobName, JOB_GROUP_NAME); scheduler.resumeJob(jobKey); return true;
e.printStackTrace(); return false;
} catch (SchedulerException e) {
} catch (SchedulerException e) {
1.4 通过网关发送短信
到达设置的时间向用户发送短信,使用公司内部的短信网关发送短信,
package tool.remind;
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.URL;
import java.net.URLConnection; import java.net.URLEncoder; public class SendSms {
public static void sendSms(String param,String msg) { BufferedReader in = null;
try {
String url = \; System.err.println(url); URL realUrl = new URL(url); // 打开和URL之间的连接
URLConnection connection = realUrl.openConnection(); // 设置通用的请求属性
connection.setRequestProperty(\, \);
connection.setRequestProperty(\, \); connection.setRequestProperty(\, \);
connection.setRequestProperty(\,\(compatible; MSIE 6.0; Windows NT 5.1;SV1)\); // 建立实际的连接
connection.connect();
connection.getHeaderFields();
//定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader( connection.getInputStream())); } catch (Exception e) {
System.out.println(\发送GET请求出现异常!\ + e); e.printStackTrace(); } finally { try {
if (in != null) { in.close(); }
} catch (Exception e2) { e2.printStackTrace(); } } } }
基于struts1.3的Action
public class RemindAction extends MappingDispatchAction{
//新增定时任务初始化
public ActionForward initAddJobAction(ActionMapping mapping,
ActionForm form,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
ServletContext application = RequestDispatcher rd =
JobForm jobForm = new JobForm(); jobForm.reset();
request.setAttribute(\, jobForm); rd.forward(request, response);
request.getSession().getServletContext();
application.getRequestDispatcher(\);
}
return null;
//新增定时任务
public ActionForward addJobAction(ActionMapping mapping,ActionForm
form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
}
//删除定时任务
public ActionForward deleteJobAction(ActionMapping
String realName =
JobForm job = (JobForm)form; job.setCreateName(realName); String cron = StringToCron(job); if(cron!=null&cron.length()>0){ }
return null;
String id=\+UUID.randomUUID(); job.setId(id);
RemindDao dao = new RemindDao(); boolean isInsert = dao.addJob(job); if(isInsert){
boolean result = QuartzManager.addJob(id, RemindJob.class, }
if(result){ }
response.setCharacterEncoding(\); response.getWriter().write(\); response.setCharacterEncoding(\); response.getWriter().write(\);
(String)request.getSession().getAttribute(\);
cron);
}else{
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{
JobForm job = (JobForm)form; String id = job.getId(); if(id!=null){ }
RemindDao dao = new RemindDao();
boolean deleteResult = dao.deleteJobById(id); if(deleteResult){ }
QuartzManager.removeJob(id);
response.sendRedirect(\);
}
return null;
//修改定时任务初始化
public ActionForward initUpdateJobAction(ActionMapping
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
}
//修改定时任务
public ActionForward updateJobAction(ActionMapping
JobForm jobForm = (JobForm)form; RemindDao dao = new RemindDao();
JobPo job= dao.getJobById(jobForm.getId()); ServletContext application = RequestDispatcher rd = if(job!=null){ }
return null;
request.setAttribute(\, job); rd.forward(request, response);
request.getSession().getServletContext();
application.getRequestDispatcher(\);
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws IOException{
}
//获取定时任务列表 public ActionForward jobListAction(ActionMapping mapping,ActionForm
JobForm jobForm = (JobForm)form; RemindDao dao = new RemindDao();
boolean updateResult = dao.updateJob(jobForm); boolean updateQuartz = if(!updateQuartz){ }
if(updateResult){ }
return null;
response.sendRedirect(\); String cron = StringToCron(jobForm);
QuartzManager.addJob(jobForm.getId(), RemindJob.class,
dao.getJobFromQuatzById(jobForm.getId());
cron);
form,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
ServletContext application =
request.getSession().getServletContext();
}
String realName = RequestDispatcher rd =
List
request.setAttribute(\, jobList); rd.forward(request, response); return null;
(String)request.getSession().getAttribute(\);
application.getRequestDispatcher(\);
//修改定时任务初始化
public ActionForward initModifyJobAction(ActionMapping
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
}
//重启定时任务
public ActionForward resumeJobAction(ActionMapping
JobForm jobForm = (JobForm)form; JobForm newForm = new JobForm(); String id = jobForm.getId(); RemindDao dao = new RemindDao(); JobPo jobPo =null; if(id!=null){ }
if(jobPo!=null){ }
return null;
newForm.setContext(jobPo.getContext()); newForm.setCreateName(jobPo.getCreateName()); newForm.setCron(jobPo.getCron()); newForm.setId(id);
newForm.setMode(jobPo.getMode());
newForm.setRemindTo(jobPo.getRemindTo()); //newForm.setState(jobPo.getState()); ServletContext application = RequestDispatcher rd =
request.setAttribute(\, newForm); rd.forward(request, response); jobPo=dao.getJobById(id);
request.getSession().getServletContext();
application.getRequestDispatcher(\);
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse
response) throws ServletException, IOException{
}
//暂停定时任务
public ActionForward pauseJobAction(ActionMapping
JobForm jobForm = (JobForm)form; String id = jobForm.getId(); if(id!=null){ }
return null;
boolean result = QuartzManager.resumeJob(id); if(result){ }
response.sendRedirect(\);
mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
}
//语言转换成cron
public String StringToCron(JobForm form){
String dateMode = form.getDateMode(); String cron=\;
if(dateMode.equals(\详细\)){ cron=\+form.getMinutes()+\+form.getHours()+\
}else if(dateMode.equals(\每日\)){ cron=\+form.getMinutes()+\+form.getHours()+\; }else if(dateMode.equals(\每周\)){ cron=\+form.getMinutes()+\+form.getHours()+\}else if(dateMode.equals(\每月\)){ cron=\+form.getMinutes()+\+form.getHours()+\}else if(dateMode.equals(\)){ }
return cron;
cron=form.getCron();
JobForm jobForm = (JobForm)form; String id = jobForm.getId(); if(id!=null){ }
return null;
boolean result = QuartzManager.pauseJob(id); if(result){ }
response.sendRedirect(\);
\+form.getDay()+\+form.getMonth()+\+form.getYear();
\+form.getWeek()+\;
\+form.getDay()+\;
}
public List
for(int i=0;i JobPo job = list.get(i); switch (job.getWeek()){ case 1: } String dateMode = job.getDateMode(); if(dateMode.equals(\详细\)){ job.setDateStr(job.getDateMode()+\+job.getYear()+\年job.setWeekStr(\星期一\); break; job.setWeekStr(\星期二\); break; job.setWeekStr(\星期三\); break; job.setWeekStr(\星期四\); break; job.setWeekStr(\星期五\); break; job.setWeekStr(\星期六\); break; job.setWeekStr(\星期天\); break; case 2: case 3: case 4: case 5: case 6: case 7: \+job.getMonth()+\月\+job.getDay()+\日 \+job.getHours()+\+job.getMinutes()); }else if(dateMode.equals(\每日\)){ job.setDateStr(job.getDateMode()+\ }else if(dateMode.equals(\每周\)){ job.setDateStr(job.getDateMode()+\+job.getWeekStr()+\}else if(dateMode.equals(\每月\)){ \+job.getHours()+\+job.getMinutes()); \+job.getHours()+\+job.getMinutes()); job.setDateStr(job.getDateMode()+\+job.getDay()+\日 \+job.getHours()+\+job.getMinutes()); }else if(dateMode.equals(\)){ } job.setDateStr(job.getDateMode()+\+job.getCron()); } } String state = job.getState(); if(state==null){ job.setState(\已失效\); }else if(state.equals(\)){ job.setState(\运行中\); }else if(state.equals(\)){ job.setState(\暂停\); }else if(state.equals(\)){ job.setState(\已失效\); } return list; 返回json数据用于制作树 public class RemindToAction extends MappingDispatchAction{ public ActionForward getRemindToAction(ActionMapping mapping, } HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { ActionForm form, Gson gson = new Gson(); List List for (int i = 0; i < departments.size(); i++) { department=departments.get(i); int personid=addJson(jsons, department); for(int j=0;j } if(numbers.get(j).getGroup().equals(department)){ 织分1级 addJson(jsons,numbers.get(j).getName(),personid); } String jsonString = gson.toJson(jsons); response.setCharacterEncoding(\); response.getWriter().write(jsonString); return null; //新建一个根节点,并且加入到原来的list中。返回新生成根目录的id public int addJson(List //新建一个节点,并且加入到原来的list中,其中pid为指定的pid public void addJson(List public void addJson(List int i = json.size()+1; WhryTree treeNode = new WhryTree(); treeNode.setId(i); treeNode.setpId(pid); treeNode.setName(rootName); treeNode.setTel(tel); json.add(treeNode); int i = json.size()+1; WhryTree treeNode = new WhryTree(); treeNode.setId(i); treeNode.setpId(pid); treeNode.setName(rootName); json.add(treeNode); int i = json.size()+1; WhryTree treeNode = new WhryTree(); treeNode.setId(i); treeNode.setpId(0); treeNode.setName(rootName); json.add(treeNode); return i; tel,int pid){ 数据操作 public class RemindDao { private static ResultSet rs=null; public List +\ +\as state FROM job List job.id,job.title,job.createName,job.cron,job.mode,job.remindTo\ .minutes\ left join qrtz_job_details on qrtz_job_details.JOB_NAME=job.id left join qrtz_triggers on job.id =qrtz_triggers.TRIGGER_NAME where createName='\+createName+\; } public JobPo getJobById(String id){ JobPo job = new JobPo(); String sql = \ Connection conn = DBConnection.initConnection(); try{ } return jobs; Statement st = conn.createStatement(); if(st!=null&sql!=null){ } while(rs.next()){ } e.printStackTrace(); DBConnection.closeConnection(conn); JobPo job =new JobPo(); job.setId(rs.getString(\)); job.setTitle(rs.getString(\)); job.setCreateName(rs.getString(\)); job.setContext(rs.getString(\)); job.setMode(rs.getInt(\)); job.setRemindTo(rs.getString(\)); job.setState(rs.getString(\)); job.setDateMode(rs.getString(\)); job.setWeek(rs.getInt(\)); job.setYear(rs.getInt(\)); job.setMonth(rs.getInt(\)); job.setDay(rs.getInt(\)); job.setHours(rs.getInt(\)); job.setMinutes(rs.getInt(\)); jobs.add(job); rs = st.executeQuery(sql); }catch(Exception e){ }finally{ job.id,job.title,job.createName,job.cron,job.mode,job.remindTo,job.context,qrtz_triggers.TRIGGER_STATE as state,job.dateMode,job.week,job.year,job.month,job.day,job.hours,job.minutes FROM job left join qrtz_job_details on qrtz_job_details.JOB_NAME=job.id left join qrtz_triggers on job.id =qrtz_triggers.TRIGGER_NAME where\+ \+id+\; Connection conn = DBConnection.initConnection(); try{ } } Statement st = conn.createStatement(); if(st!=null&sql!=null){ } while(rs.next()){ } e.printStackTrace(); DBConnection.closeConnection(conn); job.setId(rs.getString(\)); job.setTitle(rs.getString(\)); job.setCreateName(rs.getString(\)); job.setContext(rs.getString(\)); job.setMode(rs.getInt(\)); job.setRemindTo(rs.getString(\)); job.setState(rs.getString(\)); job.setCron(rs.getString(\)); job.setDateMode(rs.getString(\)); job.setWeek(rs.getInt(\)); job.setYear(rs.getInt(\)); job.setMonth(rs.getInt(\)); job.setDay(rs.getInt(\)); job.setHours(rs.getInt(\)); job.setMinutes(rs.getInt(\)); rs = st.executeQuery(sql); }catch(Exception e){ }finally{ return job; public boolean deleteJobById(String id){ String sql = \+id +\; boolean result = false; Connection conn = DBConnection.initConnection(); if(id==null|id==\){ } try{ Statement st = conn.createStatement(); if(st!=null&sql!=null){ } e.printStackTrace(); st.executeUpdate(sql); result = true; return result; }catch(Exception e){ }finally{ } } DBConnection.closeConnection(conn); return result; public boolean updateJob(JobForm job){ boolean result = false; String sql = null; String quartzSql = null; if(job.getId()!=null&job.getId()!=\){ sql = \+job.getId()+\ +\+job.getTitle()+\ +\+job.getCreateName()+\ +\+job.getContext()+\ +\+job.getMode()+\ +\+job.getRemindTo()+\ +\+job.getDateMode()+\ +\+job.getWeek()+\ +\+job.getYear()+\ +\+job.getMonth()+\ +\+job.getDay()+\ +\+job.getHours()+\ +\+job.getMinutes()+\ +\+job.getCron()+\+job.getId()+\; quartzSql = \ CRON_EXPRESSION='\+job.getCron()+\TRIGGER_NAME='\+job.getId()+\; }else{ } Connection conn = DBConnection.initConnection(); try{ } return result; Statement st = conn.createStatement(); if(st!=null&sql!=null){ } e.printStackTrace(); result = false; DBConnection.closeConnection(conn); st.executeUpdate(sql); st.executeUpdate(quartzSql); result = true; return result; }catch(Exception e){ }finally{ } public boolean addJob(JobForm job){ boolean result = false; String sql = null; if(job.getId()!=null&job.getId()!=\){ sql = \ job(id,title,createName,cron,mode,remindTo,context,dateMode,week,year,month,\+\ + } public String getRemindToNumber(String remindTo){ } Connection conn = DBConnection.initConnection(); try{ } return result; Statement st = conn.createStatement(); if(st!=null&sql!=null){ } e.printStackTrace(); result = false; DBConnection.closeConnection(conn); st.executeUpdate(sql); result = true; \ +job.getId()+\ +job.getTitle()+\ +job.getCreateName()+\ +job.getCron()+\ +job.getMode()+\ +job.getRemindTo()+\ +job.getContext()+\ +job.getDateMode()+\ +job.getWeek()+\ +job.getYear()+\ +job.getMonth()+\ +job.getDay()+\ +job.getHours()+\ +job.getMinutes() +\; }else{ return result; }catch(Exception e){ }finally{ String result=\; String[] remindToArray =remindTo.split(\); ResultSet resultSet=null; String str =\; for(int i=0;i str=str+\;//加''是为了和for循环最后一个,进行配对 String sql = \whry_tel from cs_whry where whry_name in\+str; Connection conn = DBConnection.initConnection(); try{ Statement st = conn.createStatement(); resultSet = st.executeQuery(sql); while(resultSet.next()){ } result = result.substring(0, result.length()-1);//去掉最后的 result+=resultSet.getString(\)+\; str+=\+remindToArray[i]+\; 一个, }catch(Exception e){ } public boolean getJobFromQuatzById(String id){ } boolean reault=false; String sql = \ResultSet resultSet=null; Connection conn = DBConnection.initConnection(); try{ } return reault; Statement st = conn.createStatement(); resultSet = st.executeQuery(sql); while(resultSet.next()){ } e.printStackTrace(); DBConnection.closeConnection(conn); reault=true; } return result; e.printStackTrace(); DBConnection.closeConnection(conn); }finally{ JOB_NAME='\+id+\; }catch(Exception e){ }finally{ } 数据库: quratz自带数据库 JOB表用于存放定时任务的提醒内容,提醒时间,向谁提醒等内容 drop table if exists job; create table job(`id` varchar(100) not null,`title` varchar(100), `createName` varchar(45) not null ,`cron` varchar(15) default null, `mode` int(3) default null,`remindTo` text default null, `context` text default null,`dateMode` varchar(15),`week` varchar(15),`year` int(4),`month` int(2),`day` int(2),`hours` int(2), `minutes` int(2) )engine=MyISAM DEFAULT CHARSET=latin1; 执行定时任务 public class RemindJob implements Job{ } public RemindJob(){ } @Override public void execute(JobExecutionContext context) throws } JobDataMap data = context.getJobDetail().getJobDataMap(); String id = data.getString(\); RemindDao dao = new RemindDao(); JobPo job = dao.getJobById(id); //int mode = job.getMode(); String remindTo = job.getRemindTo(); String remindToNumber = dao.getRemindToNumber(remindTo); String msg = job.getContext()+job.getCreateName(); //发送短信 SendSms.sendSms(remindToNumber, msg); JobExecutionException { JSP使用了struts的标签和boostrap的样式 短信提醒列表 <%@ page language=\ import=\ pageEncoding=\%> <%@ taglib uri=\ prefix=\%> <%@ taglib uri=\ prefix=\%> <%@ taglib uri=\ prefix=\%> <%@ taglib uri=\ prefix=\%> <% String path = request.getContextPath(); String basePath = request.getScheme()+\+request.getServerName()+\+request.getServerPort()+path+\; %> 年 value=\>10 月 日 时 分 类似Linux中的cron,例如:0 20 10 20 10 ? 2015为2015年10月20日10:20 执行一次 向谁提醒 onclick=\ value=\取消\> height: auto;text-align: center;margin:auto;border-radius:0px\>