Skip to content

Codes #307

@siddharthrgade21-a11y

Description

@siddharthrgade21-a11y

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AdvancedConcurrentScraper {

private static final HttpClient httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(5))
        .followRedirects(HttpClient.Redirect.NORMAL)
        .build();

public static void main(String[] args) {
    List<String> targetUrls = List.of(
            "https://postman-echo.com",
            "https://postman-echo.com",
            "https://postman-echo.com"
    );

    // Utilize virtual threads via an auto-closeable executor
    try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
        
        List<CompletableFuture<Void>> futures = targetUrls.stream()
                .map(url -> CompletableFuture.runAsync(() -> fetchUrl(url), executor))
                .toList();

        // Wait for all non-blocking virtual threads to finish execution
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        
    } catch (Exception e) {
        System.err.println("Execution failed: " + e.getMessage());
    }
}

private static void fetchUrl(String url) {
    try {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(3))
                .header("User-Agent", "JavaVirtualThreadScraper")
                .GET()
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        
        System.out.printf("[Thread: %s] URL: %s | Status: %d%n", 
                Thread.currentThread().toString(), url, response.statusCode());
        
    } catch (Exception e) {
        System.err.printf("Error fetching %s: %s%n", url, e.getMessage());
    }
}

}

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class AdvancedConcurrentScraper {

private static final HttpClient httpClient = HttpClient.newBuilder()
        .connectTimeout(Duration.ofSeconds(5))
        .followRedirects(HttpClient.Redirect.NORMAL)
        .build();

public static void main(String[] args) {
    List<String> targetUrls = List.of(
            "https://postman-echo.com",
            "https://postman-echo.com",
            "https://postman-echo.com"
    );

    // Utilize virtual threads via an auto-closeable executor
    try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
        
        List<CompletableFuture<Void>> futures = targetUrls.stream()
                .map(url -> CompletableFuture.runAsync(() -> fetchUrl(url), executor))
                .toList();

        // Wait for all non-blocking virtual threads to finish execution
        CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join();
        
    } catch (Exception e) {
        System.err.println("Execution failed: " + e.getMessage());
    }
}

private static void fetchUrl(String url) {
    try {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(url))
                .timeout(Duration.ofSeconds(3))
                .header("User-Agent", "JavaVirtualThreadScraper")
                .GET()
                .build();

        HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
        
        System.out.printf("[Thread: %s] URL: %s | Status: %d%n", 
                Thread.currentThread().toString(), url, response.statusCode());
        
    } catch (Exception e) {
        System.err.printf("Error fetching %s: %s%n", url, e.getMessage());
    }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions