09052042:对这个资产展示使用线程池的方法去做,第五次优化代码

master
冷调 2024-09-05 20:42:27 +08:00
parent 6c2130678c
commit b7a0ce0a00
2 changed files with 12 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
import com.muyu.source.core.DataValue; import com.muyu.source.core.DataValue;
import com.muyu.source.domain.model.DataValueModel; import com.muyu.source.domain.model.DataValueModel;
import java.sql.SQLException;
import java.util.List; import java.util.List;
/** /**
@ -17,7 +18,7 @@ import java.util.List;
public interface DataValueService extends IService<DataValue> { public interface DataValueService extends IService<DataValue> {
List<DataValue> findTableValue(DataValueModel dataValueModel); List<DataValue> findTableValue(DataValueModel dataValueModel);
List<DataValue> findTableValueByTableName(Long basicId, String tableName); List<DataValue> findTableValueByTableName(Long basicId, String tableName) throws SQLException;
Integer addTableValue(DataValueModel dataValueModel); Integer addTableValue(DataValueModel dataValueModel);
} }

View File

@ -37,7 +37,7 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
// 每批次查询的记录数 // 每批次查询的记录数
private static final int BATCH_SIZE = 10000; private static final int BATCH_SIZE = 10000;
// 线程池大小 // 线程池大小
private static final int NUM_THREADS = 10; private static final int THREAD_POOL_SIZE = 10;
@Override @Override
public List<DataValue> findTableValue(DataValueModel dataValueModel) { public List<DataValue> findTableValue(DataValueModel dataValueModel) {
List<DataValue> dataValueList = new ArrayList<>(); List<DataValue> dataValueList = new ArrayList<>();
@ -141,19 +141,16 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
} }
} }
@Override @Override
public List<DataValue> findTableValueByTableName(Long basicId, String tableName) { public List<DataValue> findTableValueByTableName(Long basicId, String tableName) throws SQLException {
DataSource etlDataScore = dataSourceService.getById(basicId); DataSource etlDataScore = dataSourceService.getById(basicId);
MysqlPool mysqlPool = new MysqlPool(etlDataScore); MysqlPool mysqlPool = new MysqlPool(etlDataScore);
mysqlPool.init(); mysqlPool.init();
int totalRecords = 0;
try { int totalRecords=getTotalRecords(mysqlPool, tableName);
totalRecords = getTotalRecords(mysqlPool, tableName);
} catch (SQLException e) {
throw new RuntimeException(e);
}
int totalBatches = (totalRecords + BATCH_SIZE - 1) / BATCH_SIZE; int totalBatches = (totalRecords + BATCH_SIZE - 1) / BATCH_SIZE;
ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS); ExecutorService executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);
List<Future<List<DataValue>>> futures = new ArrayList<>(); List<Future<List<DataValue>>> futures = new ArrayList<>();
// 创建任务 // 创建任务
@ -178,15 +175,12 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
private int getTotalRecords(MysqlPool mysqlPool, String tableName) throws SQLException { private int getTotalRecords(MysqlPool mysqlPool, String tableName) throws SQLException {
try (Connection conn = mysqlPool.getConn(); try (Connection conn = mysqlPool.getConn();
Statement statement = conn != null ? conn.createStatement() : null; Statement statement = conn.createStatement();
ResultSet resultSet = statement != null ? statement.executeQuery("SELECT COUNT(*) FROM " + tableName) : null) { ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM " + tableName)) {
if (conn == null) {
throw new SQLException("Failed to get database connection");
}
if (resultSet != null && resultSet.next()) { if (resultSet != null && resultSet.next()) {
return resultSet.getInt(1); return resultSet.getInt(1);
} else { } else {
throw new SQLException("Failed to get total record count"); throw new SQLException("无法获取总记录数");
} }
} }
} }
@ -197,7 +191,7 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
try (Connection conn = mysqlPool.getConn()) { try (Connection conn = mysqlPool.getConn()) {
if (conn == null) { if (conn == null) {
throw new SQLException("Failed to get database connection"); throw new SQLException("无法获取数据库连接");
} }
try (PreparedStatement preparedStatement = conn.prepareStatement(query)) { try (PreparedStatement preparedStatement = conn.prepareStatement(query)) {
preparedStatement.setInt(1, BATCH_SIZE); preparedStatement.setInt(1, BATCH_SIZE);