Skip to content

Commit f9bb8cf

Browse files
committed
修复定时任务执行失败后还继续执行的bug,优化Redis发生异常时,程序正常执行,优化Excel大数据导出,优化QueryHelp,其他细节优化
1 parent a481b16 commit f9bb8cf

File tree

25 files changed

+137
-87
lines changed

25 files changed

+137
-87
lines changed

eladmin-common/src/main/java/me/zhengjie/redis/RedisConfig.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
import com.alibaba.fastjson.JSON;
44
import com.alibaba.fastjson.parser.ParserConfig;
55
import lombok.extern.slf4j.Slf4j;
6-
import me.zhengjie.utils.StringUtils;
7-
import org.springframework.beans.factory.annotation.Value;
86
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
97
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
108
import org.springframework.boot.autoconfigure.data.redis.RedisProperties;
119
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.cache.Cache;
1211
import org.springframework.cache.annotation.CachingConfigurerSupport;
1312
import org.springframework.cache.annotation.EnableCaching;
13+
import org.springframework.cache.interceptor.CacheErrorHandler;
1414
import org.springframework.cache.interceptor.KeyGenerator;
1515
import org.springframework.context.annotation.Bean;
1616
import org.springframework.context.annotation.Configuration;
@@ -74,8 +74,8 @@ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisC
7474
}
7575

7676
/**
77-
* 自定义缓存key生成策略
78-
* 使用方法 @Cacheable(keyGenerator="keyGenerator")
77+
* 自定义缓存key生成策略,默认将使用该策略
78+
* 使用方法 @Cacheable
7979
* @return
8080
*/
8181
@Bean
@@ -91,4 +91,34 @@ public KeyGenerator keyGenerator() {
9191
return sb.toString();
9292
};
9393
}
94+
95+
@Bean
96+
@Override
97+
public CacheErrorHandler errorHandler() {
98+
// 异常处理,当Redis发生异常时,打印日志,但是程序正常走
99+
log.info("初始化 -> [{}]", "Redis CacheErrorHandler");
100+
CacheErrorHandler cacheErrorHandler = new CacheErrorHandler() {
101+
@Override
102+
public void handleCacheGetError(RuntimeException e, Cache cache, Object key) {
103+
log.error("Redis occur handleCacheGetError:key -> [{}]", key, e);
104+
}
105+
106+
@Override
107+
public void handleCachePutError(RuntimeException e, Cache cache, Object key, Object value) {
108+
log.error("Redis occur handleCachePutError:key -> [{}];value -> [{}]", key, value, e);
109+
}
110+
111+
@Override
112+
public void handleCacheEvictError(RuntimeException e, Cache cache, Object key) {
113+
log.error("Redis occur handleCacheEvictError:key -> [{}]", key, e);
114+
}
115+
116+
@Override
117+
public void handleCacheClearError(RuntimeException e, Cache cache) {
118+
log.error("Redis occur handleCacheClearError:", e);
119+
}
120+
};
121+
return cacheErrorHandler;
122+
}
123+
94124
}

eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import cn.hutool.core.codec.Base64;
44
import cn.hutool.core.io.IoUtil;
55
import cn.hutool.core.util.IdUtil;
6+
import cn.hutool.poi.excel.BigExcelWriter;
67
import cn.hutool.poi.excel.ExcelUtil;
78
import cn.hutool.poi.excel.ExcelWriter;
89
import me.zhengjie.exception.BadRequestException;
@@ -197,24 +198,20 @@ public static String fileToBase64(File file) throws Exception {
197198
* @return
198199
* @throws Exception
199200
*/
200-
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response){
201-
// 通过工具类创建writer
202-
ExcelWriter writer = ExcelUtil.getWriter();
201+
public static void downloadExcel(List<Map<String, Object>> list, HttpServletResponse response) throws IOException {
202+
String tempPath =System.getProperty("java.io.tmpdir") + IdUtil.fastSimpleUUID() + ".xlsx";
203+
File file = new File(tempPath);
204+
BigExcelWriter writer= ExcelUtil.getBigWriter(file);
203205
// 一次性写出内容,使用默认样式,强制输出标题
204206
writer.write(list, true);
205207
//response为HttpServletResponse对象
206-
response.setContentType("application/vnd.ms-excel;charset=utf-8");
208+
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8");
207209
//test.xls是弹出下载对话框的文件名,不能为中文,中文请自行编码
208-
response.setHeader("Content-Disposition","attachment;filename=file.xls");
209-
ServletOutputStream out= null;
210-
try {
211-
out = response.getOutputStream();
212-
} catch (IOException e) {
213-
e.printStackTrace();
214-
}
210+
response.setHeader("Content-Disposition","attachment;filename=file.xlsx");
211+
ServletOutputStream out=response.getOutputStream();
212+
// 终止后删除临时文件
213+
file.deleteOnExit();
215214
writer.flush(out, true);
216-
// 关闭writer,释放内存
217-
writer.close();
218215
//此处记得关闭输出Servlet流
219216
IoUtil.close(out);
220217
}

eladmin-common/src/main/java/me/zhengjie/utils/PageUtil.java

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package me.zhengjie.utils;
22

33
import org.springframework.data.domain.Page;
4-
import java.util.ArrayList;
5-
import java.util.HashMap;
6-
import java.util.List;
7-
import java.util.Map;
4+
import java.util.*;
85

96
/**
107
* 分页工具
@@ -39,11 +36,9 @@ public static List toPage(int page, int size , List list) {
3936
* @return
4037
*/
4138
public static Map toPage(Page page) {
42-
Map map = new HashMap();
43-
39+
Map<String,Object> map = new LinkedHashMap<>(2);
4440
map.put("content",page.getContent());
4541
map.put("totalElements",page.getTotalElements());
46-
4742
return map;
4843
}
4944

@@ -53,8 +48,7 @@ public static Map toPage(Page page) {
5348
* @return
5449
*/
5550
public static Map toPage(Object object, Object totalElements) {
56-
Map map = new HashMap();
57-
51+
Map<String,Object> map = new LinkedHashMap<>(2);
5852
map.put("content",object);
5953
map.put("totalElements",totalElements);
6054

eladmin-common/src/main/java/me/zhengjie/utils/QueryHelp.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuild
4141
String attributeName = isBlank(propName) ? field.getName() : propName;
4242
Class<?> fieldType = field.getType();
4343
Object val = field.get(query);
44-
if (ObjectUtil.isNull(val)) {
44+
if (ObjectUtil.isNull(val) || "".equals(val)) {
4545
continue;
4646
}
4747
Join join = null;
@@ -58,13 +58,24 @@ public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuild
5858
continue;
5959
}
6060
if (ObjectUtil.isNotEmpty(joinName)) {
61-
switch (q.join()) {
62-
case LEFT:
63-
join = root.join(joinName, JoinType.LEFT);
64-
break;
65-
case RIGHT:
66-
join = root.join(joinName, JoinType.RIGHT);
67-
break;
61+
String[] joinNames = joinName.split(">");
62+
for (String name : joinNames) {
63+
switch (q.join()) {
64+
case LEFT:
65+
if(ObjectUtil.isNotEmpty(join)){
66+
join = join.join(name, JoinType.LEFT);
67+
} else {
68+
join = root.join(name, JoinType.LEFT);
69+
}
70+
break;
71+
case RIGHT:
72+
if(ObjectUtil.isNotEmpty(join)){
73+
join = join.join(name, JoinType.RIGHT);
74+
} else {
75+
join = root.join(name, JoinType.RIGHT);
76+
}
77+
break;
78+
}
6879
}
6980
}
7081
switch (q.type()) {

eladmin-system/src/main/java/me/zhengjie/modules/quartz/service/QuartzJobService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface QuartzJobService {
2121
* @param pageable
2222
* @return
2323
*/
24-
@Cacheable(keyGenerator = "keyGenerator")
24+
@Cacheable
2525
Object queryAll(JobQueryCriteria criteria, Pageable pageable);
2626

2727
/**

eladmin-system/src/main/java/me/zhengjie/modules/quartz/task/TestTask.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package me.zhengjie.modules.quartz.task;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import me.zhengjie.exception.BadRequestException;
45
import org.springframework.stereotype.Component;
56

67
/**

eladmin-system/src/main/java/me/zhengjie/modules/quartz/utils/ExecutionJob.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public class ExecutionJob extends QuartzJobBean {
2525

2626
private Logger logger = LoggerFactory.getLogger(this.getClass());
2727

28+
// 建议自定义线程池实现方式,该处仅供参考
2829
private ExecutorService executorService = Executors.newSingleThreadExecutor();
2930

3031
@Override
@@ -61,8 +62,7 @@ protected void executeInternal(JobExecutionContext context) {
6162
// 任务状态 0:成功 1:失败
6263
log.setIsSuccess(false);
6364
log.setExceptionDetail(ThrowableUtil.getStackTrace(e));
64-
//出错就暂停任务
65-
quartzManage.pauseJob(quartzJob);
65+
quartzJob.setIsPause(false);
6666
//更新状态
6767
quartzJobService.updateIsPause(quartzJob);
6868
} finally {
Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package me.zhengjie.modules.quartz.utils;
22

33
import lombok.extern.slf4j.Slf4j;
4+
import me.zhengjie.exception.BadRequestException;
45
import me.zhengjie.utils.SpringContextHolder;
56
import org.apache.commons.lang3.StringUtils;
67
import org.springframework.util.ReflectionUtils;
78
import java.lang.reflect.Method;
9+
import java.util.concurrent.Callable;
810

911
/**
1012
* 执行定时任务
1113
* @author
1214
*/
1315
@Slf4j
14-
public class QuartzRunnable implements Runnable {
16+
public class QuartzRunnable implements Callable {
1517

1618
private Object target;
1719
private Method method;
@@ -30,17 +32,13 @@ public class QuartzRunnable implements Runnable {
3032
}
3133

3234
@Override
33-
public void run() {
34-
try {
35-
ReflectionUtils.makeAccessible(method);
36-
if (StringUtils.isNotBlank(params)) {
37-
method.invoke(target, params);
38-
} else {
39-
method.invoke(target);
40-
}
41-
} catch (Exception e) {
42-
log.error("定时任务执行失败",e);
35+
public Object call() throws Exception {
36+
ReflectionUtils.makeAccessible(method);
37+
if (StringUtils.isNotBlank(params)) {
38+
method.invoke(target, params);
39+
} else {
40+
method.invoke(target);
4341
}
42+
return null;
4443
}
45-
4644
}

eladmin-system/src/main/java/me/zhengjie/modules/system/rest/UserController.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.web.bind.annotation.*;
2828
import org.springframework.web.multipart.MultipartFile;
2929
import javax.servlet.http.HttpServletResponse;
30+
import java.io.IOException;
3031
import java.util.*;
3132
import java.util.stream.Collectors;
3233

@@ -59,7 +60,7 @@ public class UserController {
5960
@Log("导出用户数据")
6061
@GetMapping(value = "/users/download")
6162
@PreAuthorize("hasAnyRole('ADMIN','USER_ALL','USER_SELECT')")
62-
public void update(HttpServletResponse response, UserQueryCriteria criteria){
63+
public void update(HttpServletResponse response, UserQueryCriteria criteria) throws IOException {
6364
userService.download(userService.queryAll(criteria), response);
6465
}
6566

eladmin-system/src/main/java/me/zhengjie/modules/system/service/DeptService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public interface DeptService {
2222
* @param criteria
2323
* @return
2424
*/
25-
@Cacheable(keyGenerator = "keyGenerator")
25+
@Cacheable
2626
List<DeptDTO> queryAll(DeptQueryCriteria criteria);
2727

2828
/**
@@ -60,15 +60,15 @@ public interface DeptService {
6060
* @param deptDTOS
6161
* @return
6262
*/
63-
@Cacheable(keyGenerator = "keyGenerator")
63+
@Cacheable
6464
Object buildTree(List<DeptDTO> deptDTOS);
6565

6666
/**
6767
* findByPid
6868
* @param pid
6969
* @return
7070
*/
71-
@Cacheable(keyGenerator = "keyGenerator")
71+
@Cacheable
7272
List<Dept> findByPid(long pid);
7373

7474
Set<Dept> findByRoleIds(Long id);

0 commit comments

Comments
 (0)