From f2937772b45e8a39b628f9f358d9007475c74a0e Mon Sep 17 00:00:00 2001 From: Terekhin Alexander Date: Tue, 11 Aug 2020 16:57:43 +0300 Subject: [PATCH] Spring version upgrade, DownloaderImpl complete (need some tests!) --- pom.xml | 2 +- .../me/bearns/fias/service/Downloader.java | 6 +- .../bearns/fias/service/DownloaderImpl.java | 59 +++++++++++++++---- .../me/bearns/fias/service/FiasUpdater.java | 4 +- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/pom.xml b/pom.xml index 5037c4f..d3d81da 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 2.2.6.RELEASE + 2.3.2.RELEASE me.bearns diff --git a/src/main/java/me/bearns/fias/service/Downloader.java b/src/main/java/me/bearns/fias/service/Downloader.java index addaf77..5c1d1de 100644 --- a/src/main/java/me/bearns/fias/service/Downloader.java +++ b/src/main/java/me/bearns/fias/service/Downloader.java @@ -1,9 +1,9 @@ package me.bearns.fias.service; -import java.io.InputStream; -import java.util.concurrent.Future; +import java.io.File; +import java.util.concurrent.CompletableFuture; public interface Downloader { - public Future download(String url); + public CompletableFuture download(String url); } diff --git a/src/main/java/me/bearns/fias/service/DownloaderImpl.java b/src/main/java/me/bearns/fias/service/DownloaderImpl.java index cb204d3..9e2a8a3 100644 --- a/src/main/java/me/bearns/fias/service/DownloaderImpl.java +++ b/src/main/java/me/bearns/fias/service/DownloaderImpl.java @@ -1,28 +1,65 @@ package me.bearns.fias.service; -import java.io.InputStream; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; +import org.springframework.stereotype.Component; +import java.io.*; +import java.net.URL; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.concurrent.*; + +@Component public class DownloaderImpl implements Downloader { private static final ExecutorService pool = Executors.newCachedThreadPool(); + private static final String BASE_DIRECTORY = "/var/opt/fias"; + private static final ConcurrentMap> locks = new ConcurrentHashMap(); @Override - public Future download(String url) { + public CompletableFuture download(String url) { + + CompletableFuture start = new CompletableFuture<>(); + + //syncronized + final CompletableFuture feature = locks.putIfAbsent(url, start); - CompletableFuture completableFuture - = new CompletableFuture<>(); + //return earlier runned feature + if(feature != start) return feature; pool.submit(() -> { - InputStream is = null; //todo - completableFuture.complete(is); + final URL conn; + final Path path; + + try{ + conn = new URL(url); + path = Paths.get(BASE_DIRECTORY, conn.getFile()); + + } catch (IOException e) { + feature.obtrudeException(e); + return; + } + + final File file = path.toFile(); + + if(!file.exists()) { + + try (BufferedInputStream in = new BufferedInputStream(conn.openStream())) { + + Files.copy(in, path, StandardCopyOption.REPLACE_EXISTING); + + } catch (IOException e) { + // handle exception + feature.obtrudeException(e); + } + } + + feature.complete(file); }); - return completableFuture; + return feature; } } diff --git a/src/main/java/me/bearns/fias/service/FiasUpdater.java b/src/main/java/me/bearns/fias/service/FiasUpdater.java index 3c7243d..a1f9814 100644 --- a/src/main/java/me/bearns/fias/service/FiasUpdater.java +++ b/src/main/java/me/bearns/fias/service/FiasUpdater.java @@ -5,7 +5,7 @@ import me.bearns.fias.repository.FiasVersionRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import java.io.InputStream; +import java.io.File; import java.util.List; import java.util.concurrent.Future; import java.util.stream.Stream; @@ -63,7 +63,7 @@ public class FiasUpdater implements Updater { updates.sort((o1, o2) -> Math.toIntExact(o1.getVersionId() - o2.getVersionId())); //just for test - final Stream> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl())); + final Stream> stream = updates.stream().map(u -> downloader.download(reloadFlag ? u.getFiasCompleteXmlUrl() : u.getFiasDeltaXmlUrl())); }