FutureTask实现线程执行可以设置超时时间

FutureTask实现线程超时执行
package com.trs;

import com.alibaba.fastjson.JSON;
import com.trs.tcm.server.DebugTask;
import com.trs.tcm.server.Task;
import com.trs.util.RedisUtil;
import org.junit.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.;
import java.util.concurrent.
;

/**

  • 截图工具类
  • Created by yangj on 2019/1/19 0019.

*/
public class PrintScreenTask extends Task {

private static ResourceBundle resourceBundle = ResourceBundle.getBundle("web");
//callable线程执行超时时间
private static final int TIMEOUT = 100;
//线程池线程数
private static final int THREADSIZE = 5;
//http
private static final String HTTP = "http://";
//https
private static final String HTTPS = "https://";
//可配置 TODO

// D:\TRS\wkhtmltopdf\bin\wkhtmltoimage.exe
private String toImageExe = “D:\TRS\wkhtmltopdf\bin\wkhtmltoimage.exe”;
//apache存储地址
private String basePath = “D:\TRS\httpd-2.4.37-o110j-x64-vc14-r2\Apache\htdocs\images\”;
// apache访问地址
private String apacheUrl = “http://localhost:9999/images/”;
// redis的key
private String screenKEY = “printscreen”;

public static void main(String[] args) throws Exception {
    new DebugTask().run(-1);
}

@Override
public void work() {
    try {
        saveScreenPrint();
    } catch (Exception e) {
        e.printStackTrace();
        error(e.getMessage());
    }
}

private ExecutorService executor = Executors.newFixedThreadPool(THREADSIZE);

public void saveScreenPrint() throws Exception {
    FutureTask future;
    InputStream in = new Object().getClass().getResourceAsStream("/web.properties");
    InputStream configIn = new Object().getClass().getResourceAsStream("/config.properties");
    Properties properties = new Properties();
    Properties configProperties = new Properties();
    properties.load(in);
    configProperties.load(configIn);
    System.out.println(properties);
    apacheUrl = (String) configProperties.get("apacheUrl");
    basePath = (String) configProperties.get("basePath");
    screenKEY = (String) configProperties.get("screenKEY");
    toImageExe = (String) configProperties.get("toImageExe");
    System.out.println(configProperties);
    Map map = new HashMap<>();
    for (Object key : properties.keySet()) {
        String outFile = (String) properties.get(key);
        String httpUrl = HTTP + key;
        String commond = toImageExe + " " + httpUrl + " " +basePath+outFile+".jpg";

// createImage(commond);
future = new FutureTask(new CommondCallable(commond));
executor.submit(future);
try {
future.get(TIMEOUT, TimeUnit.SECONDS);
} catch (Exception e) {
e.printStackTrace();
reGetImage(commond, 0);
}
map.put(outFile,apacheUrl+outFile+".jpg");
}
String json = JSON.toJSONString(map);
RedisUtil.getRedisUtil().set(screenKEY,json);
info(“存储redis完毕…”);
}

private void reGetImage(String commond, int count) {
    info("重新截图 -- 截图命令:"+ commond);
    if (count >= 10) {
        info("本次截图失败..");
    }
    count++;
    FutureTask future = new FutureTask(new CommondCallable(commond));
    executor.submit(future);
    try {
        future.get(TIMEOUT, TimeUnit.SECONDS);
    } catch (Exception e) {
        error("任务执行超时..");
        e.printStackTrace();
        reGetImage(commond, count);
    }
}

class CommondCallable implements Callable {

    private String commond;

    public CommondCallable(String commond) {
        this.commond = commond;
    }

    @Override
    public Object call() throws Exception {
        info("开始截图 --- 截图命令:"+commond);
        Runtime.getRuntime().exec(commond);
        Process proc = Runtime.getRuntime().exec(commond.toString());
        proc.waitFor();
        info("截图完毕..");
        return true;
    }
}

}

不定期更新~

你可能感兴趣的:(FutureTask实现线程执行可以设置超时时间)