From 548af3935be1d815f911cc09a7814624038d3f87 Mon Sep 17 00:00:00 2001 From: Yevhen Vasyliev Date: Sat, 6 Jan 2024 11:51:39 +0200 Subject: [PATCH] fetching data asynchronously --- .github/workflows/release-v2.yml | 29 ++++++++++++++++++- README.md | 2 +- .../service/reddit/SubredditNewSupplier.java | 19 +++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.github/workflows/release-v2.yml b/.github/workflows/release-v2.yml index 58109e5..45694ad 100644 --- a/.github/workflows/release-v2.yml +++ b/.github/workflows/release-v2.yml @@ -42,7 +42,34 @@ jobs: echo "version=$version" >> "$GITHUB_OUTPUT" perform-tests: - uses: ./.github/workflows/perform-tests.yml +# uses: ./.github/workflows/perform-tests.yml + runs-on: ubuntu-latest + environment: Test + concurrency: perform-tests + steps: + - name: Checkout + uses: actions/checkout@v4 + - name: Set up JDK 21 + uses: actions/setup-java@v3 + with: + java-version: '21' + distribution: 'corretto' + cache: maven + - name: Build with Maven + env: + REDDIT_SUBREDDIT: ${{ secrets.REDDIT_SUBREDDIT }} + REDDIT_CLIENT_ID: ${{ secrets.REDDIT_CLIENT_ID }} + REDDIT_CLIENT_SECRET: ${{ secrets.REDDIT_CLIENT_SECRET }} + REDDIT_PASSWORD: ${{ secrets.REDDIT_PASSWORD }} + REDDIT_USERNAME: ${{ secrets.REDDIT_USERNAME }} + SPRING_JPA_HIBERNATE_DDL_AUTO: ${{ vars.SPRING_JPA_HIBERNATE_DDL_AUTO }} + TELEGRAM_ADMIN_ID: ${{ secrets.TELEGRAM_ADMIN_ID }} + TELEGRAM_BOT_TOKEN: ${{ secrets.TELEGRAM_BOT_TOKEN }} + TELEGRAM_BOT_USERNAME: ${{ secrets.TELEGRAM_BOT_USERNAME }} + TELEGRAM_CHANNEL_ID: ${{ secrets.TELEGRAM_CHANNEL_ID }} + TELEGRAM_CHAT_ID: ${{ secrets.TELEGRAM_CHAT_ID }} + TELEGRAM_SCHEDULE_POSTING_ENABLED: ${{ vars.TELEGRAM_SCHEDULE_POSTING_ENABLED }} + run: mvn test get-release-version: runs-on: ubuntu-latest diff --git a/README.md b/README.md index 3add44e..e0e17ca 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ Spring Boot-based Java application to forward Reddit posts to Telegram channel. # Requirements -- `Java 17` +- `Java 21` - `Maven` - [Reddit](https://www.reddit.com/login/) profile - [Telegram](https://web.telegram.org/a/) profile diff --git a/src/main/java/com/github/yvasyliev/service/reddit/SubredditNewSupplier.java b/src/main/java/com/github/yvasyliev/service/reddit/SubredditNewSupplier.java index a66e29f..0dcd344 100644 --- a/src/main/java/com/github/yvasyliev/service/reddit/SubredditNewSupplier.java +++ b/src/main/java/com/github/yvasyliev/service/reddit/SubredditNewSupplier.java @@ -14,6 +14,7 @@ import org.springframework.stereotype.Service; import org.springframework.util.function.ThrowingSupplier; +import java.io.IOException; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; @@ -23,6 +24,9 @@ import java.util.Objects; import java.util.Spliterator; import java.util.Spliterators; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.TimeUnit; import java.util.stream.StreamSupport; @Service @@ -53,7 +57,7 @@ public class SubredditNewSupplier implements ThrowingSupplier> { @Override @NonNull public List getWithException() throws Exception { - var subredditPosts = fetchNewPosts(); + var subredditPosts = fetchNewPosts().get(timeout, TimeUnit.MINUTES); var children = subredditPosts.withArray("/data/children").elements(); var lastCreated = propertyService.findLastCreated().orElse(0); return StreamSupport @@ -79,7 +83,7 @@ public List getWithException() throws Exception { .toList(); } - private JsonNode fetchNewPosts() throws Exception { + private CompletableFuture fetchNewPosts() throws Exception { var redditAccessToken = redditAccessTokenSupplier.getWithException(); var authorization = "Bearer %s".formatted(redditAccessToken.token()); var api = "https://oauth.reddit.com/r/%s/new?raw_json=1".formatted(subreddit); @@ -89,7 +93,14 @@ private JsonNode fetchNewPosts() throws Exception { .GET() .timeout(Duration.ofMinutes(timeout)) .build(); - var jsonBody = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body(); - return objectMapper.readTree(jsonBody); + return httpClient + .sendAsync(request, HttpResponse.BodyHandlers.ofInputStream()) + .thenApply(httpResponse -> { + try (var body = httpResponse.body()) { + return objectMapper.readTree(body); + } catch (IOException e) { + throw new CompletionException(e); + } + }); } }