Windows下Spring3.x计划任务实现定时备份MySql数据库

来源:互联网 发布:威戈背包知乎 编辑:程序博客网 时间:2024/05/21 21:49

今天在空闲之余查了一下关于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 id="timerAction" class="cn.gov.csrc.report.action.TimerAction"></bean>        <!-- 定义调用对象和调用对象的方法 -->    <bean id="jobtask" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">    <!-- 调用的类 -->    <property name="targetObject">    <ref bean="timerAction"/>    </property>    <!-- 调用类中的方法 -->    <property name="targetMethod">    <value>start</value>    </property>    <!-- 作业不并发调度 -->    <property name="concurrent" value="false"/>    </bean>        <!-- 定义触发时间 -->    <bean id="doTime" class="org.springframework.scheduling.quartz.CronTriggerBean">    <property name="jobDetail">    <ref bean="jobtask"/>    </property>    <!-- cron表达式 -->    <property name="cronExpression">    <!-- 每天晚上11点59分钟59秒执行一次 -->    <!-- <value>0 59 23 * * ?</value> -->    <!-- 每天上午11点04分钟59秒执行一次-->    <value>0 04 11 * * ?</value>    </property>    </bean>        <!-- 总管理类,如果将lazy-init='false'那么容器启动就会执行调度程序 -->    <bean id="startQuertz" lazy-init="false" autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">    <property name="triggers">    <list>    <ref bean="doTime"/>    </list>    </property>    </bean>

3.最后附上quartz的jar包,有需要的朋友可以下载,加到你们的Spring环境中句可以使用了,这个是8.6的:http://pan.baidu.com/s/1dDuvSwp

还有一个quartz时间格式的工具,可以任意改变时间格式:下载地址:http://pan.baidu.com/s/1o6M3PB8

0 0
原创粉丝点击