深圳建一个网站要多少钱,廊坊短视频优化,单页竞价网站,建筑公司网站 新闻今天在空闲之余查了一下关于MySql数据库备份的方案#xff0c;最后结合自己的项目情况写了一个关于Spring计划任务的例子#xff0c;目前我这个版本是在Windwos下测试成功#xff0c;希望对大家有所帮助#xff0c;不足之处还请大家多多包含#xff0c;有什么建议尽管提出…今天在空闲之余查了一下关于MySql数据库备份的方案最后结合自己的项目情况写了一个关于Spring计划任务的例子目前我这个版本是在Windwos下测试成功希望对大家有所帮助不足之处还请大家多多包含有什么建议尽管提出我会第一时间回复大家谢谢
1.首先第一步要搭建Spring3.x的环境这里就不多说直接上代码 package cn.gov.csrc.report.action;import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;import javax.annotation.Resource;import org.apache.struts2.convention.annotation.Action;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.annotation.Scope;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Controller;import cn.gov.csrc.report.service.CaseService;Controller()
Scope(prototype)
Action(TimerAction)
public class TimerAction extends QuartzJobBean {private int timeout;private static int i 0;private CaseService caseService;public TimerAction() {}/**调度工厂实例化后经过timeout时间开始执行调度*/public void setTimeout(int timeout) {this.timeout timeout;}Resourcepublic void setCaseService(CaseService caseService) {this.caseService caseService;}Overrideprotected void executeInternal(JobExecutionContext context)throws JobExecutionException {System.out.println(定时任务执行中......);JobDataMap jobDataMap context.getTrigger().getJobDataMap();System.out.println(jobDataMap-------);}public void start() {//备份mysql数据库Runtime runtime Runtime.getRuntime();System.out.println(备份数据库任务开始了......);String cmd mysqldump -h localhost -uroot -proot springdb e:/springdb.sql;//一定要加-h localhost(或是服务器IP地址)try {Process process runtime.exec(cmd /c cmd);InputStreamReader inputStreamReader new InputStreamReader(process.getErrorStream());LineNumberReader lineNumberReader new LineNumberReader(inputStreamReader);String line;while((line lineNumberReader.readLine()) ! null){System.out.println(line----------------);}System.out.println(备份成功);} catch (IOException e) {System.out.println(备份失败);e.printStackTrace();}System.out.println(备份数据库任务结束了......);}}2.配置计划任何的配置文件这里是使用的是quartz插件实现计划任务 !-- 任务计划 --!-- 要调用的工作 --bean idtimerAction classcn.gov.csrc.report.action.TimerAction/bean!-- 定义调用对象和调用对象的方法 --bean idjobtask classorg.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean!-- 调用的类 --property nametargetObjectref beantimerAction//property!-- 调用类中的方法 --property nametargetMethodvaluestart/value/property!-- 作业不并发调度 --property nameconcurrent valuefalse//bean!-- 定义触发时间 --bean iddoTime classorg.springframework.scheduling.quartz.CronTriggerBeanproperty namejobDetailref beanjobtask//property!-- cron表达式 --property namecronExpression!-- 每天晚上11点59分钟59秒执行一次 --!-- value0 59 23 * * ?/value --!-- 每天上午11点04分钟59秒执行一次--value0 04 11 * * ?/value/property/bean!-- 总管理类,如果将lazy-initfalse那么容器启动就会执行调度程序 --bean idstartQuertz lazy-initfalse autowireno classorg.springframework.scheduling.quartz.SchedulerFactoryBeanproperty nametriggerslistref beandoTime//list/property/bean 3.最后附上quartz的jar包有需要的朋友可以下载加到你们的Spring环境中句可以使用了这个是8.6的http://pan.baidu.com/s/1dDuvSwp 还有一个quartz时间格式的工具可以任意改变时间格式下载地址http://pan.baidu.com/s/1o6M3PB8