以我的搜索功能爲例,多個搜索結果合併
異步類:AsyncService
import java.util.concurrent.Future;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
/**
* 應用搜索-異步
*/
@Async
public Future<String> searchAppInfo() throws Exception{
//自己的業務邏輯
//睡眠3秒測試
Thread.sleep(3000);
String result = null;
return new AsyncResult<String>(result);
}
/**
* 待辦搜索-異步
*/
@Async
public Future<String> searchPending() throws Exception{
//自己的業務邏輯
//睡眠1秒測試
Thread.sleep(1000);
String result = null;
return new AsyncResult<String>(result);
}
/**
* 用戶搜索-異步
*/
@Async
public Future<String> searchUser() throws Exception{
//自己的業務邏輯
//睡眠2秒測試
Thread.sleep(2000);
String result = null;
return new AsyncResult<String>(result);
}
}
業務類:CompositeService
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CompositeService {
@Autowired
private AsyncService asyncService;
/**
* 綜合搜索集合
*/
public Map<String, String> searchList() {
try {
long beforeTime = System.currentTimeMillis();// 開始時間戳
//應用搜索-異步
Future<String> appResult = asyncService.searchAppInfo();
//待辦搜索-異步
Future<String> pendingResult = asyncService.searchPending();
//用戶搜索-異步
Future<String> userResult = asyncService.searchUser();
//添加結果集,30秒超時
Map<String, String> jsonObject = new HashMap<String, String>();
if(appResult != null){
jsonObject.put("AppInfo", appResult.get(30, TimeUnit.SECONDS));
}
if(pendingResult != null){
jsonObject.put("AppPending", pendingResult.get(30, TimeUnit.SECONDS));
}
if(userResult != null){
jsonObject.put("UnifiedUser", userResult.get(30, TimeUnit.SECONDS));
}
long afterTime = System.currentTimeMillis();// 結束時間戳
//耗時3秒多正確,耗時6秒多異步未成功
System.out.println("耗時"+(afterTime - beforeTime)+"毫秒");
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
調用測試:ControllerTest
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import com.async.service.CompositeService;
/**
* 測試異步任務
*/
@RestController
public class ControllerTest {
@Autowired
private CompositeService compositeService;
@GetMapping("/test")
public String test(){
compositeService.searchList();
return "異步測試...............";
}
}
打印結果,應該爲3秒多,因爲休眠的時間最長的一個方法是3秒
耗時3011毫秒
如果有打印出6秒多,證明有問題,實在解決不了的,可以使用笨一點的辦法,修改業務類CompositeService代碼
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CompositeService {
@Autowired
private AsyncService asyncService;
/**
* 綜合搜索集合
*/
public Map<String, String> searchList() {
try {
long beforeTime = System.currentTimeMillis();// 開始時間戳
//應用搜索-異步
Future<String> appResult = asyncService.searchAppInfo();
//待辦搜索-異步
Future<String> pendingResult = asyncService.searchPending();
//用戶搜索-異步
Future<String> userResult = asyncService.searchUser();
//循環判斷所有異步方法是否執行完畢
long beforeTime1 = System.currentTimeMillis();// 開始時間戳
while (true) {
//執行完畢跳出循環
if((appResult == null || appResult.isDone())
&& (pendingResult == null || pendingResult.isDone())
&& (userResult == null || userResult.isDone())){
break;
}
//超時跳出循環
long afterTime1 = System.currentTimeMillis();// 結束時間戳
if((afterTime1 - beforeTime1) > 30000){ //30秒
break;
}
Thread.sleep(20); //每隔20毫秒執行一次
}
//添加結果集,30秒超時
Map<String, String> jsonObject = new HashMap<String, String>();
if(appResult != null){
jsonObject.put("AppInfo", appResult.get());
}
if(pendingResult != null){
jsonObject.put("AppPending", pendingResult.get());
}
if(userResult != null){
jsonObject.put("UnifiedUser", userResult.get());
}
long afterTime = System.currentTimeMillis();// 結束時間戳
//耗時3秒多正確,耗時6秒多異步未成功
System.out.println("耗時"+(afterTime - beforeTime)+"毫秒");
return jsonObject;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
使用循環判斷Future類自帶的方法isDone()判斷是否執行完畢
//循環判斷所有異步方法是否執行完畢
long beforeTime1 = System.currentTimeMillis();// 開始時間戳
while (true) {
//執行完畢跳出循環
if((appResult == null || appResult.isDone())
&& (pendingResult == null || pendingResult.isDone())
&& (userResult == null || userResult.isDone())){
break;
}
//超時跳出循環
long afterTime1 = System.currentTimeMillis();// 結束時間戳
if((afterTime1 - beforeTime1) > 30000){ //30秒
break;
}
Thread.sleep(20); //每隔20毫秒執行一次
}