Windows下Spring3.x计划任务实现定时备份MySql数据库
生活随笔
收集整理的這篇文章主要介紹了
Windows下Spring3.x计划任务实现定时备份MySql数据库
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
今天在空閑之余查了一下關于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
總結
以上是生活随笔為你收集整理的Windows下Spring3.x计划任务实现定时备份MySql数据库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Oracle中计算两个时间的时间差:
- 下一篇: 通过nginx在window下部署项目