Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
94ea3bebfd

+ 7
- 7
src/main/java/fr/natan/akkastreamfileprocessingapi/futurecompleteness/CompletableFutureBuilder.java View File

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

+ 2
- 2
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingFuture.scala View File

15
 
15
 
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
17
   def getPersonsForTvSerieByTvSerieIDFuture(tvSerieId: String): Future[IndexedSeq[Person]]
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 View File

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

Powered by TurnKey Linux.