future = executorService.submit(() -> {
return queryFromMysql(id);
});
CompletableFuture.supplyAsync(new Supplier() {
@Override
public String get() {
try {
return future.get();
} catch (Exception e) {
return null;
}
}
}).thenAccept( (String dbResult) -> {
resultFuture.complete(Collections.singleton(dbResult));
});
}
private String queryFromMysql(String param) throws SQLException {
String sql = "select name from info where id = ?";
String result = null;
Connection connection = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
connection = dataSource.getConnection();
stmt = connection.prepareStatement(sql);
stmt.setString(1,param);
rs = stmt.executeQuery();
while (rs.next()){
result = rs.getString("name");
}
}finally {
if (rs != null){
rs.close();
}
if (stmt != null){
stmt.close();
}
if (connection != null){
connection.close();
}
}
if (result != null){
//可以放入缓存中
}
return result;
}
}
963

被折叠的 条评论
为什么被折叠?



