From 982897b823d65a32049def459fe11a3419f4be93 Mon Sep 17 00:00:00 2001 From: Florent Youinou Date: Sat, 9 Dec 2017 15:51:01 +0100 Subject: [PATCH] indexer: add delay funciton --- .../java/com/epita/guereza/IndexerApp.java | 22 +++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/epita/guereza/IndexerApp.java b/src/main/java/com/epita/guereza/IndexerApp.java index 1244d4a..0494adc 100644 --- a/src/main/java/com/epita/guereza/IndexerApp.java +++ b/src/main/java/com/epita/guereza/IndexerApp.java @@ -8,6 +8,9 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.time.Duration; +import java.util.concurrent.*; + public class IndexerApp extends App { private static final Logger LOGGER = LoggerFactory.getLogger(IndexerApp.class); private final Indexer indexer; @@ -40,14 +43,25 @@ private void requestNextUrl() { sendMessage("/request/indexer/url", subscribeUrl); } + private void retryIn(final int seconds) { + LOGGER.info("Retry fetching url in {}seconds", seconds); + final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); + executor.schedule(this::requestNextUrl, seconds, TimeUnit.SECONDS); + executor.shutdownNow(); + } + @Override public void run() { eventBus.subscribe(subscribeUrl, msg -> { - final String url = msg.getContent(); - LOGGER.info("Receive url: {}", url); - indexAndPublish(url); + if (msg != null) { + final String url = msg.getContent(); + LOGGER.info("Receive url: {}", url); + indexAndPublish(url); - requestNextUrl(); + requestNextUrl(); + } else { + retryIn(30); + } }); requestNextUrl();