wuxw
2024-01-22 a40c2eb885bcdb697c2e16dae4f43cb6cd718973
service-job/src/main/java/com/java110/job/export/ExportDataExecutor.java
@@ -3,9 +3,9 @@
import com.java110.core.client.FileUploadTemplate;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.data.ExportDataDto;
import com.java110.dto.userDownloadFile.UserDownloadFileDto;
import com.java110.dto.user.UserDownloadFileDto;
import com.java110.intf.job.IUserDownloadFileV1InnerServiceSMO;
import com.java110.po.userDownloadFile.UserDownloadFilePo;
import com.java110.po.user.UserDownloadFilePo;
import com.java110.utils.factory.ApplicationContextFactory;
import com.java110.utils.util.ExceptionUtil;
import com.java110.utils.util.StringUtil;
@@ -30,19 +30,29 @@
    //默认线程大小
    private static final int DEFAULT_EXPORT_POOL = 4;
    private boolean isRun = false;
    public ExportDataExecutor(boolean isRun) {
        this.isRun = isRun;
    }
    public ExportDataExecutor() {
        this.userDownloadFileV1InnerServiceSMOImpl = ApplicationContextFactory.getBean("userDownloadFileV1InnerServiceSMOImpl", IUserDownloadFileV1InnerServiceSMO.class);
        this.fileUploadTemplate = ApplicationContextFactory.getBean("fileUploadTemplate", FileUploadTemplate.class);
    }
    @Override
    public void run() {
        try {
            doExportData();
        } catch (Exception e) {
            log.error("处理消息异常", e);
            e.printStackTrace();
        while (isRun) {
            log.debug("导出数据线程开始处理");
            try {
                doExportData();
            } catch (Throwable e) {
                log.error("处理消息异常", e);
                e.printStackTrace();
            }
            log.debug("导出数据线程处理完成");
        }
    }
@@ -50,6 +60,9 @@
    private void doExportData() throws Exception {
        ExportDataDto exportDataDto = ExportDataQueue.getData();
        this.userDownloadFileV1InnerServiceSMOImpl = ApplicationContextFactory.getBean("userDownloadFileV1InnerServiceSMOImpl", IUserDownloadFileV1InnerServiceSMO.class);
        this.fileUploadTemplate = ApplicationContextFactory.getBean("fileUploadTemplate", FileUploadTemplate.class);
        ByteArrayInputStream inputStream = null;
        ByteArrayOutputStream os = null;
@@ -76,9 +89,12 @@
            inputStream = new ByteArrayInputStream(os.toByteArray());
            fileName = fileUploadTemplate.saveFile(inputStream, exportDataDto.getFileName());
            updateUserDownloadFile(exportDataDto, UserDownloadFileDto.STATE_FINISH,fileName, "下载完成");
        } catch (Exception e) {
        } catch (Throwable e) {
            e.printStackTrace();
            updateUserDownloadFile(exportDataDto, UserDownloadFileDto.STATE_FAIL, "","下载失败" + ExceptionUtil.getStackTrace(e));
        } finally {
            try {
@@ -115,7 +131,10 @@
     * 线程启动器
     */
    public static void startExportDataExecutor() {
        log.debug("开始初始化导出队列");
        ExecutorService executorService = Executors.newFixedThreadPool(DEFAULT_EXPORT_POOL);
        executorService.execute(new ExportDataExecutor());
        executorService.execute(new ExportDataExecutor(true));
        log.debug("初始化导出队列完成");
    }
}