placidenduwayo vor 2 Jahren
Ursprung
Commit
94ea3bebfd

+ 7
- 7
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java Datei anzeigen

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

+ 2
- 2
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala Datei anzeigen

@@ -15,6 +15,6 @@ trait AkkaStreamFileProcessingFuture {
15 15
 
16 16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
17 17
   def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]]
18
-  def getAllTvSeriesFuture(): Future[Done]
19
-  def getAllPersonsFuture(): Future[Done]
18
+  def getAllTvSeriesFuture: Future[Done]
19
+  def getAllPersonsFuture: Future[Done]
20 20
 }

+ 21
- 18
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala Datei anzeigen

@@ -7,11 +7,9 @@ import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.{nameBasics, t
7 7
 import fr.natan.akkastreamfileprocessingapi.models.Models.{Person, TvSerie}
8 8
 import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{
9 9
   actorSystem, buildAllPersonsSink, buildAllTvSeriesSink, buildAndValidateSource, buildPersonFlow, buildSource,
10
-  buildTvSerieFlow, filterPersonByIdFlow, filterPersonByNameFlow, filterTvSerieByIdFlow, filterTvSerieByPrimaryTitleFlow
11
-}
10
+  buildTvSerieFlow, filterPersonByIdFlow, filterPersonByNameFlow, filterTvSerieByIdFlow, filterTvSerieByPrimaryTitleFlow}
12 11
 import fr.natan.akkastreamfileprocessingapi.service.UtilitiesClass.{
13
-  getListOfPersonsForTvSerie, getListOfPersonsIDByTvSerieID, getTvSerieIDFuture, getTvSerieIdByPrimaryTitle
14
-}
12
+  getListOfPersonsForTvSerie, getListOfPersonsIDByTvSerieID, getTvSerieIDFuture, getTvSerieIdByPrimaryTitle}
15 13
 import org.slf4j.LoggerFactory
16 14
 import org.springframework.stereotype.Component
17 15
 
@@ -32,11 +30,11 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
32 30
     val start: Long = System.currentTimeMillis()
33 31
     val personFuture: Future[Option[Person]] = source
34 32
       .via(flow = filterPersonByIdFlow(personID = personID))
35
-      .runWith(Sink.headOption)
33
+      .runWith(Sink.headOption[Person])
36 34
 
37 35
     personFuture.andThen({
38 36
       case Failure(exception) => logger.info(s"$exception")
39
-      case Success(value:Option[Person]) =>
37
+      case Success(value: Option[Person]) =>
40 38
         logger.info(s"$value")
41 39
         logger.info(s"SUCCESS, elapsed time:${(System.currentTimeMillis() - start) / 1000} sec")
42 40
     })
@@ -44,13 +42,13 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
44 42
     personFuture
45 43
   }
46 44
 
47
-  override def getPersonByNameFuture(primaryName: String):  Future[Option[Person]]= {
45
+  override def getPersonByNameFuture(primaryName: String): Future[Option[Person]] = {
48 46
     val source: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
49 47
 
50 48
     val start: Long = System.currentTimeMillis()
51
-    val personFuture:  Future[Option[Person]] = source
49
+    val personFuture: Future[Option[Person]] = source
52 50
       .via(flow = filterPersonByNameFlow(primaryName = primaryName))
53
-      .runWith(Sink.headOption)
51
+      .runWith(Sink.headOption[Person])
54 52
 
55 53
     personFuture
56 54
   }
@@ -59,9 +57,9 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
59 57
     val source: Source[Map[String, String], _] = buildSource(inputFile = titleBasics)
60 58
 
61 59
     val start: Long = System.currentTimeMillis()
62
-    val tvSerieFuture: Future[Option[TvSerie]]= source
60
+    val tvSerieFuture: Future[Option[TvSerie]] = source
63 61
       .via(flow = filterTvSerieByIdFlow(tvSerieID = tvSerieID))
64
-      .runWith(Sink.headOption)
62
+      .runWith(Sink.headOption[TvSerie])
65 63
 
66 64
     tvSerieFuture.onComplete({
67 65
       case Failure(exception) => logger.info(s"$exception")
@@ -71,6 +69,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
71 69
     })
72 70
     tvSerieFuture
73 71
   }
72
+
74 73
   override def getTvSeriesByPrimaryTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[TvSerie]] = {
75 74
 
76 75
     val start: Long = System.currentTimeMillis()
@@ -102,11 +101,12 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
102 101
         case Success(value: Option[String]) =>
103 102
           logger.info(s"TvSerie ID: $value")
104 103
           logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
104
+
105 105
       })
106 106
         .flatMap({
107 107
           _ =>
108 108
             logger.info("STEP 2/3 START")
109
-            val  start2: Long = System.currentTimeMillis()
109
+            val start2: Long = System.currentTimeMillis()
110 110
             val listPersonIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
111 111
               tvSerieIdFuture = tvSerieIDFuture)
112 112
             listPersonIDsFuture.andThen({
@@ -140,15 +140,16 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
140 140
     val tvSerieIdFuture: Future[Option[String]] = getTvSerieIDFuture(tvSerieId = tvSerieId)
141 141
     tvSerieIdFuture.andThen({
142 142
       case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
143
-      case Success(value:Option[String]) =>
143
+      case Success(value: Option[String]) =>
144 144
         logger.info(s"$value")
145 145
         logger.info(s"STEP 1/3 END, elapsed time:${(System.currentTimeMillis() - start1) / 1000} sec")
146 146
 
147 147
     })
148 148
       .flatMap({
149
-        _ => logger.info("STEP 2/3")
149
+        _ =>
150
+          logger.info("STEP 2/3")
150 151
           val start2: Long = System.currentTimeMillis()
151
-          val  listOfPersonsIDsFuture : Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
152
+          val listOfPersonsIDsFuture: Future[IndexedSeq[Option[String]]] = getListOfPersonsIDByTvSerieID(
152 153
             tvSerieIdFuture = tvSerieIdFuture)
153 154
           listOfPersonsIDsFuture.andThen({
154 155
             case Failure(exception) => logger.error(s"${exception.printStackTrace()}")
@@ -157,7 +158,8 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
157 158
               logger.info(s"STEP 2/3 END, elapsed time:${(System.currentTimeMillis() - start2) / 1000} sec")
158 159
           })
159 160
             .flatMap({
160
-              _ => logger.info("STEP 3/3")
161
+              _ =>
162
+                logger.info("STEP 3/3")
161 163
                 val start3: Long = System.currentTimeMillis()
162 164
                 val personsTeamFuture: Future[IndexedSeq[Person]] = getListOfPersonsForTvSerie(
163 165
                   listPersonsIDsFuture = listOfPersonsIDsFuture)
@@ -171,7 +173,8 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
171 173
       })
172 174
 
173 175
   }
174
-  override def getAllPersonsFuture(): Future[Done] = {
176
+
177
+  override def getAllPersonsFuture: Future[Done] = {
175 178
     val personSource: Source[Map[String, String], _] = buildSource(inputFile = nameBasics)
176 179
     //graph
177 180
     val startTime: Long = System.currentTimeMillis()
@@ -190,7 +193,7 @@ class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessingFuture {
190 193
     result
191 194
   }
192 195
 
193
-  override def getAllTvSeriesFuture(): Future[Done] = {
196
+  override def getAllTvSeriesFuture: Future[Done] = {
194 197
     val source: Source[Map[String, String], _] = buildAndValidateSource(inputFile = titleBasics)
195 198
     val sink: Sink[TvSerie, Future[Done]] = buildAllTvSeriesSink(logger = logger)
196 199
 

Powered by TurnKey Linux.