parent
83fcb23e67
commit
f2937772b4
@ -1,9 +1,9 @@ |
|||||||
package me.bearns.fias.service; |
package me.bearns.fias.service; |
||||||
|
|
||||||
import java.io.InputStream; |
import java.io.File; |
||||||
import java.util.concurrent.Future; |
import java.util.concurrent.CompletableFuture; |
||||||
|
|
||||||
public interface Downloader { |
public interface Downloader { |
||||||
|
|
||||||
public Future<InputStream> download(String url); |
public CompletableFuture<File> download(String url); |
||||||
} |
} |
||||||
|
@ -1,28 +1,65 @@ |
|||||||
package me.bearns.fias.service; |
package me.bearns.fias.service; |
||||||
|
|
||||||
import java.io.InputStream; |
import org.springframework.stereotype.Component; |
||||||
import java.util.concurrent.CompletableFuture; |
|
||||||
import java.util.concurrent.ExecutorService; |
|
||||||
import java.util.concurrent.Executors; |
|
||||||
import java.util.concurrent.Future; |
|
||||||
|
|
||||||
|
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 { |
public class DownloaderImpl implements Downloader { |
||||||
|
|
||||||
private static final ExecutorService pool = Executors.newCachedThreadPool(); |
private static final ExecutorService pool = Executors.newCachedThreadPool(); |
||||||
|
private static final String BASE_DIRECTORY = "/var/opt/fias"; |
||||||
|
private static final ConcurrentMap<String,CompletableFuture<File>> locks = new ConcurrentHashMap(); |
||||||
|
|
||||||
@Override |
@Override |
||||||
public Future<InputStream> download(String url) { |
public CompletableFuture<File> download(String url) { |
||||||
|
|
||||||
|
CompletableFuture<File> start = new CompletableFuture<>(); |
||||||
|
|
||||||
|
//syncronized
|
||||||
|
final CompletableFuture<File> feature = locks.putIfAbsent(url, start); |
||||||
|
|
||||||
CompletableFuture<InputStream> completableFuture |
//return earlier runned feature
|
||||||
= new CompletableFuture<>(); |
if(feature != start) return feature; |
||||||
|
|
||||||
pool.submit(() -> { |
pool.submit(() -> { |
||||||
InputStream is = null; |
|
||||||
//todo
|
//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; |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue