该方法为网上查询,感觉可行,并未真正尝试。
主线程:
package com.swagger.demo.service;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.swagger.demo.config.SpringJobBeanFactory;
import com.swagger.demo.mapper.DeadManMapper;
import com.swagger.demo.model.entity.DeadManExcelData;
import com.swagger.demo.thread.DeadManThread;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author zrc
* @version 1.0
* @description: TODO 最新入狱名单导入监听器
*
* @date 2022/5/30 15:56
*/
@Service
@Slf4j
@Component
public class DeadManExcelListener extends AnalysisEventListener<DeadManExcelData> {
/**
* 多线程保存集合,使用线程安全集合
*/
private List<DeadManExcelData> list = Collections.synchronizedList(new ArrayList<>());
/**
* 创建线程池必要参数
*/
private static final int CORE_POOL_SIZE = 10; // 核心线程数
private static final int MAX_POOL_SIZE = 100; // 最大线程数
private static final int QUEUE_CAPACITY = 100; // 队列大小
private static final Long KEEP_ALIVE_TIME = 1L; // 存活时间
public List<DeadManExcelData> getData(){
return list;
}
public DeadManExcelListener(){
}
public void setData(List<DeadManExcelData> deadManExcelDataList){
this.list = deadManExcelDataList;
}
@Override
public void invoke(DeadManExcelData deadManExcelData, AnalysisContext analysisContext) {
if(deadManExcelData!=null){
list.add(deadManExcelData);
}
}
/**
* 多线程方式保存
* @param analysisContext
*/
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
log.info("解析结束,开始插入数据");
// 创建线程池
ExecutorService executor = new ThreadPoolExecutor(CORE_POOL_SIZE,
MAX_POOL_SIZE,
KEEP_ALIVE_TIME,
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(QUEUE_CAPACITY),
new ThreadPoolExecutor.CallerRunsPolicy());
// 指定每个线程需要处理的导入数量,假设每个线程处理15000条,注意配合上面线程池的大小
int singleThreadDealCount = 15000;
// 根据假设每个线程需要处理的数量以及总数,计算需要提交到线程池的线程数量
int threadSize=(list.size()/singleThreadDealCount)+1;
// 计算需要导入的数据总数,用于拆分时线程需要处理数据时使用
int rowSize = list.size() + 1;
// 测试开始时间
long startTime = System.currentTimeMillis();
// 申明该线程需要处理数据的开始位置
int startPosition = 0;
// 申明该线程需要处理数据的结束位置
int endPosition = 0;
// 为了让每个线程执行完后回到当前线程,使用CountDownLatch,值为线程数,每次线程执行完就会执行countDown方法减1,为0后回到主线程,也就是当前线程执行后续的代码
CountDownLatch count = new CountDownLatch(threadSize);
// 用来控制主线程回到子线程
CountDownLatch mainCount = new CountDownLatch(1);
// 用来控制最终回到主线程
CountDownLatch endCount = new CountDownLatch(threadSize);
// 用来存放子线程的处理结果,若出错就保存一个false
CopyOnWriteArrayList<Boolean> sonResult = new CopyOnWriteArrayList<Boolean>();
// 使用线程安全的对象存储,保存主线程最后总的判断结果,是提交还是回滚
AtomicBoolean ifSubmit = new AtomicBoolean(true);
// 计算每个线程要处理的数据
for(int i=0;i<threadSize;i++){
// 如果是最后一个线程,为保证程序不发生空指针异常,特殊判断结束位置
if((i+1)==threadSize){
// 计算开始位置
startPosition = (i * singleThreadDealCount);
// 当前线程为划分的最后一个线程,则取总数据的最后为此线程的结束位置
endPosition = rowSize-1;
}else{
// 计算开始位置
startPosition = (i * singleThreadDealCount);
// 计算结束位置
endPosition = (i + 1) * singleThreadDealCount;
}
DeadManMapper deadManMapper = SpringJobBeanFactory.getBean(DeadManMapper.class);
DeadManThread thread = new DeadManThread(count,deadManMapper,list,startPosition,endPosition
,sonResult,mainCount,ifSubmit,endCount);
executor.execute(thread);
}
try {
count.await();
for (Boolean resp : sonResult) {
if (!resp) {
// 只要有一个子线程出异常,就设置最终结果为回滚
log.info("主线程:有线程执行失败,所有线程需要回滚");
ifSubmit.set(false);
break;
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}finally {
// 回到子线程处理回滚或者提交事务
mainCount.countDown();
}
try {
endCount.await();
// 逻辑处理完,关闭线程池
executor.shutdown();
long endTime = System.currentTimeMillis();
log.info("总耗时:"+(endTime-startTime));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
子线程:
package com.swagger.demo.thread;
import com.swagger.demo.config.SpringJobBeanFactory;
import com.swagger.demo.mapper.DeadManMapper;
import com.swagger.demo.model.entity.DeadMan;
import com.swagger.demo.model.entity.DeadManExcelData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Component;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* @author zrc
* @version 1.0
* @description TODO
* @date 2022/7/22 15:40
*/
@Component
@Slf4j
public class DeadManThread implements Runnable{
/**
* 当前线程需要处理的总数据中的开始位置
*/
private int startPosition;
/**
* 当前线程需要处理的总数据中的结束位置
*/
private int endPosition;
/**
* 需要处理的未拆分之前的全部数据
*/
private List<DeadManExcelData> list = Collections.synchronizedList(new ArrayList<>());
/**
* 记录子线程第一次执行是否完成
*/
private CountDownLatch count;
private DeadManMapper deadManMapper;
/**
* 保存每个线程的执行结果
*/
private CopyOnWriteArrayList<Boolean> sonResult;
/**
* 记录主线程是否执行过判断每个线程的执行结果这个操作
*/
private CountDownLatch mainCount;
/**
* 记录主线程对每个线程的执行结果的判断
*/
private AtomicBoolean ifSubmit;
/**
* 声明该子线程的事务管理器
*/
private DataSourceTransactionManager dataSourceTransactionManager;
/**
* 声明该线程事务的状态
*/
private TransactionStatus status;
/**
* 记录子线程第二次执行是否完成
*/
private CountDownLatch endCount;
public DeadManThread() {
}
public DeadManThread(CountDownLatch count, DeadManMapper deadManMapper, List<DeadManExcelData> list
, int startPosition, int endPosition, CopyOnWriteArrayList<Boolean> sonResult,CountDownLatch mainCount
,AtomicBoolean ifSubmit,CountDownLatch endCount) {
this.startPosition = startPosition;
this.endPosition = endPosition;
this.deadManMapper = deadManMapper;
this.list = list;
this.count = count;
this.sonResult = sonResult;
this.mainCount = mainCount;
this.ifSubmit = ifSubmit;
this.endCount = endCount;
}
@Override
public void run() {
try{
dataSourceTransactionManager = SpringJobBeanFactory.getBean(DataSourceTransactionManager.class);
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
status = dataSourceTransactionManager.getTransaction(def);
if(Thread.currentThread().getName().contains("3")){
throw new RuntimeException("线程3出问题了");
}
List<DeadMan> deadManList = new ArrayList<>();
List<DeadManExcelData> newList = list.subList(startPosition, endPosition);
// 将EasyExcel对象和实体类对象进行一个转换
for (DeadManExcelData deadManExcelData : newList) {
DeadMan deadMan = new DeadMan();
BeanUtils.copyProperties(deadManExcelData, deadMan);
deadManList.add(deadMan);
}
// 批量新增
deadManMapper.insertBatchSomeColumn(deadManList);
sonResult.add(true);
} catch (Exception e) {
e.printStackTrace();
sonResult.add(false);
} finally {
// 当一个线程执行完了计数要减一不然这个线程会被一直挂起
count.countDown();
try {
log.info(Thread.currentThread().getName() + ":准备就绪,等待其他线程结果,判断是否事务提交");
mainCount.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (ifSubmit.get()) {
dataSourceTransactionManager.commit(status);
log.info(Thread.currentThread().getName() + ":事务提交");
} else {
dataSourceTransactionManager.rollback(status);
log.info(Thread.currentThread().getName() + ":事务回滚");
}
// 执行完所有逻辑,等待主线程执行
endCount.countDown();
}
}
}