Akka streams in web application

57 views
Skip to first unread message

Madabhattula Rajesh Kumar

unread,
Sep 29, 2016, 7:29:59 AM9/29/16
to Akka User List
Hi,

I am planning to use Akka Streams in web application. Below is my use case.

Whenever the request come to web server, I will do some operations like stg01, stg02 ...

I have a below questions :-
  • Do we need to close the streams after each request processed ?
  • If N multiple requests come, below invoke method will execute in N times with parallel. In this case, how the actors will create and close ?

Below is the example code snippet :-

  implicit val actorSystem = ActorSystem("Example")
  implicit val materializer = ActorMaterializer()
  implicit val executor = actorSystem.dispatcher
  val ll = scala.collection.mutable.ListBuffer[String]()
  ll += "123"; ll += "456"; ll += "789"
  invoke(ll.toList)
 
  def invoke(customerId: List[String]) {
    val source = Source(customerId)
    val sink = Sink.foreach[String] { x => display(x) }
    val stg01 = Flow[String].map { x => getStg01(x) }
    val stg02 = Flow[Int].map { x => getStg02(x) }

    val runnable = source via stg01 via stg02 to sink
   
    runnable.run

  }
  def getStg01(request: String) = request.toInt
  def getStg02(request: Int) = request.toString
  def display(request: String) = println(" EVENT ===> " + request)
}

Akka Team

unread,
Oct 7, 2016, 11:30:26 AM10/7/16
to Akka User List
As you have a finite stream of customer ids the stream will complete after all elements has been processed. If your actual business problem does in fact look like this though, it may make sense to not involve Akka Streams at all and just use the regular Scala collection API to achieve the same.

Also, note that you should probably not create a new Materializer for every request but instead create a single instance and reuse that across requests, as the materializer does have a lifecycle and needs to be stopped if it should have a shorter lifecycle than the ActorSystem it was created with.

--
Johan
Akka Team

--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+unsubscribe@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages