Menma
Menma
发布于 2023-06-02 / 19 阅读 / 0 评论 / 0 点赞

excel导出异步处理遇到的问题

excel导出异步处理遇到的问题

1.dto vo都涉及到时区转换的问题

一开始的想法是通过RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();在线程之间进行上下文传递。 发现在dto中获取时区信息String timeZone = (String) BaneksRequestContext.getAttribute("timeZone");在主线程中是没问题的; 但是在子线程中 当请求完成(SCOPE_REQUEST)后,AbstractRequestAttributes将执行requestCompleted()方法,导致requestActive属性更改为false;随后在子线程中再去使 用上下文requestAttributes.getAttribute(name, RequestAttributes.SCOPE_REQUEST)

例如mybatis判断<if test="xxxDto.startTime != null and xxto.startTime != ''">

调用get方法,就会报错Cannot ask for request attribute - request is not active anymore!,因为此时scope为SCOPE_REQUESTrequestActive属为false。 ​ 2.解决方案 ​ 将子线程dto vo涉及到时区转化(需要用到上下文的),手动set赋值(放在一个属性中去存储或者其他地方如redis),不用getAttribute()方法。

requestCompleted

[getAttribute]问题2:存入redis序列化和发序列化的问题,待补充

1.字符串格式 =》json 读取

2.二进制格式 =》使用文件相关的redisTemplate

问题3:HttpMessageNotWritableException 转换出错

1.HttpServletResponse 会在请求结束后自动返回,文件格式的返回头是`application/octet-stream;charset=UTF-8` ,若是用ResponseResul封装或者Object作为返回值都会报错(使用了`@ResponseBody`,由`HttpMessageConverter`处理): `No converter for [class com.panda.bankman.model.common.ResponseResult] with preset Content-Type 'application/octet-stream;charset=UTF-8'`

2.解决:使用HttpServletRespons手动构造返回体

		response.setContentType("application/json;charset=UTF-8");  
		// 获取响应输出流  
		PrintWriter writer = response.getWriter();  
		// 构建 JSON 字符串  
		String json = "{\n" +  
		" \"code\":500,\n" +  
		" \"msg\":\"访问过于频繁,请稍后重试...\"\n" +  
		"}";  
		// 设置响应体的内容  
		writer.println(json);  
		// 关闭输出流  
		writer.close();

评论