添加一个类ThreadPoolConfig.java

package com.cjcx.inter.framework.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor; @Configuration
@EnableAsync
public class ThreadPoolConfig {
/** Set the ThreadPoolExecutor's core pool size. */
private int corePoolSize = 3;
/** Set the ThreadPoolExecutor's maximum pool size. */
private int maxPoolSize = 7;
/** Set the capacity for the ThreadPoolExecutor's BlockingQueue. */
private int queueCapacity = 5; @Bean
public Executor interfaceAsync() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("interface-"); // rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}

方法调用

public boolean doSendTask(ShoppingReceiptContentDto dto) {
boolean bool = false;
logger.info("单号:{}, ====开始上传数据", dto.getOrderNum());
try {
Future<HashMap<String, Object>> future = testAync(dto);
long s = System.currentTimeMillis();
boolean flag = true;
while (flag) {
//异步任务完成并且未被取消,则获取返回的结果
if (future.isDone() && !future.isCancelled()) {
HashMap<String, Object> futureResult = future.get();
logger.info("单号:{}, ====上传的结果是:{}", dto.getOrderNum(), futureResult.get("errorCode"));
Integer errorCode = Integer.parseInt(futureResult.get("errorCode").toString());
bool = errorCode == 0 ? true : false;
flag = false;
} //如果3秒内没有响应,取消任务
if (System.currentTimeMillis() - s > 5000) {
logger.info("单号:{}, ====上传超时,5秒钟内K11服务器无返回", dto.getOrderNum());
future.cancel(true);
bool = false;
flag = false;
}
}
} catch (Exception e) {
e.printStackTrace();
}
logger.info("单号:{}, ====结束, 上传{}", dto.getOrderNum(), (bool ? "成功" : "失败"));
return bool;
}

@Async("interfaceAsync")
public Future<HashMap<String, Object>> testAync(ShoppingReceiptContentDto dto) throws InterruptedException {
HashMap<String, Object> map = new HashMap<>(); String k11Response = "";
try {
//处理数据map.put("errorCode", 0); //测试先改为成功
} catch (Exception e) {
e.printStackTrace();
map.put("errorCode", -10001);
}
return new AsyncResult<>(map);
}

@Aync 内部实现也就是FutureTask

最新文章

  1. [高性能MYSQL 读后随笔] 关于事务的隔离级别(一)
  2. Java Basic Exception
  3. wind.onload和$(document).ready()的区别例示
  4. ASP.NET MVC 5 一 入门
  5. hdu 4622 **
  6. Mediator
  7. asp.net 生成PDF方法
  8. Oracle备忘录
  9. 关于DocumentCompleted事件
  10. CentOS6.5下Mysql数据库的安装与配置
  11. 总结OpenWrt系统基本操作方法
  12. css常用文本属性
  13. Reflection and array
  14. [TCP/IP] 传输层-TCP和UDP的使用场景
  15. 在c:forEach与s:iterator里面使用if标签判断当前位置是否为2的倍数
  16. [转]docker之Dockerfile实践
  17. “段错误(segment fault)”、“非法操作,该内存地址不能read/write” 非法指针解引用造成的错误。
  18. java-权限修饰符的区别
  19. ArcGIS 10安装及破解
  20. 4. 深入 Python 流程控制

热门文章

  1. vue.js-列表分页
  2. SharePoint开发 - TimerJob简单实例讲解
  3. Haproxy的负载均衡和高可用配置
  4. opencv:基于颜色空间的肤色检测方法
  5. 最新ICE源码编译安装
  6. Element-ui实现loading的局部刷新
  7. latex的使用
  8. jenkins配置git
  9. 【转】把VS的智能提示快捷键改成Eclipse的习惯
  10. 阿里云 搭建Git服务器