[Java]簡單的排程Schedule程式

Task 需要被實作的類別

package yku;

public interface  Task {
    public boolean toDo();
    //public boolean doNext();
}

NetTest 測試網路狀態

package yku;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.*;
import java.text.ParseException;
import java.util.StringTokenizer;

public class NetTest implements Task {
    org.apache.log4j.Logger logger = org.apache.log4j.LogManager.getLogger(this
            .getClass());

    // toDo是被TaskRunner呼叫的function,必需實作
    @Override
    public boolean toDo() {
        logger.info("Do Test!");
        try {
            // 取得getGateWay的ip
            String gateway = windowsParseGateWay(windowsRunIpConfigCommand());
            logger.info("Default GateWay:" + gateway);
            if (gateway.length() == 0) {
                // 沒有ip時,利用Runtime.getRuntime.exec(要執行的cmd)來指行dos指令;
                Runtime.getRuntime().exec("c:/autoConnection2.cmd");
                logger.info("run autoCommend2.cmd");
            }
        } catch (ParseException e) {
            try {
                Runtime.getRuntime().exec("c:/autoConnection2.cmd");
            } catch (IOException ex) {
                logger.info(ex);
            }
            logger.info("run autoCommend2.cmd");
            e.printStackTrace();
            logger.info(e);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            logger.info(e);
        }

        return true;

    }

    // 執行ipconfig /all 來取得windows的網路資料
    private final static String windowsRunIpConfigCommand() throws IOException {
        Process p = Runtime.getRuntime().exec("ipconfig /all");
        InputStream stdoutStream = new BufferedInputStream(p.getInputStream());

        StringBuffer buffer = new StringBuffer();
        for (;;) {
            int c = stdoutStream.read();
            if (c == -1)
                break;
            buffer.append((char) c);
        }
        String outputText = buffer.toString();

        stdoutStream.close();

        return outputText;
    }

    private final static String windowsParseGateWay(String ipConfigResponse)
            throws ParseException {
        String localHost = null;
        try {
            // 先測試是否能取得ip,如果無ip,代表gateway也是沒有的
            localHost = InetAddress.getLocalHost().getHostAddress();
        } catch (java.net.UnknownHostException ex) {
            ex.printStackTrace();
            throw new ParseException(ex.getMessage(), 0);
        }
        // 把每一行當成一個要判斷的內容
        StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
        String lastMacAddress = null;
        boolean flg = false;
        while (tokenizer.hasMoreTokens()) {
            String line = tokenizer.nextToken().trim();
            // flg是底下用來判斷是在那一張網卡下的gateway,
            // 因為是利用撥接的ADSL,所以設定檔可能有二個以上
            if (flg) {

                // 取得Default Gateway的ip
                if (line.indexOf("Default Gateway") >= 0) {
                    int GatePosition = line.indexOf(":");
                    if (GatePosition >= 0) {
                        return line.substring(GatePosition + 1).trim();
                    }
                }
            }
            // 判斷是否在ADSL的撥接設定裡,其實只需要判斷存在此項目就可以了

            if (line.indexOf("WAN (PPP/SLIP) Interface") >= 0) {
                flg = true;
            }

        }

        ParseException ex = new ParseException(
                "cannot read Default Gateway from [" + ipConfigResponse + "]",
                0);
        ex.printStackTrace();
        throw ex;
    }

}

利用Windows排程在開機後讓ADSL自動撥號指令

存檔成autoConnection.cmd,其中rasphone.exe -d hinet是撥號adsl設定名稱為hinet,net start named 是啟動ics bind dns,java -jar xxx.jar是執行我打包好的jar檔

C:\WINDOWS\system32\rasphone.exe -d "hinet"
net start named
java -jar D:\tool\AutoCon\AutoCon.jar

當檢查斷線時執行的command

存檔成autoConnection2.cmd,其它設定跟前一個指令很像

@echo off
C:\WINDOWS\system32\rasphone.exe -d "hinet"
net stop named
net start named

原始碼下載

發表迴響