Untitled
unknown
java
a year ago
2.2 kB
11
Indexable
import java.net.URI; import java.util.List; import java.util.concurrent.*; public class YourService { private final int TIMEOUT_SECONDS = 5; // Set your timeout duration in seconds public List<AmWorkList> yourInternalApiMethod() { ExecutorService executor = Executors.newFixedThreadPool(1); try { CompletableFuture<List<AmWorkList>> future = CompletableFuture.supplyAsync(() -> { try { SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); requestFactory.setConnectTimeout(TIMEOUT_SECONDS * 1000); requestFactory.setReadTimeout(TIMEOUT_SECONDS * 1000); RestTemplate restTemplate = new RestTemplate(requestFactory); // Replace the following with the creation of your RequestEntity<List<AmWorkList>> // For example, assuming you have a List<AmWorkList> object and a URI endpoint: List<AmWorkList> workList = //...; // Replace with your actual List<AmWorkList> object URI uri = URI.create("https://external-api.com/endpoint"); // Replace with your actual endpoint RequestEntity<List<AmWorkList>> requestEntity = RequestEntity.post(uri).body(workList); return restTemplate.exchange(requestEntity, new ParameterizedTypeReference<List<AmWorkList>>() {}).getBody(); } catch (Exception e) { // Handle exceptions if needed throw new RuntimeException("Error during the external API call", e); } }, executor); return future.get(TIMEOUT_SECONDS, TimeUnit.SECONDS); } catch (TimeoutException e) { // Handle timeout exception throw new GatewayTimeoutException("Timeout occurred during the external API call"); } catch (InterruptedException | ExecutionException e) { // Handle other exceptions if needed throw new RuntimeException("Error during the external API call", e); } finally { executor.shutdown(); // Shutdown the executor } } // Other methods as needed }
Editor is loading...
Leave a Comment