|
|
@@ -0,0 +1,84 @@
|
|
|
+package com.mokasz.pool.image;
|
|
|
+
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.http.HttpResponse;
|
|
|
+import org.apache.http.NameValuePair;
|
|
|
+import org.apache.http.client.HttpClient;
|
|
|
+import org.apache.http.client.entity.UrlEncodedFormEntity;
|
|
|
+import org.apache.http.client.methods.HttpPost;
|
|
|
+import org.apache.http.impl.client.HttpClientBuilder;
|
|
|
+import org.apache.http.message.BasicNameValuePair;
|
|
|
+
|
|
|
+import javax.xml.bind.DatatypeConverter;
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.FileReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.ExecutorService;
|
|
|
+import java.util.concurrent.Executors;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+import java.util.concurrent.atomic.AtomicLong;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author wudi
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+public class TestProgram {
|
|
|
+ public static void main(String[] args) {
|
|
|
+ if (args.length < 1) {
|
|
|
+ System.err.println("Usage: <urls.txt>");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ long count = 0;
|
|
|
+ long skip = args.length >= 2 ? DatatypeConverter.parseLong(args[1]) : 0;
|
|
|
+ ExecutorService executorService = Executors.newFixedThreadPool(16);
|
|
|
+ AtomicLong finished = new AtomicLong(0);
|
|
|
+ try (BufferedReader br = new BufferedReader(new FileReader(args[0]))) {
|
|
|
+ String line;
|
|
|
+ while ((line = br.readLine()) != null) {
|
|
|
+ count++;
|
|
|
+ if (count <= skip) continue;
|
|
|
+ // process the line.
|
|
|
+ if (line.startsWith("http")) {
|
|
|
+ executorService.execute(new HttpWorker(line, finished));
|
|
|
+ }
|
|
|
+ while (!executorService.isTerminated()) {
|
|
|
+ if (executorService.awaitTermination(100L, TimeUnit.MILLISECONDS)) log.info("Done: {}", finished);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ executorService.shutdown();
|
|
|
+ } catch (IOException | InterruptedException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+@Slf4j
|
|
|
+class HttpWorker implements Runnable {
|
|
|
+ private final String url;
|
|
|
+ private final AtomicLong counter;
|
|
|
+
|
|
|
+ HttpWorker(String url, AtomicLong counter) {
|
|
|
+ this.url = url;
|
|
|
+ this.counter = counter;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void run() {
|
|
|
+ try {
|
|
|
+ HttpClient httpClient = HttpClientBuilder.create().build();
|
|
|
+ HttpPost httpPost = new HttpPost("http://image-pool-service-svc:8080/service/image/store/url");
|
|
|
+ List<NameValuePair> params = new ArrayList<>();
|
|
|
+ params.add(new BasicNameValuePair("url", url));
|
|
|
+ httpPost.setEntity(new UrlEncodedFormEntity(params));
|
|
|
+ HttpResponse response = httpClient.execute(httpPost);
|
|
|
+ log.debug(response.getStatusLine().getReasonPhrase());
|
|
|
+ } catch (IOException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ long done = this.counter.incrementAndGet();
|
|
|
+ if (done % 1000 == 0) log.info("Executed: {}", done);
|
|
|
+ }
|
|
|
+}
|