-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
optimized delay management for sending posts
- Loading branch information
Showing
18 changed files
with
188 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/main/java/com/github/yvasyliev/service/async/DelayedBlockingExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.yvasyliev.service.async; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Queue; | ||
import java.util.concurrent.Callable; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
@Component | ||
public class DelayedBlockingExecutor { | ||
@Value("#{new java.util.concurrent.LinkedBlockingQueue()}") | ||
private Queue<Runnable> delayedRunnables; | ||
|
||
public <T> CompletableFuture<T> submit(Callable<T> callable) { | ||
var completableFuture = new CompletableFuture<T>(); | ||
delayedRunnables.add(() -> { | ||
try { | ||
completableFuture.complete(callable.call()); | ||
} catch (Exception e) { | ||
completableFuture.completeExceptionally(e); | ||
} | ||
}); | ||
return completableFuture; | ||
} | ||
|
||
@Scheduled(fixedDelayString = "${delayed.blocking.executor.delay.in.seconds:20}", timeUnit = TimeUnit.SECONDS) | ||
public void runNextRunnable() { | ||
if (!delayedRunnables.isEmpty()) { | ||
delayedRunnables.poll().run(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.