Commit 010404dc by landerliang@163.com

不使用Quartz管理定时任务了,改为纯Scheduled,根据多少个任务开多少个线程,目前站点能够正常使用

parent 18d6e520
...@@ -63,4 +63,4 @@ ...@@ -63,4 +63,4 @@
<artifactId>commons-codec</artifactId> <artifactId>commons-codec</artifactId>
</dependency> </dependency>
</dependencies> </dependencies>
</project> </project>
\ No newline at end of file
...@@ -39,7 +39,7 @@ import org.springframework.web.client.RestTemplate; ...@@ -39,7 +39,7 @@ import org.springframework.web.client.RestTemplate;
* @date 2018/11/15 9:20:19 * @date 2018/11/15 9:20:19
*/ */
@MapperScan("me.zhengjie.modules.system.repository") @MapperScan("me.zhengjie.modules.system.repository")
//@EnableAsync @EnableAsync
@RestController @RestController
@Api(hidden = true) @Api(hidden = true)
@EnableScheduling @EnableScheduling
......
...@@ -40,7 +40,7 @@ public class AsyncTaskExecutePool implements AsyncConfigurer { ...@@ -40,7 +40,7 @@ public class AsyncTaskExecutePool implements AsyncConfigurer {
} }
@Override @Override
public Executor getAsyncExecutor() { public ThreadPoolTaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//核心线程池大小 //核心线程池大小
executor.setCorePoolSize(config.getCorePoolSize()); executor.setCorePoolSize(config.getCorePoolSize());
...@@ -50,11 +50,15 @@ public class AsyncTaskExecutePool implements AsyncConfigurer { ...@@ -50,11 +50,15 @@ public class AsyncTaskExecutePool implements AsyncConfigurer {
executor.setQueueCapacity(config.getQueueCapacity()); executor.setQueueCapacity(config.getQueueCapacity());
//活跃时间 //活跃时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds()); executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
//优雅关闭线程 用来设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
executor.setWaitForTasksToCompleteOnShutdown(true);
//线程名字前缀 //线程名字前缀
executor.setThreadNamePrefix("el-async-"); executor.setThreadNamePrefix("el-async-");
//当pool已经达到最大size则丢弃前面的任务,不抛出异常
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
// setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务 // setRejectedExecutionHandler:当pool已经达到max size的时候,如何处理新任务
// CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行 // CallerRunsPolicy:不在新线程中执行任务,而是由调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize(); executor.initialize();
return executor; return executor;
} }
......
package me.zhengjie.modules.quartz.config;
import lombok.extern.slf4j.Slf4j;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/**
* @author Lander
*/
@Slf4j
public class CustomAbortPolicy implements RejectedExecutionHandler {
public void CustomAbortPolicy() { }
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
System.out.println("进入拒绝策略。。。。。。。。。。");
if (!executor.isShutdown()) {
try {
log.error("full-->>线程池已满,执行拒绝策略");
while (executor.getQueue().remainingCapacity() == 0);
executor.execute(r);
} catch (Exception e) {
log.error("rejectedExecutionException====>>>>>"+e.toString());
}
}
}
}
...@@ -32,11 +32,11 @@ import java.util.List; ...@@ -32,11 +32,11 @@ import java.util.List;
* @date 2019-01-07 * @date 2019-01-07
*/ */
@Component @Component
@RequiredArgsConstructor //@RequiredArgsConstructor
public class JobRunner implements ApplicationRunner { public class JobRunner implements ApplicationRunner {
private static final Logger log = LoggerFactory.getLogger(JobRunner.class); private static final Logger log = LoggerFactory.getLogger(JobRunner.class);
private final QuartzJobRepository quartzJobRepository; //private final QuartzJobRepository quartzJobRepository;
private final QuartzManage quartzManage; //private final QuartzManage quartzManage;
/** /**
* 项目启动时重新激活启用的定时任务 * 项目启动时重新激活启用的定时任务
...@@ -46,8 +46,8 @@ public class JobRunner implements ApplicationRunner { ...@@ -46,8 +46,8 @@ public class JobRunner implements ApplicationRunner {
@Override @Override
public void run(ApplicationArguments applicationArguments) { public void run(ApplicationArguments applicationArguments) {
log.info("--------------------注入定时任务---------------------"); log.info("--------------------注入定时任务---------------------");
List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse(); //List<QuartzJob> quartzJobs = quartzJobRepository.findByIsPauseIsFalse();
quartzJobs.forEach(quartzManage::addJob); //quartzJobs.forEach(quartzManage::addJob);
log.info("--------------------定时任务注入完成---------------------"); log.info("--------------------定时任务注入完成---------------------");
} }
} }
...@@ -15,11 +15,16 @@ ...@@ -15,11 +15,16 @@
*/ */
package me.zhengjie.modules.quartz.config; package me.zhengjie.modules.quartz.config;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.config.thread.AsyncTaskExecutePool;
import me.zhengjie.config.thread.AsyncTaskProperties;
import org.quartz.Scheduler; import org.quartz.Scheduler;
import org.quartz.spi.TriggerFiredBundle; import org.quartz.spi.TriggerFiredBundle;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory; import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.AdaptableJobFactory; import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.SchedulerFactoryBean; import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
...@@ -29,9 +34,17 @@ import org.springframework.stereotype.Component; ...@@ -29,9 +34,17 @@ import org.springframework.stereotype.Component;
* @author / * @author /
* @date 2019-01-07 * @date 2019-01-07
*/ */
@Slf4j
@Configuration @Configuration
public class QuartzConfig { public class QuartzConfig {
/** 注入配置类 */
private final AsyncTaskProperties config;
private final AsyncTaskExecutePool asyncTaskExecutePool;
public QuartzConfig(AsyncTaskProperties config, AsyncTaskExecutePool asyncTaskExecutePool) {
this.config = config;
this.asyncTaskExecutePool = asyncTaskExecutePool;
}
/** /**
* 解决Job中注入Spring Bean为null的问题 * 解决Job中注入Spring Bean为null的问题
*/ */
...@@ -54,6 +67,32 @@ public class QuartzConfig { ...@@ -54,6 +67,32 @@ public class QuartzConfig {
} }
} }
@Bean(name ="quartzTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
/*ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 核心线程数
executor.setCorePoolSize(config.getCorePoolSize());
// 最大线程数
executor.setMaxPoolSize(config.getMaxPoolSize());
// 任务队列大小
executor.setQueueCapacity(config.getQueueCapacity());
// 线程前缀名
executor.setThreadNamePrefix("CartReptiles-Quartz");
// 线程的空闲时间
executor.setKeepAliveSeconds(config.getKeepAliveSeconds());
executor.setThreadFactory(new ThreadFactoryBuilder().setNameFormat("Quartz定时任务-runner-%d").build());
// 拒绝策略
executor.setRejectedExecutionHandler(new CustomAbortPolicy());
executor.setWaitForTasksToCompleteOnShutdown(true);
// 线程初始化
//executor.initialize();
return executor;*/
return asyncTaskExecutePool.getAsyncExecutor();
}
/** /**
* 注入scheduler到spring * 注入scheduler到spring
* @param quartzJobFactory / * @param quartzJobFactory /
......
...@@ -145,7 +145,7 @@ public class QuartzJobServiceImpl implements QuartzJobService { ...@@ -145,7 +145,7 @@ public class QuartzJobServiceImpl implements QuartzJobService {
} }
} }
//@Async @Async
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void executionSubJob(String[] tasks) throws InterruptedException { public void executionSubJob(String[] tasks) throws InterruptedException {
......
...@@ -27,15 +27,68 @@ import org.springframework.stereotype.Component; ...@@ -27,15 +27,68 @@ import org.springframework.stereotype.Component;
@Component @Component
public class TestTask { public class TestTask {
/*private final JobMapper jobMapper;
private final ReportService reportService;
@Autowired
public TestTask(JobMapper jobMapper, ReportService reportService) {
this.jobMapper = jobMapper;
this.reportService = reportService;
}*/
public void run(){ public void run(){
log.info("run 执行成功");
}
public void run1(String str){ /* log.info(">>> ================================ 开始自动爬取新的检测报告进行下载、签章、上传 ====================================");
log.info("run1 执行成功,参数为: {}" + str);
} //查询全部非管理员任务 且 状态为 执行中的
QueryWrapper<QuartzJob> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().and(i -> {
i.notIn(QuartzJob::getDeptId,19)
.eq(QuartzJob::getIsPause,0);
});
List<QuartzJob> quartzJobs = jobMapper.selectList(queryWrapper);
try {
quartzJobs.forEach( quartzJob -> {
String params = quartzJob.getParams();
if(StrUtil.isNotBlank(params)){
JSONObject jsonParam = JSONObject.parseObject(params);
public void run2(){ log.info(">>> 环保账户: {}, 环保密码:{}, 授权人签名盘符:{}, 批准人签名盘符:{}, 公章签名盘符:{}, 部门编号:{}",
log.info("run2 执行成功"); jsonParam.getString("account"),jsonParam.getString("password"),
jsonParam.getString("snKey1"),jsonParam.getString("snKey2"),jsonParam.getString("snKey3"),jsonParam.getInteger("deptId"));
AssertUtil.isNotNull(jsonParam.get("account"),"缺少环保系统账户,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("password"),"缺少环保系统密码,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey1"),"缺少授权人签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey2"),"缺少批准人签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey3"),"缺少公章签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey4"),"缺少MA章签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName1"),"缺少授权人签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName2"),"缺少批准人签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName3"),"缺少公章签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName4"),"缺少MA章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("deptId"),"缺少部门id,自动任务执行终止!");
reportService.autoDownloadAndSignAndUploadReport(jsonParam.getString("account"),jsonParam.getString("password"),
jsonParam.getString("snKey1"),jsonParam.getString("snKey2"),jsonParam.getString("snKey3"),
jsonParam.getString("snName1"),jsonParam.getString("snName2"),jsonParam.getString("snName3")
,jsonParam.getString("snKey4"),jsonParam.getString("snName4"),jsonParam.getInteger("deptId"));
}
});
} catch (Exception e) {
log.error(e.getMessage());
}
log.info(">> ========================================== 本次自动签章任务执行完毕 ==========================================");*/
} }
} }
...@@ -32,6 +32,7 @@ import me.zhengjie.utils.StringUtils; ...@@ -32,6 +32,7 @@ import me.zhengjie.utils.StringUtils;
import me.zhengjie.utils.ThrowableUtil; import me.zhengjie.utils.ThrowableUtil;
import org.quartz.JobExecutionContext; import org.quartz.JobExecutionContext;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.scheduling.quartz.QuartzJobBean; import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.*; import java.util.*;
import java.util.concurrent.*; import java.util.concurrent.*;
...@@ -41,12 +42,10 @@ import java.util.concurrent.*; ...@@ -41,12 +42,10 @@ import java.util.concurrent.*;
* @author / * @author /
* @date 2019-01-07 * @date 2019-01-07
*/ */
//@Async @Async
@SuppressWarnings({"unchecked","all"}) @SuppressWarnings({"unchecked","all"})
public class ExecutionJob extends QuartzJobBean { public class ExecutionJob extends QuartzJobBean {
private final ThreadPoolTaskExecutor EXECUTOR = (ThreadPoolTaskExecutor) SpringContextHolder.getBean("quartzTaskExecutor");
/** 该处仅供参考 */
private final static ThreadPoolExecutor EXECUTOR = ThreadPoolExecutorUtil.getPoll();
@Override @Override
public void executeInternal(JobExecutionContext context) { public void executeInternal(JobExecutionContext context) {
...@@ -70,6 +69,7 @@ public class ExecutionJob extends QuartzJobBean { ...@@ -70,6 +69,7 @@ public class ExecutionJob extends QuartzJobBean {
// 执行任务 // 执行任务
System.out.println("--------------------------------------------------------------"); System.out.println("--------------------------------------------------------------");
System.out.println("任务开始执行,任务名称:" + quartzJob.getJobName()); System.out.println("任务开始执行,任务名称:" + quartzJob.getJobName());
System.out.println("-------------------当前线程池大小:" + EXECUTOR.getPoolSize() +",线程池最大数:" + EXECUTOR.getMaxPoolSize() +"---------------------");
QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(), QuartzRunnable task = new QuartzRunnable(quartzJob.getBeanName(), quartzJob.getMethodName(),
quartzJob.getParams()); quartzJob.getParams());
Future<?> future = EXECUTOR.submit(task); Future<?> future = EXECUTOR.submit(task);
...@@ -93,6 +93,7 @@ public class ExecutionJob extends QuartzJobBean { ...@@ -93,6 +93,7 @@ public class ExecutionJob extends QuartzJobBean {
if(StringUtils.isNotBlank(uuid)) { if(StringUtils.isNotBlank(uuid)) {
redisUtils.set(uuid, false); redisUtils.set(uuid, false);
} }
System.out.println("任务执行失败,任务名称:" + quartzJob.getJobName()); System.out.println("任务执行失败,任务名称:" + quartzJob.getJobName());
System.out.println("--------------------------------------------------------------"); System.out.println("--------------------------------------------------------------");
long times = System.currentTimeMillis() - startTime; long times = System.currentTimeMillis() - startTime;
......
...@@ -51,6 +51,7 @@ public class QuartzManage { ...@@ -51,6 +51,7 @@ public class QuartzManage {
.withSchedule(CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression())) .withSchedule(CronScheduleBuilder.cronSchedule(quartzJob.getCronExpression()))
.build(); .build();
cronTrigger.getJobDataMap().put(QuartzJob.JOB_KEY, quartzJob); cronTrigger.getJobDataMap().put(QuartzJob.JOB_KEY, quartzJob);
//重置启动时间 //重置启动时间
...@@ -63,6 +64,7 @@ public class QuartzManage { ...@@ -63,6 +64,7 @@ public class QuartzManage {
if (quartzJob.getIsPause()) { if (quartzJob.getIsPause()) {
pauseJob(quartzJob); pauseJob(quartzJob);
} }
} catch (Exception e){ } catch (Exception e){
log.error("创建定时任务失败", e); log.error("创建定时任务失败", e);
throw new BadRequestException("创建定时任务失败"); throw new BadRequestException("创建定时任务失败");
......
...@@ -23,26 +23,29 @@ public class MyConstants { ...@@ -23,26 +23,29 @@ public class MyConstants {
/** /**
* 身份证文件名后缀 * 身份证文件名后缀
*/ */
public static final String IDCARD_SUFFIX = "_sfz.jpg"; public static final String IDCARD_SUFFIX = "\\IMG_0000.jpg";
/** /**
* 检测申请表文件后缀 * 行驶证1文件后缀
*/ */
public static final String APPLY_SUFFIX = "_jcsqb.jpg"; public static final String DRIVER_SUFFIX1 = "\\IMG_0001.jpg";
/** /**
* 维修凭证文件后缀 * 行驶证2文件后缀
*/ */
public static final String CERTIFICATE_SUFFIX = "_wxpz.jpg"; public static final String DRIVER_SUFFIX2 = "\\IMG_0002.jpg";
/** /**
* 行驶证1文件后缀 * 检测申请表文件后缀
*/ */
public static final String DRIVER_SUFFIX1 = "_xsz1.jpg"; public static final String APPLY_SUFFIX = "\\IMG_0003.jpg";
/** /**
* 行驶证2文件后缀 * 维修凭证文件后缀
*/ */
public static final String DRIVER_SUFFIX2 = "_xsz2.jpg"; public static final String CERTIFICATE_SUFFIX = "\\IMG_0004.jpg";
} }
/*
* Copyright 2019-2020 Zheng Jie
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package me.zhengjie.modules.system.domain;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.persistence.Transient;
import javax.validation.constraints.NotBlank;
/**
* @author Zheng Jie
* @date 2019-01-07
*/
@Data
@TableName("sys_quartz_job")
public class QuartzJob{
public static final String JOB_KEY = "JOB_KEY";
@TableId(type = IdType.AUTO)
private Long jobId;
@TableField(exist = false)
@ApiModelProperty(value = "用于子任务唯一标识", hidden = true)
private String uuid;
@ApiModelProperty(value = "定时器名称")
private String jobName;
@NotBlank
@ApiModelProperty(value = "Bean名称")
private String beanName;
@ApiModelProperty(value = "方法名称")
private String methodName;
@ApiModelProperty(value = "参数")
private String params;
@ApiModelProperty(value = "cron表达式")
private String cronExpression;
@ApiModelProperty(value = "状态,暂时或启动")
private Boolean isPause = false;
@ApiModelProperty(value = "负责人")
private String personInCharge;
@ApiModelProperty(value = "报警邮箱")
private String email;
@ApiModelProperty(value = "子任务")
private String subTask;
@ApiModelProperty(value = "失败后暂停")
private Boolean pauseAfterFailure;
@ApiModelProperty(value = "备注")
private String description;
@ApiModelProperty("部门id")
private Long deptId;
}
...@@ -33,4 +33,8 @@ public class HtmlToPdfInterceptor extends Thread{ ...@@ -33,4 +33,8 @@ public class HtmlToPdfInterceptor extends Thread{
} }
} }
} }
package me.zhengjie.modules.system.repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import me.zhengjie.modules.system.domain.QuartzJob;
import org.springframework.stereotype.Repository;
/**
* @author Lander
*/
@Repository
public interface JobMapper extends BaseMapper<QuartzJob> {
}
...@@ -36,14 +36,9 @@ import org.springframework.security.core.context.SecurityContextHolder; ...@@ -36,14 +36,9 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder; import sun.misc.BASE64Encoder;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 检测报告service * 检测报告service
...@@ -71,6 +66,8 @@ public class ReportService { ...@@ -71,6 +66,8 @@ public class ReportService {
private static List<TestReportVo> reportVoList; private static List<TestReportVo> reportVoList;
private final TaskErrorMapper taskErrorMapper; private final TaskErrorMapper taskErrorMapper;
@Value("${uploadFilePath}")
private String uploadFilePath;
@Autowired @Autowired
public ReportService(UserMapper userMapper, DeptMapper deptMapper, CarReportUtil carReportUtil, ReportPdfMapper reportPdfMapper, SignatureService signatureService, RestTemplate restTemplate, TaskErrorMapper taskErrorMapper) { public ReportService(UserMapper userMapper, DeptMapper deptMapper, CarReportUtil carReportUtil, ReportPdfMapper reportPdfMapper, SignatureService signatureService, RestTemplate restTemplate, TaskErrorMapper taskErrorMapper) {
...@@ -123,7 +120,7 @@ public class ReportService { ...@@ -123,7 +120,7 @@ public class ReportService {
* 自动下载检测报告并签章上传 * 自动下载检测报告并签章上传
* *
*/ */
public synchronized void autoDownloadAndSignAndUploadReport(String account,String password,String snKey1,String snKey2,String snKey3, public void autoDownloadAndSignAndUploadReport(String account,String password,String snKey1,String snKey2,String snKey3,
String snName1,String snName2,String snName3,String snKey4,String snName4,Integer deptId){ String snName1,String snName2,String snName3,String snKey4,String snName4,Integer deptId){
...@@ -142,34 +139,38 @@ public class ReportService { ...@@ -142,34 +139,38 @@ public class ReportService {
//下载报告pdf 并写入数据库 //下载报告pdf 并写入数据库
reportPdfVo = new ReportPdfVo(); reportPdfVo = new ReportPdfVo();
String path = carReportUtil.downloadReport(account, password, reportDetailsReqVo); String path = carReportUtil.authDownloadReport(account, password, reportDetailsReqVo);
reportPdfVo.setPath(path);
reportPdfVo.setVehicleId(testReportVo.getVehicleID()); //如果下载成功
reportPdfVo.setUniqueString(testReportVo.getUniqueString()); if(StrUtil.isNotBlank(path)){
reportPdfVo.setReportName(testReportVo.getInspectionReportNo() + ".pdf");
reportPdfVo.setReportNum(testReportVo.getInspectionReportNo()); reportPdfVo.setPath(path);
reportPdfVo.setBusinessKey(StrUtil.isNotBlank(testReportVo.getBusinessKey())?testReportVo.getBusinessKey():testReportVo.getInspectionNum()); reportPdfVo.setVehicleId(testReportVo.getVehicleID());
reportPdfVo.setCreateTime(DateUtil.date()); reportPdfVo.setUniqueString(testReportVo.getUniqueString());
reportPdfVo.setCarNum(testReportVo.getVLPN()); reportPdfVo.setReportName(testReportVo.getInspectionReportNo() + ".pdf");
reportPdfVo.setReportNum(testReportVo.getInspectionReportNo());
reportPdfVo.setBusinessKey(StrUtil.isNotBlank(testReportVo.getBusinessKey())?testReportVo.getBusinessKey():testReportVo.getInspectionNum());
//根据snKey查询签章信息 reportPdfVo.setCreateTime(DateUtil.date());
SignatureVo signatureVo1 = signatureService.getBySnKeyAndSnName(snKey1,snName1,deptId); reportPdfVo.setCarNum(testReportVo.getVLPN());
AssertUtil.isNotNull(signatureVo1,"找不到snKey为: " + snKey1 + "的签名章");
SignatureVo signatureVo2 = signatureService.getBySnKeyAndSnName(snKey2,snName2,deptId);
AssertUtil.isNotNull(signatureVo2,"找不到snKey为: " + snKey2 + "的签名章"); //根据snKey查询签章信息
SignatureVo signatureVo3 = signatureService.getBySnKeyAndSnName(snKey3,snName3,deptId); SignatureVo signatureVo1 = signatureService.getBySnKeyAndSnName(snKey1,snName1,deptId);
AssertUtil.isNotNull(signatureVo3,"找不到snKey为: " + snKey3 + "的公章"); AssertUtil.isNotNull(signatureVo1,"找不到snKey为: " + snKey1 + "的签名章");
SignatureVo signatureVo4 = signatureService.getBySnKeyAndSnName(snKey4,snName4,deptId); SignatureVo signatureVo2 = signatureService.getBySnKeyAndSnName(snKey2,snName2,deptId);
AssertUtil.isNotNull(signatureVo3,"找不到snKey为: " + snKey4 + "的公章"); AssertUtil.isNotNull(signatureVo2,"找不到snKey为: " + snKey2 + "的签名章");
SignatureVo signatureVo3 = signatureService.getBySnKeyAndSnName(snKey3,snName3,deptId);
//进行签章 AssertUtil.isNotNull(signatureVo3,"找不到snKey为: " + snKey3 + "的公章");
CarSignatureReqVo carSignatureReqVo = new CarSignatureReqVo(); SignatureVo signatureVo4 = signatureService.getBySnKeyAndSnName(snKey4,snName4,deptId);
carSignatureReqVo.setReportPdfVo(reportPdfVo); AssertUtil.isNotNull(signatureVo3,"找不到snKey为: " + snKey4 + "的公章");
carSignatureReqVo.setSignature1(signatureVo1);
carSignatureReqVo.setSignature2(signatureVo2); //进行签章
carSignatureReqVo.setSignature3(signatureVo3); CarSignatureReqVo carSignatureReqVo = new CarSignatureReqVo();
carSignatureReqVo.setSignature4(signatureVo4); carSignatureReqVo.setReportPdfVo(reportPdfVo);
carSignatureReqVo.setSignature1(signatureVo1);
carSignatureReqVo.setSignature2(signatureVo2);
carSignatureReqVo.setSignature3(signatureVo3);
carSignatureReqVo.setSignature4(signatureVo4);
HttpEntity<CarSignatureReqVo> httpEntity = new HttpEntity<>(carSignatureReqVo); HttpEntity<CarSignatureReqVo> httpEntity = new HttpEntity<>(carSignatureReqVo);
ResponseEntity<ResultVo> responseEntity = this.restTemplate.exchange( ResponseEntity<ResultVo> responseEntity = this.restTemplate.exchange(
...@@ -189,20 +190,18 @@ public class ReportService { ...@@ -189,20 +190,18 @@ public class ReportService {
reportPdfVo.setPath(signatureReport.getPath()); reportPdfVo.setPath(signatureReport.getPath());
reportPdfVo.setReportName(signatureReport.getReportName()); reportPdfVo.setReportName(signatureReport.getReportName());
//上传文件 (连同检测报告全部文件)
Integer success = carReportUtil.autoUploadReportPost(account, password, reportPdfVo, signatureVo1.getDeptId()); Integer success = carReportUtil.autoUploadReportPost(account, password, reportPdfVo, signatureVo1.getDeptId());
//上传成功的文件小于2个则视为上传失败,清除下载记录
/*if(success < 2){
deleteReportPdf(signatureReport.getReportNum());
}*/
if(success >= 2){ if(success >= 2){
log.info(">> 成功自动上传:{} 个文件,本次上传任务完成!",success); log.info(">> 成功自动上传:{} 个文件,本次上传任务完成!",success);
//写入pdf下载记录 //写入pdf下载记录
reportPdfMapper.insert(reportPdfVo); reportPdfMapper.insert(reportPdfVo);
} }
} else {
log.error(">> 自动下载检测报告失败!");
}
} }
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -370,6 +369,10 @@ public class ReportService { ...@@ -370,6 +369,10 @@ public class ReportService {
reportPdfMapper.updateById(reportPdfVo); reportPdfMapper.updateById(reportPdfVo);
} }
if(!FileUtil.exist(uploadFilePath + detailsReqVo)){
FileUtil.mkdir(uploadFilePath + detailsReqVo);
}
return reportPdfVo; return reportPdfVo;
} }
} }
......
package me.zhengjie.modules.system.task;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.system.domain.QuartzJob;
import me.zhengjie.modules.system.repository.JobMapper;
import me.zhengjie.modules.system.service.ReportService;
import me.zhengjie.utils.AssertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
@Slf4j
@Component
public class Task {
@Autowired
private JobMapper jobMapper;
@Autowired
private ReportService reportService;
@Scheduled(initialDelay = 2000,fixedDelay = 20000)
public void run(){
log.info(">>> ================================ 开始自动爬取新的检测报告进行下载、签章、上传 ====================================");
//查询全部非管理员任务 且 状态为 执行中的
QueryWrapper<QuartzJob> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().and(i -> {
i.notIn(QuartzJob::getDeptId,19)
.eq(QuartzJob::getIsPause,0);
});
List<QuartzJob> quartzJobs = jobMapper.selectList(queryWrapper);
try {
quartzJobs.forEach( quartzJob -> {
AutoScanReportThread autoScanReportThread = new AutoScanReportThread();
autoScanReportThread.setParams(quartzJob.getParams());
autoScanReportThread.start();
});
} catch (Exception e) {
log.error(e.getMessage());
}
log.info(">> ========================================== 本次自动签章任务执行完毕 ==========================================");
}
/**
* 自动扫描线程
*/
class AutoScanReportThread extends Thread {
private String params;
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
@Override
public void run() {
if(StrUtil.isNotBlank(params)){
JSONObject jsonParam = JSONObject.parseObject(params);
log.info(">>> 环保账户: {}, 环保密码:{}, 授权人签名盘符:{}, 批准人签名盘符:{}, 公章签名盘符:{}, 部门编号:{}",
jsonParam.getString("account"),jsonParam.getString("password"),
jsonParam.getString("snKey1"),jsonParam.getString("snKey2"),jsonParam.getString("snKey3"),jsonParam.getInteger("deptId"));
AssertUtil.isNotNull(jsonParam.get("account"),"缺少环保系统账户,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("password"),"缺少环保系统密码,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey1"),"缺少授权人签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey2"),"缺少批准人签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey3"),"缺少公章签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snKey4"),"缺少MA章签名盘符,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName1"),"缺少授权人签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName2"),"缺少批准人签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName3"),"缺少公章签章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("snName4"),"缺少MA章名称,自动任务执行终止!");
AssertUtil.isNotNull(jsonParam.get("deptId"),"缺少部门id,自动任务执行终止!");
reportService.autoDownloadAndSignAndUploadReport(jsonParam.getString("account"),jsonParam.getString("password"),
jsonParam.getString("snKey1"),jsonParam.getString("snKey2"),jsonParam.getString("snKey3"),
jsonParam.getString("snName1"),jsonParam.getString("snName2"),jsonParam.getString("snName3")
,jsonParam.getString("snKey4"),jsonParam.getString("snName4"),jsonParam.getInteger("deptId"));
}
}
}
}
...@@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j; ...@@ -4,7 +4,7 @@ import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.system.interceptor.HtmlToPdfInterceptor; import me.zhengjie.modules.system.interceptor.HtmlToPdfInterceptor;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.File; import java.io.*;
/** /**
* @author Lander * @author Lander
...@@ -14,6 +14,19 @@ import java.io.File; ...@@ -14,6 +14,19 @@ import java.io.File;
public class PdfUtil { public class PdfUtil {
public static void run(InputStream is){
try{
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader br = new BufferedReader(isr);
String line = null;
while ((line = br.readLine()) != null) {
log.info(">> HTML转PDF INFO :{}",line.toString()); //输出内容
}
}catch (IOException e){
e.printStackTrace();
}
}
/** /**
* html转pdf * html转pdf
...@@ -54,9 +67,9 @@ public class PdfUtil { ...@@ -54,9 +67,9 @@ public class PdfUtil {
try { try {
Process proc = Runtime.getRuntime().exec(cmd.toString()); Process proc = Runtime.getRuntime().exec(cmd.toString());
HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream()); HtmlToPdfInterceptor error = new HtmlToPdfInterceptor(proc.getErrorStream());
HtmlToPdfInterceptor output = new HtmlToPdfInterceptor(proc.getInputStream()); HtmlToPdfInterceptor out = new HtmlToPdfInterceptor(proc.getInputStream());
error.start(); error.start();
output.start(); out.start();
proc.waitFor(); proc.waitFor();
} catch (Exception e) { } catch (Exception e) {
result = false; result = false;
......
...@@ -35,16 +35,17 @@ spring: ...@@ -35,16 +35,17 @@ spring:
max-file-size: 100MB max-file-size: 100MB
max-request-size: 100MB #最大请求文件的大小 max-request-size: 100MB #最大请求文件的大小
task: task:
pool: pool:
# 核心线程池大小 # 核心线程池大小
core-pool-size: 10 core-pool-size: 25
# 最大线程数 # 最大线程数
max-pool-size: 30 max-pool-size: 30
# 活跃时间 # 活跃时间
keep-alive-seconds: 60 keep-alive-seconds: 50
# 队列容量 # 队列容量
queue-capacity: 50 queue-capacity: 30
#七牛云 #七牛云
qiniu: qiniu:
......
...@@ -22,7 +22,7 @@ ...@@ -22,7 +22,7 @@
<appender-ref ref="console" /> <appender-ref ref="console" />
</logger> </logger>
<logger name="jdbc.resultset" level="ERROR" additivity="false"> <logger name="jdbc.resultset" level="INFO" additivity="false">
<appender-ref ref="console" /> <appender-ref ref="console" />
</logger> </logger>
......
...@@ -166,8 +166,8 @@ class BackendApplicationTests { ...@@ -166,8 +166,8 @@ class BackendApplicationTests {
}); });
log.info(">> 检测报告下载完成!"); log.info(">> 检测报告下载完成!");
frameDriver.close(); frameDriver.quit();
driver.close(); driver.quit();
} }
...@@ -315,8 +315,8 @@ class BackendApplicationTests { ...@@ -315,8 +315,8 @@ class BackendApplicationTests {
//点击上传已勾选的文件 //点击上传已勾选的文件
frameDriver.findElement(new By.ById("Button3")).click(); frameDriver.findElement(new By.ById("Button3")).click();
frameDriver.close(); frameDriver.quit();
driver.close(); driver.quit();
log.info(">> ======== 资料已上传完成 =========== <<"); log.info(">> ======== 资料已上传完成 =========== <<");
} }
......
package me.zhengjie; package me.zhengjie;
import cn.hutool.core.img.Img;
import cn.hutool.core.io.FileUtil;
import me.zhengjie.modules.security.service.UserDetailsServiceImpl; import me.zhengjie.modules.security.service.UserDetailsServiceImpl;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
...@@ -16,38 +18,20 @@ public class LoginCacheTest { ...@@ -16,38 +18,20 @@ public class LoginCacheTest {
@Resource(name = "userDetailsService") @Resource(name = "userDetailsService")
private UserDetailsServiceImpl userDetailsService; private UserDetailsServiceImpl userDetailsService;
public synchronized void testSyncronized(Integer i){
System.out.println(i + "进入同步方法");
}
public void testSync(Integer i){ @Test
System.out.println(i + "开始异步执行同步方法。。。"); public void testCreateFileDirect(){
if(i ==2){ String dir = "666";
try { if(!FileUtil.exist("C:\\reptiles\\uploadPicture\\" + dir)){
Thread.sleep(10000); FileUtil.mkdir("C:\\reptiles\\uploadPicture\\" + dir);
} catch (InterruptedException e) { }
e.printStackTrace();
}
}
testSyncronized(i);
} }
@Test @Test
public void testCache() { public void testCache() {
long start1 = System.currentTimeMillis(); Img.from(FileUtil.file("C:\\Users\\Lander.LAPTOP-6VMQUJS1\\Desktop\\test-report\\桂BFF605_xsz2.jpg"))
int size = 10000; .setQuality(0.8)//压缩比率
for (int i = 0; i < size; i++) { .write(FileUtil.file("C:\\Users\\Lander.LAPTOP-6VMQUJS1\\Desktop\\test-report\\桂BFF605_xsz2.jpg"));
userDetailsService.loadUserByUsername("admin");
}
long end1 = System.currentTimeMillis();
//关闭缓存
userDetailsService.setEnableCache(false);
long start2 = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
userDetailsService.loadUserByUsername("admin");
}
long end2 = System.currentTimeMillis();
System.out.print("使用缓存:" + (end1 - start1) + "毫秒\n 不使用缓存:" + (end2 - start2) + "毫秒");
} }
@Test @Test
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment