diff --git a/src/main/java/com/tuoheng/steam/service/TaskService.java b/src/main/java/com/tuoheng/steam/service/TaskService.java index 95db085..25b3351 100644 --- a/src/main/java/com/tuoheng/steam/service/TaskService.java +++ b/src/main/java/com/tuoheng/steam/service/TaskService.java @@ -8,6 +8,9 @@ import com.tuoheng.steam.util.TimeUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -18,8 +21,35 @@ import java.util.*; import java.util.concurrent.*; @Service +@EnableScheduling public class TaskService implements ITaskService{ + @Value("${srs.targetPath}") + private String targetPath; + + + /** + * 清除没有被关闭的进程 + */ + @Scheduled(fixedRate = 60000) + public void cleaTask() { + for (ConcurrentHashMap.Entry entry : runningTasks.entrySet()) { + StreamTask streamTask = entry.getValue(); + if(streamTask.getStartTime().getTime() < new Date().getTime() - 60 * 20 *60 ) { + StreamTask s = stopTask(entry.getKey()); + scheduler.schedule(() -> { + File file = new File(targetPath +"/"+ s.getOutFileName()); + try { + file.delete(); + logger.info("废弃文件删除成功"); + }catch (Exception e) { + logger.error(e.getMessage()); + } + }, 60, TimeUnit.SECONDS); + } + } + } + private static final Logger logger = LoggerFactory.getLogger(TaskService.class); @Autowired @@ -38,7 +68,7 @@ public class TaskService implements ITaskService{ /** * 任务池 */ - ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); + ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2); @PostConstruct public void init() { @@ -96,7 +126,7 @@ public class TaskService implements ITaskService{ if (runningTasks.putIfAbsent(streamUrl, taskInstance) == null) { logger.info("streamUrl {} startTask {} ", streamUrl, taskInstance.getTaskId()); startTask(streamUrl, taskInstance); - runningTasks.put(streamUrl, taskInstance); + // runningTasks.put(streamUrl, taskInstance); return taskInstance; } else { return runningTasks.get(streamUrl); diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 6af6220..dc50637 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -1,4 +1,6 @@ spring.application.name=demo -server.port = 9011 +server.port = 8080 srs.splitPath=/data/java/srs/stream_server/temp -srs.targetPath=/data/java/srs/srs/trunk/objs/nginx/html \ No newline at end of file +srs.targetPath=/data/java/srs/srs/trunk/objs/nginx/html +#srs.splitPath=/Users/sunpeng/workspace/stream/temp +#srs.targetPath=/Users/sunpeng/workspace/stream/html \ No newline at end of file