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<String> 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<String,String> 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<String> future = new FutureTask<String>(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;
    }
}

}

不定期更新~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值