隨筆-112  評(píng)論-73  文章-0  trackbacks-0
          有一個(gè)自動(dòng)備份mysql 數(shù)據(jù)庫的需求,windows 下可以寫一個(gè)bat文件,然后加入到計(jì)劃任務(wù)中設(shè)置執(zhí)行,可是偉大的Windows系統(tǒng)加入計(jì)劃任務(wù)有時(shí)間卻不執(zhí)行,而且設(shè)置計(jì)劃任務(wù)也挺復(fù)雜(寫腳本把執(zhí)行備份的腳本加入計(jì)劃中)。那就用程序?qū)懸粋€(gè)吧備份的功能吧。還是調(diào)用備份的腳本,自動(dòng)任務(wù)部分使用Spring3的@Scheduled來實(shí)現(xiàn)。
          pom.xml文件中依賴的jar:
           <dependencies>
                  <dependency>
                      <groupId>org.springframework</groupId>
                      <artifactId>spring-context</artifactId>
                      <version>3.1.4.RELEASE</version>
                  </dependency>

                  <dependency>
                      <groupId>org.quartz-scheduler</groupId>
                      <artifactId>quartz</artifactId>
                      <version>1.8.5</version>
                  </dependency>
          </dependencies>
          spring-config.xml配置如下:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
                 xmlns:task
          ="http://www.springframework.org/schema/task"
                 xmlns:context
          ="http://www.springframework.org/schema/context"
                 xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation
          ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/task
                      http://www.springframework.org/schema/task/spring-task-3.1.xsd http://www.springframework.org/schema/context
                      http://www.springframework.org/schema/context/spring-context-3.1.xsd"
          >
              <context:component-scan base-package="cn.test" />
              <task:annotation-driven/>
          </beans>

          定義一個(gè)接口,寫一個(gè)實(shí)現(xiàn)類。
          package cn.test;

          /**
           * Created by libo on 13-12-18.
           
          */
          public interface SchedulerService {
              void doSome();
          }
          package cn.test;

          import org.springframework.scheduling.annotation.Scheduled;
          import org.springframework.stereotype.Component;

          import java.io.*;
          import java.util.Calendar;

          /**
           * Created by libo on 13-12-18.
           
          */
          @Component
          public class SchedulerServiceImpl implements SchedulerService {

              @Scheduled(cron = "0/5 * *  * * ? ")   //每5秒執(zhí)行一次
              @Override
              public void doSome() {
                  System.out.println("do soming" + Calendar.getInstance().getTime());
                  Runtime runtime = Runtime.getRuntime();
                  Process p = null;
                  FileWriter fw = null;
                  try {
                      //此處執(zhí)行的是ipconfig命令,可以換成任何cmd 里的命令。
                      p = runtime.exec("cmd /c ipconfig /all");
                      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
                      // 將命令執(zhí)行結(jié)果保存到文件中
                      fw = new FileWriter(new File("C:/temp/cmdout.txt"));
                      String line = null;
                      while ((line = reader.readLine()) != null) {
                          fw.write(line + "\n");
                      }
                      fw.flush();
                  } catch (IOException e) {
                      e.printStackTrace();
                  } finally {
                      if (p != null) {
                          p.destroy();
                      }
                      try {
                          if (fw != null)
                              fw.close();
                          if (p != null)
                              p.getOutputStream().close();
                      } catch (IOException e) {
                          e.printStackTrace();
                      }
                  }
                  System.out.println("do soming" + Calendar.getInstance().getTime());
              }

          }

          測(cè)試類(注意:使用junit是不能測(cè)試自動(dòng)任務(wù)地!)
          package cn.test;

          import org.springframework.context.ApplicationContext;
          import org.springframework.context.support.ClassPathXmlApplicationContext;

          /**
           * Created by libo on 13-12-18.
           
          */
          public class Test {

              public static void main(String[] args){
                  ApplicationContext context = new ClassPathXmlApplicationContext("/spring-config.xml");
                  System.out.println("請(qǐng)等待5秒讓任務(wù)飛一會(huì)兒!");
              }
          }

          end.
          posted on 2013-12-18 16:35 Libo 閱讀(812) 評(píng)論(0)  編輯  收藏 所屬分類: 其他
          主站蜘蛛池模板: 伊金霍洛旗| 蛟河市| 依安县| 涞源县| 信宜市| 无锡市| 长岭县| 江永县| 榆树市| 越西县| 张北县| 广昌县| 吉木萨尔县| 洛隆县| 健康| 安达市| 方城县| 盐边县| 桂平市| 孟连| 玛纳斯县| 怀远县| 东港市| 布尔津县| 吉木萨尔县| 彩票| 贵德县| 万源市| 如东县| 泽州县| 石棉县| 外汇| 黔西县| 宜春市| 库车县| 宁国市| 澄迈县| 融水| 甘南县| 长宁区| 普宁市|