-1

I am trying to create a scheduler that sends a get request to a web service and gets the count of the items we need. then divide the Total by per_page then send any number of requests needed but requests are Async.

my code is working perfectly fine in my test class but in the main Application im getting two error based on the JDK version

this is my API service :

    public interface RestService {

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

    UpcomingEventsResponse getUpcomingEvents(String day,String token);
}

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

and this is my scheduler 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());
    }

}

and this is the error I'm getting in JDK 8:

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

and this is the error on JDK 10 or 11 :

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

is there a better way to do this? and what is the problem? is this a bug?

peterh
  • 11,875
  • 18
  • 85
  • 108
Rea Teria
  • 133
  • 2
  • 17

1 Answers1

0

The problem was in the Web Service, although I really can't understand the reason for the error in different JDKs. as far as I known this is a known bug and you can read more about it here

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

Rea Teria
  • 133
  • 2
  • 17