Browse Source

first commit

placidenduwayo 1 year ago
parent
commit
3b03899427

+ 9
- 5
src/main/java/fr/natan/akkastreamfileprocessingapi/controller/TvSeriesController.java View File

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

+ 15
- 12
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
-
3
+import akka.util.Timeout;
4
+import scala.Option;
4
 import scala.collection.IndexedSeq;
5
 import scala.collection.IndexedSeq;
6
+import scala.concurrent.Await;
5
 import scala.concurrent.Future;
7
 import scala.concurrent.Future;
6
 
8
 
7
-import java.util.Random;
9
+import java.time.Duration;
8
 import java.util.concurrent.CompletableFuture;
10
 import java.util.concurrent.CompletableFuture;
9
 import java.util.concurrent.Executors;
11
 import java.util.concurrent.Executors;
10
-
12
+import java.util.concurrent.TimeUnit;
13
+import java.util.concurrent.TimeoutException;
11
 
14
 
12
 
15
 
13
 public class CompletableFutureBuilder<T> {
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
         CompletableFuture<T> completableFuture = new CompletableFuture<>();
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
             return null;
25
             return null;
23
         });
26
         });
24
 
27
 
25
         return completableFuture;
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
         CompletableFuture<IndexedSeq<T>> completableFuture = new CompletableFuture<>();
32
         CompletableFuture<IndexedSeq<T>> completableFuture = new CompletableFuture<>();
30
         Executors.newSingleThreadExecutor().submit(() -> {
33
         Executors.newSingleThreadExecutor().submit(() -> {
31
-            while(!futureListT.isCompleted()){
32
-                Thread.sleep(100);
34
+            while (!futureListT.isCompleted()) {
35
+                TimeUnit.MILLISECONDS.sleep(300);
33
             }
36
             }
34
             completableFuture.complete(futureListT.value().get().get());
37
             completableFuture.complete(futureListT.value().get().get());
35
             return null;
38
             return null;

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

7
 
7
 
8
 //noinspection SpellCheckingInspection,AccessorLikeMethodIsUnit
8
 //noinspection SpellCheckingInspection,AccessorLikeMethodIsUnit
9
 trait AkkaStreamFileProcessingFuture {
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
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
14
   def getTvSeriesByPrimaryTitleFuture(tvSerieTitle: String):Future[IndexedSeq[TvSerie]]
15
 
15
 
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]
16
   def getPersonsForTvSerieByTvSerieTitleFuture(tvSeriePrimaryTitle: String): Future[IndexedSeq[Person]]

+ 14
- 32
src/main/scala/fr/natan/akkastreamfileprocessingapi/service/AkkaStreamFileProcessingImpl.scala View File

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

Powered by TurnKey Linux.