簡體   English   中英

Spring 啟動異步 rest 調用

[英]Spring boot Async rest call

我正在嘗試創建一個調度程序,它將獲取請求發送到 web 服務並獲取我們需要的項目的數量。 然后將 Total 除以 per_page 然后發送所需的任意數量的請求,但請求是異步的。

我的代碼在我的測試 class 中運行良好,但在主應用程序中,我收到兩個基於 JDK 版本的錯誤

這是我的 API 服務:

    public interface RestService {

    @Async
    CompletableFuture<UpcomingEventsResponse> getUpcomingEvents(int page,String day, String token);

    UpcomingEventsResponse getUpcomingEvents(String day,String token);
}

我的 RestService 實現:

@Service
@RequiredArgsConstructor
@Slf4j
public class RestServiceImpl implements RestService {

    public static final String UPCOMING_EVENTS_URL = "HTTP://localhost/test";
    private final RestTemplate restTemplate;

    @Override
    public CompletableFuture<UpcomingEventsResponse> getUpcomingEvents(int page,String day, String token) {
        String url = createUrl(UPCOMING_EVENTS_URL,createQuery("day",day),createQuery("page", page), createQuery("token", token));

        return makeCallAsync(url,HttpMethod.GET,null,UpcomingEventsResponse.class);
    }

    @Override
    public UpcomingEventsResponse getUpcomingEvents(String day,String token) {
        String url = createUrl(UPCOMING_EVENTS_URL,createQuery("day",day), createQuery("page", 1), createQuery("token", token));

        return makeCall(url,HttpMethod.GET,null,UpcomingEventsResponse.class);
    }

    private <T> T makeCall(String url,
                           HttpMethod method,
                           HttpEntity<Object> httpEntity,
                           Class<T> outClass) {

        return restTemplate.exchange(url, method, httpEntity, outClass).getBody();
    }

    private <T> CompletableFuture<T> makeCallAsync(String url,
                                                   HttpMethod method,
                                                   HttpEntity<Object> httpEntity,
                                                   Class<T> outClass) {

        return CompletableFuture.completedFuture(restTemplate.exchange(url, method, httpEntity, outClass).getBody());
    }
}

這是我的調度程序 class:

@Component
@RequiredArgsConstructor
@Slf4j
public class EventScheduler {

    private final RestService restService;

    //TODO change time
    @Scheduled(cron = "0 */2 * * * *")
    public void getAllEvents(){
        long start = System.currentTimeMillis();

        //TODO add token from database or env

        UpcomingEventsResponse upcomingEvents = restService.getUpcomingEvents(null, "token");

        List<ResultsItem> resultsItems = new ArrayList<>(upcomingEvents.getResults());
        List<CompletableFuture<UpcomingEventsResponse>> completableFutures = new ArrayList<>();

        int repeatTimes = upcomingEvents.getPager().getTotal() / upcomingEvents.getPager().getPerPage();

        for (int i = 0; i < repeatTimes; i++) {

            int page = i + 2;

            CompletableFuture<UpcomingEventsResponse> events = restService.getUpcomingEvents(page, null, "token");
            completableFutures.add(events);
        }

        CompletableFuture.allOf(completableFutures.toArray(new CompletableFuture[0])).join();

        log.info("Elapsed time: " + (System.currentTimeMillis() - start));

        completableFutures.forEach(completableFuture -> {
            try {
                resultsItems.addAll(completableFuture.get().getResults());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        });
        log.info("Size " + resultsItems.size());
        log.info("Total " + upcomingEvents.getPager().getTotal());
    }

}

這是我在 JDK 8 中遇到的錯誤:

peer not authenticated; nested exception is javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated

這是 JDK 10 或 11 上的錯誤:

javax.net.ssl.SSLException: No PSK available. Unable to resume

有一個更好的方法嗎? 問題是什么? 這是一個錯誤嗎?

問題出在 Web 服務中,雖然我真的無法理解不同 JDK 中錯誤的原因。 據我所知,這是一個已知的錯誤,您可以在此處閱讀更多信息

this implementation works just fine and you can use Apache HTTP Client with resttemplate but you can't use OkHttp or Apache HttpClient with Spring webflux WebService

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM