<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-112  評論-73  文章-0  trackbacks-0
    有一個自動備份mysql 數據庫的需求,windows 下可以寫一個bat文件,然后加入到計劃任務中設置執行,可是偉大的Windows系統加入計劃任務有時間卻不執行,而且設置計劃任務也挺復雜(寫腳本把執行備份的腳本加入計劃中)。那就用程序寫一個吧備份的功能吧。還是調用備份的腳本,自動任務部分使用Spring3的@Scheduled來實現。
    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>

    定義一個接口,寫一個實現類。
    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秒執行一次
        @Override
        public void doSome() {
            System.out.println("do soming" + Calendar.getInstance().getTime());
            Runtime runtime = Runtime.getRuntime();
            Process p = null;
            FileWriter fw = null;
            try {
                //此處執行的是ipconfig命令,可以換成任何cmd 里的命令。
                p = runtime.exec("cmd /c ipconfig /all");
                BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream(), "GBK"));
                // 將命令執行結果保存到文件中
                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());
        }

    }

    測試類(注意:使用junit是不能測試自動任務地!)
    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("請等待5秒讓任務飛一會兒!");
        }
    }

    end.
    posted on 2013-12-18 16:35 Libo 閱讀(812) 評論(0)  編輯  收藏 所屬分類: 其他
    主站蜘蛛池模板: 亚洲高清专区日韩精品| 欧洲精品免费一区二区三区| 亚洲中文字幕无码久久2017| 日韩免费在线中文字幕| 免费国产美女爽到喷出水来视频| 亚洲熟妇AV日韩熟妇在线| 在线免费观看a级片| 国内精品久久久久影院亚洲| 久久精品国产亚洲av麻豆色欲| 中文日本免费高清| 亚洲精品无码AV人在线播放| 久久免费美女视频| 亚洲成熟xxxxx电影| 猫咪免费人成网站在线观看| 亚洲av成人综合网| 日本免费人成黄页网观看视频 | 国内一级一级毛片a免费| 亚洲AV无码一区二区三区牛牛| 午夜小视频免费观看| 美女被免费视频网站| 亚洲理论电影在线观看| 免费人成在线观看网站品爱网| 亚洲高清免费视频| AAAAA级少妇高潮大片免费看| 久久精品国产精品亚洲精品| 1000部羞羞禁止免费观看视频| 亚洲kkk4444在线观看| 在线a亚洲v天堂网2018| 你是我的城池营垒免费看| 亚洲色偷偷偷网站色偷一区| 好男人视频在线观看免费看片| 国产精品免费久久久久久久久| 亚洲经典在线中文字幕| 免费无码又爽又刺激网站| 老色鬼久久亚洲AV综合| 在线免费观看污网站| 国产免费久久精品99久久| 亚洲无砖砖区免费| 免费一区二区三区四区五区 | 免费国产草莓视频在线观看黄| 国产成人A亚洲精V品无码|