Commit e054929f authored by lzz's avatar lzz

循环清洗

parent eb2f3726
......@@ -71,5 +71,9 @@
<artifactId>spring-data-commons</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -14,7 +14,7 @@ import java.util.concurrent.TimeUnit;
public class ThreadPoolConfiguration {
@Bean(name = "defaultThreadPoolExecutor", destroyMethod = "shutdown")
public ThreadPoolExecutor systemCheckPoolExecutorService() {
return new ThreadPoolExecutor(5, 20, 60, TimeUnit.SECONDS,
return new ThreadPoolExecutor(2, 5, 60, TimeUnit.SECONDS,
new LinkedBlockingQueue<Runnable>(10000));
}
}
package com.ruoyi.system.service.impl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.domain.ActHistoryRoleTypeCodeModel;
import com.ruoyi.system.domain.ActReadHistoryModel;
import com.ruoyi.system.mapper.ActReadDataMapper;
import com.ruoyi.system.service.ActReadDataService;
import com.ruoyi.system.service.impl.Conversion.LkSpecialMethod;
import com.ruoyi.system.service.impl.Conversion.MineConver;
import com.ruoyi.system.service.impl.Conversion.OtherMethod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.regex.Pattern;
/**
......@@ -20,6 +15,9 @@ import java.util.regex.Pattern;
*/
@Service
public class ActReadDataServiceImpl implements ActReadDataService {
@Autowired
private MineConver mineConver;
private static ActReadDataMapper actReadDataMapper;
public ActReadDataServiceImpl(ActReadDataMapper actReadDataMapper) {
......@@ -30,6 +28,33 @@ public class ActReadDataServiceImpl implements ActReadDataService {
public static final Pattern PATTERN = Pattern.compile("\\d+");
@Override
public AjaxResult read(String mineId) {
//获取未清洗数据条数
int count = actReadDataMapper.selectCleanToOne(mineId);
if (count <= 0) {
return new AjaxResult(500, "数据库没有:" + mineId + "数据,无法清洗");
} else {
//开启多线程,每次开启5个线程,执行完后
//lambda表达式,开启线程
for (int i = 0; i <count; i++) {
Future<String> future = mineConver.readToId(mineId ,i);
try {
// 尝试获取结果,如果异步方法未完成,这里会抛出异常
future.get();
System.out.println("异步方法开启线程成功!" + i);
} catch (Exception e) {
System.out.println("异步方法开启线程失败!!!!!!!" + i);
}
}
}
return new AjaxResult(200, "清洗成功");
}
/*
@Override
public AjaxResult read(String mineId) {
......@@ -47,6 +72,7 @@ public class ActReadDataServiceImpl implements ActReadDataService {
}
return new AjaxResult(200, "清洗成功");
}
*/
/*@Async("defaultThreadPoolExecutor")
void readToId(String mineId, int c) {
......@@ -78,31 +104,4 @@ public class ActReadDataServiceImpl implements ActReadDataService {
System.err.println("本次清洗了:" + list.size() + "条数据");
}
}*/
void readToId(String mineId, int c) {
long start = System.currentTimeMillis();
System.err.println("查询" + c + "条时间为:" + (System.currentTimeMillis() - start));
Map<String, String> materialMap = new HashMap<>();
List<ActHistoryRoleTypeCodeModel> listMaterial = actReadDataMapper.selectMaterialCode();
for (ActHistoryRoleTypeCodeModel model : listMaterial) {
materialMap.putIfAbsent(model.getKey(), model.getValue());
}
MineConver mineConver;
if (mineId.equals(LK)) {
mineConver = new MineConver(new LkSpecialMethod(actReadDataMapper));
} else {
mineConver = new MineConver(new OtherMethod(actReadDataMapper));
}
List<ActReadHistoryModel> list = actReadDataMapper.readDateToHistory(mineId, 1);
mineConver.clean(list.get(0), materialMap);
// for (ActReadHistoryModel actReadHistoryModel : list) {
// mineConver.clean(actReadHistoryModel, materialMap);
// }
long end = System.currentTimeMillis();
System.err.println(end - start);
System.err.println("本次清洗了:" + list.size() + "条数据");
}
}
......@@ -10,6 +10,7 @@ import com.ruoyi.system.mapper.ActReadDataMapper;
import com.ruoyi.system.service.impl.ActReadDataServiceImpl;
import com.ruoyi.system.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
......@@ -20,6 +21,7 @@ import java.util.stream.IntStream;
/**
* 2024/3/27
*/
@Service
public class LkSpecialMethod implements MineConversion {
private final ActReadDataMapper actReadDataMapper;
......
package com.ruoyi.system.service.impl.Conversion;
import com.ruoyi.system.domain.ActHistoryRoleTypeCodeModel;
import com.ruoyi.system.domain.ActReadHistoryModel;
import com.ruoyi.system.mapper.ActReadDataMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import java.util.regex.Pattern;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
/**
* 2024/3/27
*/
@Service
public class MineConver {
private MineConversion mineConversion;
/*private MineConversion mineConversion;
public MineConver(MineConversion mineConversion) {
this.mineConversion = mineConversion;
}*/
@Autowired
private OtherMethod otherMethod;
private void setOtherMethod(OtherMethod otherMethod) {
this.otherMethod = otherMethod;
}
@Autowired
private LkSpecialMethod lkSpecialMethod;
private void setLkSpecialMethod(LkSpecialMethod lkSpecialMethod) {
this.lkSpecialMethod = lkSpecialMethod;
}
@Autowired
private ActReadDataMapper actReadDataMapper;
private static final String LK = "011702";
//@Async注解,注意:如果调用方法和被调用方法都在Service中,则不生效
@Async("defaultThreadPoolExecutor")
//public void readToId(String mineId) {
public Future<String> readToId(String mineId, int i) {
// 模拟耗时操作
try {
Map<String, String> materialMap = new HashMap<>();
List<ActHistoryRoleTypeCodeModel> listMaterial = actReadDataMapper.selectMaterialCode();
for (ActHistoryRoleTypeCodeModel model : listMaterial) {
materialMap.putIfAbsent(model.getKey(), model.getValue());
}
List<ActReadHistoryModel> list = actReadDataMapper.readDateToHistory(mineId, 1);
ActReadHistoryModel historyModel = list.get(0);
if (mineId.equals(LK)) {
LkSpecialMethod lkSpecialMethod = new LkSpecialMethod(actReadDataMapper);
//调用清洗方法
lkSpecialMethod.conversion(historyModel, materialMap);
} else {
OtherMethod otherMethod = new OtherMethod(actReadDataMapper);
//调用清洗方法
otherMethod.conversion(historyModel, materialMap);
}
} catch (Exception e) {
e.printStackTrace();
}
return new AsyncResult<>("YES");
public void clean(ActReadHistoryModel actReadHistoryModel, Object obj){
mineConversion.conversion(actReadHistoryModel, obj);
}
/*public void clean(ActReadHistoryModel actReadHistoryModel, Object obj){
mineConversion.conversion(actReadHistoryModel, obj);
}*/
}
......@@ -10,6 +10,7 @@ import com.ruoyi.system.mapper.ActReadDataMapper;
import com.ruoyi.system.service.impl.ActReadDataServiceImpl;
import com.ruoyi.system.utils.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
......@@ -19,6 +20,7 @@ import java.util.stream.IntStream;
/**
* 2024/3/27
*/
@Service
public class OtherMethod implements MineConversion {
private final ActReadDataMapper actReadDataMapper;
......
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