记录一下 spring boot 线程处理返回数据

@Api
@Controller
public class FileController {


    private final static ExecutorService executor = Executors.newFixedThreadPool(2);
    private static Logger logger = LoggerFactory.getLogger(FileController.class);

    @Autowired
    private FileService fileService;


    @PostMapping("/uploadFile")
    @ApiResponse
    @ApiOperation(value = "上传文件", httpMethod = "POST")
    @PassToken
    public FileResponse uploadFile(@RequestParam("file") MultipartFile multipartFile) throws ApiException {

        FileResponse fileResponse = new FileResponse();
        fileResponse.setFilepath(fileService.uploadFile(multipartFile));
        return fileResponse;

    }


    @PostMapping("/uploadVideoFile")
    @ApiResponse
    @ApiOperation(value = "上传文件", httpMethod = "POST")
    @PassToken
    public FileResponse uploadVideoFile(@RequestParam("file") MultipartFile multipartFile) throws Exception {

        FileResponse fileResponse = new FileResponse();

        final CountDownLatch end = new CountDownLatch(2);
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    fileResponse.setFilepath(fileService.uploadFile(multipartFile));
                }  catch(Exception e){
                    fileResponse.setFilepath(null);
                    logger.error(e.getMessage());
                }finally{
                    end.countDown();
                }

            }

        });
        executor.execute(new Runnable() {
            @Override
            public void run() {
                try {
                    fileResponse.setVideFilePath(fileService.cutImage(multipartFile, 1));
                } catch (Exception e) {
                    fileResponse.setVideFilePath(null);
                    logger.error(e.getMessage());
                } finally {
                    end.countDown();
                }

            }

        });
        end.await();
        return fileResponse;
    }

 


posted @ 2020-11-09 17:05  爱吃炒饭的逗比小豆豆  阅读(384)  评论(0编辑  收藏  举报