@@ -25,13 +25,30 @@ public class J8Examples {
2525 public static void main (final String [] argv ) throws InterruptedException , ExecutionException {
2626 final TaskExecutorService executorService = TaskExecutors .newFixedThreadPool (3 );
2727
28- Promises .success ("ABC" ).applyToEither (Promises .failure (new IllegalArgumentException ()),r -> {
29- System .out .println ("Race won by value: " + r );
30- return r ;
31- });
28+
29+ Promise <String > poller = Promises .poll (
30+ J8Examples ::pollingMethod , executorService ,
31+ RetryPolicy .DEFAULT
32+ .withMaxRetries (10 )
33+ .withTimeout (DelayPolicy .fixedInterval (3200 ))
34+ .withBackoff (DelayPolicy .fixedInterval (200 ).withMinDelay (100 ).withFirstRetryNoDelay ())
35+ );
36+
37+ System .out .println ("Poller: " + poller .get ());
38+
39+ CompletableTask
40+ .delay ( Duration .ofMillis (100 ), executorService )
41+ .thenRun (() -> System .out .println ("After initial delay" ));
42+
3243
3344 CompletableTask
3445 .supplyAsync (() -> awaitAndProduceN (73 ), executorService )
46+ .dependent ()
47+ .delay ( Duration .ofMillis (100 ), true , true )
48+ .thenApply (v -> {
49+ System .out .println ("After delay: " + v );
50+ return v ;
51+ }, true )
3552 .onTimeout (123456789 , Duration .ofMillis (200 ))
3653 .thenAcceptAsync (J8Examples ::onComplete )
3754 .get ();
@@ -127,7 +144,7 @@ private static int awaitAndProduce2(int i) {
127144 private static int awaitAndProduceN (int i ) {
128145 try {
129146 System .out .println ("Delay N + " + i + " in " + Thread .currentThread ());
130- Thread .sleep (500 );
147+ Thread .sleep (1500 );
131148 if (i % 2 == 0 ) {
132149 throw new RuntimeException ("Even value: " + i );
133150 }
@@ -139,6 +156,24 @@ private static int awaitAndProduceN(int i) {
139156 }
140157 }
141158
159+ private static String pollingMethod () throws InterruptedException {
160+ RetryContext ctx = RetryContext .current ();
161+ System .out .println ("Polling method, #" + ctx .getRetryCount ());
162+ try {
163+ if (ctx .getRetryCount () < 5 ) {
164+ Thread .sleep ((5 - ctx .getRetryCount ()) * 1000 );
165+ }
166+ if (ctx .getRetryCount () < 7 ) {
167+ throw new IllegalStateException ();
168+ }
169+ return "Result " + ctx .getRetryCount ();
170+ } catch (final InterruptedException ex ) {
171+ System .out .println ("Polling method, #" + ctx .getRetryCount () + ", interrupted!" );
172+ Thread .currentThread ().interrupt ();
173+ throw ex ;
174+ }
175+ }
176+
142177 private static void onComplete (int i ) {
143178 System .out .println (">>> Result " + i + ", " + Thread .currentThread ());
144179 }
0 commit comments