Spring boot异步休息电话

问题描述 投票:-1回答:1

我正在尝试创建一个调度程序,该调度程序将get请求发送到Web服务并获取我们需要的项目数。然后将Total除以per_page,然后发送所需数量的请求,但请求是异步的。

我的代码在我的测试类中工作得很好,但是在主应用程序中,基于JDK版本,我遇到了两个错误

这是我的API服务:

    public interface RestService {

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

    UpcomingEventsResponse getUpcomingEvents(String day,String token);
}

我的RestService Impl:

@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());
    }
}

这是我的调度程序类:

@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

有更好的方法吗?有什么问题吗?这是一个错误吗?

java spring spring-boot asynchronous resttemplate
1个回答
0
投票

问题出在Web服务中,尽管我真的不明白在不同的JDK中出现错误的原因。据我所知,这是一个已知的错误,您可以阅读有关它的更多信息here

此实现效果很好,您可以将Apache HTTP Client与resttemplate一起使用,但不能将OkHttpApache HttpClientSpring webflux WebService一起使用

热门问题
推荐问题
最新问题