线程池执行exe文件
This commit is contained in:
@@ -13,7 +13,6 @@ import org.springframework.scheduling.annotation.EnableAsync;
|
||||
* @author tajochen
|
||||
*/
|
||||
@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
|
||||
@EnableAsync
|
||||
public class MicroserviceApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
@@ -0,0 +1,27 @@
|
||||
package com.xkrs.microservice.common.config;
|
||||
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.annotation.EnableAsync;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
|
||||
@Configuration
|
||||
@EnableAsync
|
||||
public class AsyncConfig {
|
||||
|
||||
@Bean(name = "threadPoolTaskExecutor")
|
||||
public Executor threadPoolTaskExecutor() {
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||
executor.setCorePoolSize(10); // 核心线程数
|
||||
executor.setMaxPoolSize(20); // 最大线程数
|
||||
executor.setQueueCapacity(500); // 队列容量
|
||||
executor.setKeepAliveSeconds(60); // 线程空闲时间
|
||||
executor.setThreadNamePrefix("MyThreadPoolTaskExecutor-"); // 线程名前缀
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); // 拒绝策略
|
||||
executor.initialize(); // 初始化
|
||||
return executor;
|
||||
}
|
||||
}
|
@@ -0,0 +1,17 @@
|
||||
package com.xkrs.microservice.service;
|
||||
|
||||
public interface AsyncService {
|
||||
|
||||
/**
|
||||
* 异步执行exe文件
|
||||
* @param id 数据文件id
|
||||
* @param dataFileCode 数据文件code
|
||||
* @param leftFilePath 左影像文件地址
|
||||
* @param rightFilePath 右影像文件地址
|
||||
* @param maxX 最大X坐标
|
||||
* @param maxY 最大Y坐标
|
||||
* @param minX 最小X坐标
|
||||
* @param minY 最小Y坐标
|
||||
*/
|
||||
public void executeRunData(Integer id,String dataFileCode,String leftFilePath,String rightFilePath,String maxX,String maxY,String minX,String minY);
|
||||
}
|
@@ -57,7 +57,7 @@ public interface DataFileService {
|
||||
|
||||
/**
|
||||
* 运行数据文件
|
||||
* @param dataFileEntity
|
||||
* @param dataFileQo
|
||||
* @return
|
||||
*/
|
||||
ResultVo runDataFile(DataFileQo dataFileQo);
|
||||
|
@@ -0,0 +1,66 @@
|
||||
package com.xkrs.microservice.service.impl;
|
||||
|
||||
import com.xkrs.microservice.common.config.RedisUtil;
|
||||
import com.xkrs.microservice.dao.DataFileDao;
|
||||
import com.xkrs.microservice.model.entity.DataFileEntity;
|
||||
import com.xkrs.microservice.service.AsyncService;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.scheduling.annotation.Async;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import javax.annotation.RegEx;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
/**
|
||||
* 线程池执行
|
||||
*/
|
||||
@Service
|
||||
public class AsyncServiceImpl implements AsyncService {
|
||||
|
||||
@Value("${my.gsmExePath}")
|
||||
private String gsmExePath;
|
||||
|
||||
@Value("${my.gsmOutPath}")
|
||||
private String gsmOutPath;
|
||||
|
||||
@Resource
|
||||
private DataFileDao dataFileDao;
|
||||
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Async("threadPoolTaskExecutor")
|
||||
@Override
|
||||
public void executeRunData(Integer id,String dataFileCode,String leftFilePath,String rightFilePath,String maxX,String maxY,String minX,String minY){
|
||||
String[] params = {gsmExePath,"-l",leftFilePath,"-r",rightFilePath,"-o",gsmOutPath + dataFileCode,
|
||||
"--maxx",maxX,"--maxy",maxY,"--minx",minX,"--miny",minY};
|
||||
try {
|
||||
// 执行命令
|
||||
Process process = Runtime.getRuntime().exec(params);
|
||||
|
||||
// 读取执行结果
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
// 等待执行结束
|
||||
int exitCode = process.waitFor();
|
||||
DataFileEntity dataFileEntity = dataFileDao.findById(id).get();
|
||||
dataFileEntity.setRunFlag(2);
|
||||
dataFileDao.save(dataFileEntity);
|
||||
}catch (IOException | InterruptedException e){
|
||||
e.printStackTrace();
|
||||
// 如果出现异常将文件的运行状态改为未运行
|
||||
DataFileEntity dataFileEntity = dataFileDao.findById(id).get();
|
||||
dataFileEntity.setRunFlag(0);
|
||||
dataFileDao.save(dataFileEntity);
|
||||
}
|
||||
//不管运行结果怎么样都把文件锁去掉
|
||||
redisUtil.del("runFile_" + id);
|
||||
}
|
||||
}
|
@@ -10,6 +10,7 @@ import com.xkrs.microservice.model.qo.DataFileQo;
|
||||
import com.xkrs.microservice.model.qo.ZipFileInfoQo;
|
||||
import com.xkrs.microservice.model.vo.FileServiceResDetailVo;
|
||||
import com.xkrs.microservice.model.vo.ResultVo;
|
||||
import com.xkrs.microservice.service.AsyncService;
|
||||
import com.xkrs.microservice.service.DataFileService;
|
||||
import com.xkrs.microservice.service.FileServerService;
|
||||
import com.xkrs.microservice.util.CopyPropertiesUtil;
|
||||
@@ -55,12 +56,6 @@ public class DataFileServiceImpl implements DataFileService {
|
||||
@Value("${my.FileServerPath}")
|
||||
private String fileServerPath;
|
||||
|
||||
@Value("${my.gsmExePath}")
|
||||
private String gsmExePath;
|
||||
|
||||
@Value("${my.gsmOutPath}")
|
||||
private String gsmOutPath;
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(DataFileServiceImpl.class);
|
||||
|
||||
@Resource
|
||||
@@ -72,6 +67,8 @@ public class DataFileServiceImpl implements DataFileService {
|
||||
@Resource
|
||||
private RedisUtil redisUtil;
|
||||
|
||||
@Resource
|
||||
private AsyncService asyncService;
|
||||
|
||||
/**
|
||||
* 获取所有数据文件
|
||||
@@ -163,7 +160,7 @@ public class DataFileServiceImpl implements DataFileService {
|
||||
dataFileDao.save(dataFileEntity);
|
||||
Optional<DataFileEntity> optional = dataFileDao.findById(dataFileQo.getId());
|
||||
//线程池,执行GSM操作
|
||||
executeRunData(dataFileQo.getId(),optional.get().getDataFileCode(),optional.get().getLeftFilePath(),
|
||||
asyncService.executeRunData(dataFileQo.getId(),optional.get().getDataFileCode(),optional.get().getLeftFilePath(),
|
||||
optional.get().getRightFilePath(),
|
||||
dataFileQo.getMaxX(),dataFileQo.getMaxY(),
|
||||
dataFileQo.getMinX(),dataFileQo.getMinY());
|
||||
@@ -172,49 +169,6 @@ public class DataFileServiceImpl implements DataFileService {
|
||||
return resultVo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行文件操作
|
||||
* @param id 文件的id
|
||||
* @param dataFileCode 上传的文件编码
|
||||
* @param leftFilePath 左影像文件
|
||||
* @param rightFilePath 右影像文件
|
||||
* @param maxX
|
||||
* @param maxY
|
||||
* @param minX
|
||||
* @param minY
|
||||
*/
|
||||
@Async
|
||||
public void executeRunData(Integer id,String dataFileCode,String leftFilePath,String rightFilePath,String maxX,String maxY,String minX,String minY){
|
||||
String[] params = {gsmExePath,"-l",leftFilePath,"-r",rightFilePath,"-o",gsmOutPath + dataFileCode,
|
||||
"--maxx",maxX,"--maxy",maxY,"--minx",minX,"--miny",minY};
|
||||
try {
|
||||
// 执行命令
|
||||
Process process = Runtime.getRuntime().exec(params);
|
||||
|
||||
// 读取执行结果
|
||||
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
System.out.println(line);
|
||||
}
|
||||
|
||||
// 等待执行结束
|
||||
int exitCode = process.waitFor();
|
||||
DataFileEntity dataFileEntity = dataFileDao.findById(id).get();
|
||||
dataFileEntity.setRunFlag(2);
|
||||
dataFileDao.save(dataFileEntity);
|
||||
}catch (IOException | InterruptedException e){
|
||||
e.printStackTrace();
|
||||
// 如果出现异常将文件的运行状态改为未运行
|
||||
DataFileEntity dataFileEntity = dataFileDao.findById(id).get();
|
||||
dataFileEntity.setRunFlag(0);
|
||||
dataFileDao.save(dataFileEntity);
|
||||
}
|
||||
//不管运行结果怎么样都把文件锁去掉
|
||||
redisUtil.del("runFile_" + id);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public ResultVo uploadDataFile(DataFileQo dataFileQo, MultipartFile leftFile, MultipartFile rightFile, String userName) {
|
||||
ResultVo resultVo = new ResultVo();
|
||||
|
@@ -9,13 +9,6 @@ spring.application.name = RemoteSensing Shandong
|
||||
|
||||
server.tomcat.uri-encoding = UTF-8
|
||||
|
||||
# 线程池设置
|
||||
spring.task.execution.pool.core-size=10
|
||||
spring.task.execution.pool.max-size=50
|
||||
spring.task.execution.pool.queue-capacity=100
|
||||
spring.task.execution.pool.keep-alive=60s
|
||||
|
||||
|
||||
## 关闭spring data 的redis仓库
|
||||
spring.data.redis.repositories.enabled = false
|
||||
|
||||
|
Reference in New Issue
Block a user