java Timer 定时任务(二)

来源:互联网 发布:mac魔法卡片助手 编辑:程序博客网 时间:2024/05/16 05:25

上篇文章介绍了模仿程序时出现的问题

下面是shell启动执行调度的一个简单流程图:

 


下面一步一步搭建调度:
构建timer 类:
public class MyTimer  extends TimerTask {
/** *//**
* Simple demo that uses java.util.Timer to schedule a task to execute
* once 5 seconds have passed.
*/ 


public  MyTimer(){

}
public void run(){ 
       System.out.println("Time''s up!");
       MyThread my=new MyThread();
       my.start();
   } 

public static void main(String[] args) {
System.out.println("About to schedule task.");
String flowCode="";
String proFilePath = "lowtimer.properties";
TimerPropertiesLoader properties = new TimerPropertiesLoader(proFilePath);
//PropertiesLoader properties = new PropertiesLoader("flowtimer.properties");
String hour = properties.getProperty("HOUR_OF_DAY");
String minute = properties.getProperty("MINUTE");
String second = properties.getProperty("SECOND");
//flowType = properties.getProperty(flowCode + "_TYPE");
//shellPath = properties.getProperty(flowCode + "_SHELL_PATH");
System.out.println("【flowCode:" + flowCode + "流程Timer启动成功,每天的执行时间是:" + hour + ":" + minute + ":" + second);
Timer timer = new Timer();
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(hour)); // 控制时
calendar.set(Calendar.MINUTE, Integer.parseInt(minute)); // 控制分
calendar.set(Calendar.SECOND, Integer.parseInt(second)); // 控制秒
Date time = calendar.getTime(); // 得出执行任务的时间,此处为今天的12:00:00
   try{
timer.schedule(new MyTimer(), time, 5000);//第一个参数是任务 第二个参数 是第一次执行的时间,第三个参数 是周期 每隔多长时间执行一次1000 * 60 * 60 * 24 每隔24小时执行一次 所以还是每天在这个时间去执行
   }catch(Exception e){
   e.printStackTrace();
   }
   System.out.println("Task scheduled.");
}
}




/*ProcessBuilder proc = new ProcessBuilder("notepad.exe", "test");
try {
proc.directory(new File("D:\\"));
Process start =proc.start();
// any error message?
StreamGobbler errorGobbler = new StreamGobbler(start.getErrorStream(), "ERROR");
// any output?
StreamGobbler outputGobbler = new
StreamGobbler(start.getInputStream(), "OUTPUT");
// kick them off
errorGobbler.start();
outputGobbler.start();
int waitFor = start.waitFor();
if(waitFor==0){
System.out.println(waitFor);
}

} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}*/
class StreamGobbler extends Thread
{
InputStream is;
String type;


StreamGobbler(InputStream is, String type)
{
this.is = is;
this.type = type;
}


public void run()
{
try
{
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null)
System.out.println(line);
} catch (IOException ioe)
{
ioe.printStackTrace();
}
}
}
class MyThread extends Thread {
private volatile Process process;


public void close() {
process.destroy();
}
@Override
public void run() {
System.out.println("开始线程-----");
// String[] command = {"/server/script/shell/runCreateTxt.sh" };
System.out.println("执行成功");
ProcessBuilder builder = new ProcessBuilder("sh","/server/script/shell/runCreateTxt.sh");
try {
process = builder.start();
StreamGobbler errorGobbler = new
StreamGobbler(process.getErrorStream(), "ERROR");
StreamGobbler outputGobbler = new
StreamGobbler(process.getInputStream(), "OUTPUT");

errorGobbler.start();
outputGobbler.start();
System.out.println("开始执行-----");
int exitVal = process.waitFor();
System.out.println("执行结束-------");
System.out.println("ExitValue: " + exitVal);
} catch (IOException e) {
e.printStackTrace();
} // 获取脚本错误输出
catch (InterruptedException e) {
e.printStackTrace();
}


}
}

解析属性文件的(TimerPropertiesLoader):
public class TimerPropertiesLoader {




private final Properties properties;
public TimerPropertiesLoader(String... resourcesPaths) {
properties = loadProperties(resourcesPaths);
}

public Properties getProperties() {
return properties;
}


/**
* 取出Property。
*/
private String getValue(String key) {
String systemProperty = System.getProperty(key);
if (systemProperty != null) {
return systemProperty;
}
return properties.getProperty(key);
}


/**
* 取出String类型的Property,如果都為Null则抛出异常.
*/
public String getProperty(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return value;
}


/**
* 取出String类型的Property.如果都為Null則返回Default值.
*/
public String getProperty(String key, String defaultValue) {
String value = getValue(key);
return value != null ? value : defaultValue;
}


/**
* 取出Integer类型的Property.如果都為Null或内容错误则抛出异常.
*/
public Integer getInteger(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Integer.valueOf(value);
}


/**
* 取出Integer类型的Property.如果都為Null則返回Default值,如果内容错误则抛出异常
*/
public Integer getInteger(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Integer.valueOf(value) : defaultValue;
}


/**
* 取出Double类型的Property.如果都為Null或内容错误则抛出异常.
*/
public Double getDouble(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Double.valueOf(value);
}


/**
* 取出Double类型的Property.如果都為Null則返回Default值,如果内容错误则抛出异常
*/
public Double getDouble(String key, Integer defaultValue) {
String value = getValue(key);
return value != null ? Double.valueOf(value) : defaultValue;
}


/**
* 取出Boolean类型的Property.如果都為Null抛出异常,如果内容不是true/false则返回false.
*/
public Boolean getBoolean(String key) {
String value = getValue(key);
if (value == null) {
throw new NoSuchElementException();
}
return Boolean.valueOf(value);
}


/**
* 取出Boolean类型的Propert.如果都為Null則返回Default值,如果内容不为true/false则返回false.
*/
public Boolean getBoolean(String key, boolean defaultValue) {
String value = getValue(key);
return value != null ? Boolean.valueOf(value) : defaultValue;
}


/**
* 载入多个文件, 文件路径使用Spring Resource格式.
*/
private Properties loadProperties(String... resourcesPaths) {
Properties props = new Properties();


for (String location : resourcesPaths) {


System.out.println("Loading properties file from path:{}"+location);


InputStream is = null;
try {
is = new BufferedInputStream(new FileInputStream(location));
props.load(is); 
} catch (IOException ex) {
System.out.println("Could not load properties from path:{}, {} "+ location+ex.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return props;
}

}

属性文件(flowtimer.properties):
#BT_USER_INFO_DAY
HOUR_OF_DAY=22
MINUTE=30
SECOND=0
SLEEP=300000
TYPE=D
#SHELL_PATH=/home//timer/bin/start_bss.sh

启动的shell:
[root@hadoop06 shell]# vi startTimer.sh
nohup java -cp /server/script/shell/timer.jar com.test.timer.MyTimer &

所要执行的shell:
[root@hadoop06 shell]# vi runCreateTxt.sh


#!/bin/sh


for i in $(cat createTxt.txt)
  do
    #mv $i  ${i/txt/doc}
    touch  $i
done

按照上面的来肯定能成功,一些执行的shell都可以在属性文件中进行配置
0 0
原创粉丝点击