09052042:对这个资产展示使用线程池的方法去做,第五次优化代码
parent
6c2130678c
commit
b7a0ce0a00
|
@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
import com.muyu.source.core.DataValue;
|
||||
import com.muyu.source.domain.model.DataValueModel;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
|
@ -17,7 +18,7 @@ import java.util.List;
|
|||
public interface DataValueService extends IService<DataValue> {
|
||||
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);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
|
|||
// 每批次查询的记录数
|
||||
private static final int BATCH_SIZE = 10000;
|
||||
// 线程池大小
|
||||
private static final int NUM_THREADS = 10;
|
||||
private static final int THREAD_POOL_SIZE = 10;
|
||||
@Override
|
||||
public List<DataValue> findTableValue(DataValueModel dataValueModel) {
|
||||
List<DataValue> dataValueList = new ArrayList<>();
|
||||
|
@ -141,19 +141,16 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
|
|||
}
|
||||
}
|
||||
@Override
|
||||
public List<DataValue> findTableValueByTableName(Long basicId, String tableName) {
|
||||
public List<DataValue> findTableValueByTableName(Long basicId, String tableName) throws SQLException {
|
||||
DataSource etlDataScore = dataSourceService.getById(basicId);
|
||||
MysqlPool mysqlPool = new MysqlPool(etlDataScore);
|
||||
mysqlPool.init();
|
||||
|
||||
int totalRecords = 0;
|
||||
try {
|
||||
totalRecords = getTotalRecords(mysqlPool, tableName);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
int totalRecords=getTotalRecords(mysqlPool, tableName);
|
||||
|
||||
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<>();
|
||||
|
||||
// 创建任务
|
||||
|
@ -178,15 +175,12 @@ public class DataValueServiceImpl extends ServiceImpl<DataValueMapper, DataValue
|
|||
|
||||
private int getTotalRecords(MysqlPool mysqlPool, String tableName) throws SQLException {
|
||||
try (Connection conn = mysqlPool.getConn();
|
||||
Statement statement = conn != null ? conn.createStatement() : null;
|
||||
ResultSet resultSet = statement != null ? statement.executeQuery("SELECT COUNT(*) FROM " + tableName) : null) {
|
||||
if (conn == null) {
|
||||
throw new SQLException("Failed to get database connection");
|
||||
}
|
||||
Statement statement = conn.createStatement();
|
||||
ResultSet resultSet = statement.executeQuery("SELECT COUNT(*) FROM " + tableName)) {
|
||||
if (resultSet != null && resultSet.next()) {
|
||||
return resultSet.getInt(1);
|
||||
} 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()) {
|
||||
if (conn == null) {
|
||||
throw new SQLException("Failed to get database connection");
|
||||
throw new SQLException("无法获取数据库连接");
|
||||
}
|
||||
try (PreparedStatement preparedStatement = conn.prepareStatement(query)) {
|
||||
preparedStatement.setInt(1, BATCH_SIZE);
|
||||
|
|
Loading…
Reference in New Issue