No Description

AkkaStreamFileProcessingImpl.scala 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package fr.natan.akkastreamfileprocessingapi.service
  2. import akka.actor.ActorSystem
  3. import akka.stream.scaladsl.{Flow, Sink, Source}
  4. import akka.{Done, NotUsed}
  5. import com.typesafe.scalalogging.slf4j.Logger
  6. import fr.natan.akkastreamfileprocessingapi.datasource.Datasource.titleBasics
  7. import fr.natan.akkastreamfileprocessingapi.models.Movie
  8. import fr.natan.akkastreamfileprocessingapi.service.AkkaStreamComponents.{buildAndValidateSource, buildMovieFlow, buildSink, filterByMoviePrimaryTitleFlow}
  9. import org.slf4j.LoggerFactory
  10. import org.springframework.stereotype.Component
  11. import scala.concurrent.ExecutionContext.Implicits.global
  12. import scala.concurrent.Future
  13. import scala.util.{Failure, Success}
  14. @Component
  15. class AkkaStreamFileProcessingImpl extends AkkaStreamFileProcessing {
  16. implicit val actorSystem: ActorSystem = ActorSystem("AkkaStreamActor")
  17. implicit val logger: Logger = Logger(LoggerFactory.getLogger(this.getClass))
  18. override def getAllMovies(): List[Movie] = {
  19. val source: Source[String, NotUsed] = buildAndValidateSource(inputFile = titleBasics)
  20. val sink: Sink[Movie, Future[Done]] = buildSink(logger = logger)
  21. val movieFlow: Flow[String, Movie, NotUsed] = buildMovieFlow(movieID = "tconst")
  22. val startingTime: Long = System.currentTimeMillis()
  23. source.via(flow = movieFlow).runWith(sink = sink)
  24. .andThen {
  25. case Success(value) =>
  26. val elapsedTime: Long = (System.currentTimeMillis() - startingTime) / 1000
  27. logger.info(s"$value: successfully processing file, elapsed time $titleBasics: $elapsedTime sec")
  28. case Failure(error: Error) => logger.error(s"$error")
  29. }
  30. }
  31. override def getMoviesByTitle(moviePrimaryTitle: String): Unit = {
  32. val source: Source[String, NotUsed] = buildAndValidateSource(inputFile = titleBasics)
  33. val sink: Sink[Movie, Future[Done]] = buildSink(logger = logger)
  34. val filterByMovieTitleFlow: Flow[String, Movie, NotUsed] =
  35. filterByMoviePrimaryTitleFlow(moviePrimaryTitle = moviePrimaryTitle)
  36. val startTime: Long = System.currentTimeMillis()
  37. source.via(flow = filterByMovieTitleFlow).runWith(sink = sink).andThen {
  38. case Success(value) =>
  39. val elapsedTime: Long = (System.currentTimeMillis() - startTime) / 1000
  40. logger.info(s"$value : successfully processing file, elapsed time $elapsedTime sec")
  41. case Failure(err: Error) => logger.error(s"$err")
  42. }
  43. }
  44. }

Powered by TurnKey Linux.