placidenduwayo před 1 rokem
rodič
revize
3b03899427

+ 9
- 5
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java Zobrazit soubor

@@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
13 13
 import org.springframework.web.bind.annotation.RestController;
14 14
 import play.api.libs.json.JsValue;
15 15
 import play.api.libs.json.Json;
16
+import scala.Option;
16 17
 import scala.collection.IndexedSeq;
17 18
 import scala.concurrent.Future;
18 19
 
@@ -22,6 +23,8 @@ import java.util.Random;
22 23
 import java.util.concurrent.CompletableFuture;
23 24
 import java.util.concurrent.ExecutionException;
24 25
 
26
+import java.util.concurrent.TimeoutException;
27
+
25 28
 
26 29
 @SuppressWarnings("SpellCheckingInspection")
27 30
 @RestController
@@ -29,7 +32,7 @@ public class TvSeriesController {
29 32
 
30 33
     private final AkkaStreamFileProcessingFuture akkaStreamFilesProcessing;
31 34
     private final Logger logger = LoggerFactory.getLogger(this.getClass());
32
-    private static final int BORN_INF = 500, BORN_SUP = 5000;
35
+    private static final int BORN_INF = 100, BORN_SUP = 2000;
33 36
 
34 37
     public TvSeriesController(AkkaStreamFileProcessingFuture akkaStreamFilesProcessing) {
35 38
         this.akkaStreamFilesProcessing = akkaStreamFilesProcessing;
@@ -37,7 +40,7 @@ public class TvSeriesController {
37 40
 
38 41
     @RequestMapping(value = "/persons/id/{personID}", method = RequestMethod.GET)
39 42
     private String getPersonByID(@PathVariable(name = "personID") String personID) throws ExecutionException, InterruptedException {
40
-        Future<Models.Person> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
43
+        Future<Option<Models.Person>> futurePerson = akkaStreamFilesProcessing.getPersonByIdFuture(personID);
41 44
         CompletableFuture<Models.Person> completableFuture = CompletableFutureBuilder
42 45
                 .buildcompletableFuture1(futurePerson);
43 46
         while (!completableFuture.isDone()) {
@@ -45,6 +48,7 @@ public class TvSeriesController {
45 48
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
46 49
         }
47 50
         Models.Person person = completableFuture.get();
51
+
48 52
         JsValue personJs = Json.toJson(person, person.personFormat());
49 53
         return Json.prettyPrint(personJs);
50 54
     }
@@ -53,7 +57,7 @@ public class TvSeriesController {
53 57
     private String getPersonByName(@PathVariable(name = "primaryName") String primaryName)
54 58
             throws ExecutionException, InterruptedException {
55 59
 
56
-        Future<Models.Person> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
60
+        Future<Option<Models.Person>> personFuture = akkaStreamFilesProcessing.getPersonByNameFuture(primaryName);
57 61
         CompletableFuture<Models.Person> personCompletableFuture = CompletableFutureBuilder
58 62
                 .buildcompletableFuture1(personFuture);
59 63
         while (!personCompletableFuture.isDone()) {
@@ -68,7 +72,7 @@ public class TvSeriesController {
68 72
     @RequestMapping(value = "/tvseries/id/{tvSerieId}", method = RequestMethod.GET)
69 73
     private String getTvSerieByID(@PathVariable(name = "tvSerieId") String tvSerieID)
70 74
             throws ExecutionException, InterruptedException {
71
-        Future<Models.TvSerie> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
75
+        Future<Option<Models.TvSerie>> tvSerieFuture = akkaStreamFilesProcessing.getTvSerieByIdFuture(tvSerieID);
72 76
         CompletableFuture<Models.TvSerie> tvSerieCompletableFuture = CompletableFutureBuilder
73 77
                 .buildcompletableFuture1(tvSerieFuture);
74 78
         while (!tvSerieCompletableFuture.isDone()) {
@@ -137,7 +141,7 @@ public class TvSeriesController {
137 141
                 .buildcompletableFuture2(futureIndexedSeqPersons);
138 142
 
139 143
         while (!seqCompletableFuture.isDone()){
140
-            logger.info("IS PROCESSING");
144
+            logger.info("IS PROCESSING...");
141 145
             Thread.sleep(new Random().nextInt(BORN_SUP-BORN_INF)+BORN_INF);
142 146
         }
143 147
         IndexedSeq<Models.Person> personIndexedSeq = seqCompletableFuture.get();

+ 15
- 12
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java Zobrazit soubor

@@ -1,35 +1,38 @@
1 1
 package fr.natan.akkastreamfileprocessingapi.futurecompleteness;
2 2
 
3
-
3
+import akka.util.Timeout;
4
+import scala.Option;
4 5
 import scala.collection.IndexedSeq;
6
+import scala.concurrent.Await;
5 7
 import scala.concurrent.Future;
6 8
 
7
-import java.util.Random;
9
+import java.time.Duration;
8 10
 import java.util.concurrent.CompletableFuture;
9 11
 import java.util.concurrent.Executors;
10
-
12
+import java.util.concurrent.TimeUnit;
13
+import java.util.concurrent.TimeoutException;
11 14
 
12 15
 
13 16
 public class CompletableFutureBuilder<T> {
14 17
 
15
-    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<T> futureT) {
18
+    public static<T> CompletableFuture<T> buildcompletableFuture1(Future<Option<T>> futureT) {
16 19
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
17
-       Executors.newSingleThreadExecutor().submit(() -> {
18
-           while(!futureT.isCompleted()){
19
-               Thread.sleep(new Random().nextInt(500-100)+100);
20
-           }
21
-            completableFuture.complete(futureT.value().get().get());
20
+        Executors.newSingleThreadExecutor().submit(() -> {
21
+            while(!futureT.isCompleted()){
22
+                TimeUnit.MILLISECONDS.sleep(100);
23
+            }
24
+            completableFuture.complete(futureT.value().get().get().get());
22 25
             return null;
23 26
         });
24 27
 
25 28
         return completableFuture;
26 29
     }
27 30
 
28
-    public static<T> CompletableFuture<IndexedSeq<T>> buildcompletableFuture2(Future<IndexedSeq<T>> futureListT) {
31
+    public static <T> CompletableFuture<IndexedSeq<T>> buildcompletableFuture2(Future<IndexedSeq<T>> futureListT) {
29 32
         CompletableFuture<IndexedSeq<T>> completableFuture = new CompletableFuture<>();
30 33
         Executors.newSingleThreadExecutor().submit(() -> {
31
-            while(!futureListT.isCompleted()){
32
-                Thread.sleep(100);
34
+            while (!futureListT.isCompleted()) {
35
+                TimeUnit.MILLISECONDS.sleep(300);
33 36
             }
34 37
             completableFuture.complete(futureListT.value().get().get());
35 38
             return null;

+ 3
- 3
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala Zobrazit soubor

@@ -7,10 +7,10 @@ import scala.concurrent.Future
7 7
 
8 8
 //noinspection SpellCheckingInspection,AccessorLikeMethodIsUnit
9 9
 trait AkkaStreamFileProcessingFuture {
10
-  def getPersonByIdFuture(personID: String): Future[Person]
11
-  def getPersonByNameFuture(primaryName: String): Future[Person]
10
+  def getPersonByIdFuture(personID: String): Future[Option[Person]]
11
+  def getPersonByNameFuture(primaryName: String):  Future[Option[Person]]
12 12
 
13
-  def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie]
13
+  def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]]
14 14
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
15 15
 
16 16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]

+ 14
- 32
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Zobrazit soubor

@@ -6,23 +6,11 @@ import com.typesafe.scalalogging.slf4j.Logger
6 6
 import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, titleBasics}
7 7
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
8 8
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
9
-  actorSystem,
10
-  buildAllPersonsSink,
11
-  buildAllTvSeriesSink,
12
-  buildAndValidateSource,
13
-  buildPersonFlow,
14
-  buildSource,
15
-  buildTvSerieFlow,
16
-  filterPersonByIdFlow,
17
-  filterPersonByNameFlow,
18
-  filterTvSerieByIdFlow,
19
-  filterTvSerieByPrimaryTitleFlow
9
+  actorSystem, buildAllPersonsSink, buildAllTvSeriesSink, buildAndValidateSource, buildPersonFlow, buildSource,
10
+  buildTvSerieFlow, filterPersonByIdFlow, filterPersonByNameFlow, filterTvSerieByIdFlow, filterTvSerieByPrimaryTitleFlow
20 11
 }
21 12
 import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{
22
-  getListOfPersonsForTvSerie,
23
-  getListOfPersonsIDByTvSerieID,
24
-  getTvSerieIDFuture,
25
-  getTvSerieIdByPrimaryTitle
13
+  getListOfPersonsForTvSerie, getListOfPersonsIDByTvSerieID, getTvSerieIDFuture, getTvSerieIdByPrimaryTitle
26 14
 }
27 15
 import org.slf4j.LoggerFactory
28 16
 import org.springframework.stereotype.Component
@@ -38,17 +26,17 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
38 26
 
39 27
   implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
40 28
 
41
-  override def getPersonByIdFuture(personID: String): Future[Person] = {
29
+  override def getPersonByIdFuture(personID: String): Future[Option[Person]] = {
42 30
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
43 31
 
44 32
     val start: Long = System.currentTimeMillis()
45
-    val personFuture: Future[Person] = source
33
+    val personFuture: Future[Option[Person]] = source
46 34
       .via(flow = filterPersonByIdFlow(personID = personID))
47
-      .runWith(Sink.head)
35
+      .runWith(Sink.headOption)
48 36
 
49 37
     personFuture.andThen({
50 38
       case Failure(exception) => logger.info(s"$exception")
51
-      case Success(value:Person) =>
39
+      case Success(value:Option[Person]) =>
52 40
         logger.info(s"$value")
53 41
         logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
54 42
     })
@@ -56,34 +44,28 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
56 44
     personFuture
57 45
   }
58 46
 
59
-  override def getPersonByNameFuture(primaryName: String): Future[Person] = {
47
+  override def getPersonByNameFuture(primaryName: String):  Future[Option[Person]]= {
60 48
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
61 49
 
62 50
     val start: Long = System.currentTimeMillis()
63
-    val personFuture: Future[Person] = source
51
+    val personFuture:  Future[Option[Person]] = source
64 52
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
65
-      .runWith(Sink.head)
66
-
67
-    personFuture.onComplete({
68
-      case Failure(exception) => logger.error(s"$exception")
69
-      case Success(value: Person) => logger.info(s"$value")
70
-        logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
71
-    })
53
+      .runWith(Sink.headOption)
72 54
 
73 55
     personFuture
74 56
   }
75 57
 
76
-  override def getTvSerieByIdFuture(tvSerieID: String): Future[TvSerie] = {
58
+  override def getTvSerieByIdFuture(tvSerieID: String): Future[Option[TvSerie]] = {
77 59
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
78 60
 
79 61
     val start: Long = System.currentTimeMillis()
80
-    val tvSerieFuture: Future[TvSerie] = source
62
+    val tvSerieFuture: Future[Option[TvSerie]]= source
81 63
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
82
-      .runWith(Sink.head)
64
+      .runWith(Sink.headOption)
83 65
 
84 66
     tvSerieFuture.onComplete({
85 67
       case Failure(exception) => logger.info(s"$exception")
86
-      case Success(value: TvSerie) =>
68
+      case Success(value: Option[TvSerie]) =>
87 69
         logger.info(s"$value")
88 70
         logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
89 71
     })

Powered by TurnKey Linux.