Hi Vladimir,

it is possible to complete the request with a Source (no materialization
needed) if you provide a marshaller for the source. You need marshaller in
this case basically only to set the content type of your response.

In your case, you would prepare a Source with:

val mySource = 
Source.actorPublisher[UserRegisterSource.RegisterUser](UserRegisterSource.props)
  .mapAsync(1)(fakeSaveUserAndReturnCreatedUserId)

and then use mySource to complete the request.

You can find an example of marshaller for a Source in the akka-see
<https://github.com/hseeberger/akka-sse/blob/0f35dc492f75a03e9091be5c7d0e04ff45f3f0de/akka-sse/src/main/scala/de/heikoseeberger/akkasse/EventStreamMarshalling.scala#L40>
project.

On Tue, Jun 9, 2015 at 2:55 PM, Владимир Морозов <[email protected]>
wrote:

> Hi,
>
> I try to write complete code example:
>
> import akka.actor.{ActorSystem, Props}import akka.http.scaladsl._import 
> akka.http.scaladsl.server.Directives._import 
> akka.http.scaladsl.server._import akka.stream.ActorFlowMaterializerimport 
> akka.stream.actor.ActorPublisherimport akka.stream.scaladsl.{Sink, Source}
> import scala.annotation.tailrecimport scala.concurrent.Future
> object UserRegisterSource {
>   def props: Props = Props[UserRegisterSource]
>
>   final case class RegisterUser(username: String)
> }
> class UserRegisterSource extends 
> ActorPublisher[UserRegisterSource.RegisterUser] {
>
>   import UserRegisterSource._
>   import akka.stream.actor.ActorPublisherMessage._
>
>   val MaxBufferSize = 100
>   var buf = Vector.empty[RegisterUser]
>
>   override def receive: Receive = {
>     case request: RegisterUser =>
>       if (buf.isEmpty && totalDemand > 0)
>         onNext(request)
>       else {
>         buf :+= request
>         deliverBuf()
>       }
>     case Request(_) =>
>       deliverBuf()
>     case Cancel =>
>       context.stop(self)
>   }
>
>   @tailrec final def deliverBuf(): Unit =
>     if (totalDemand > 0) {
>       if (totalDemand <= Int.MaxValue) {
>         val (use, keep) = buf.splitAt(totalDemand.toInt)
>         buf = keep
>         use foreach onNext
>       } else {
>         val (use, keep) = buf.splitAt(Int.MaxValue)
>         buf = keep
>         use foreach onNext
>         deliverBuf()
>       }
>     }}
> object Main extends App {
>   val host = "127.0.0.1"
>   val port = 8094
>
>   implicit val system = ActorSystem("my-testing-system")
>   implicit val fm = ActorFlowMaterializer()
>   implicit val executionContext = system.dispatcher
>
>   val serverSource: Source[Http.IncomingConnection, 
> Future[Http.ServerBinding]] = Http(system).bind(interface = host, port = port)
>
>   val mySource = 
> Source.actorPublisher[UserRegisterSource.RegisterUser](UserRegisterSource.props)
>   val requestProcessor = mySource
>     .mapAsync(1)(fakeSaveUserAndReturnCreatedUserId)
>     .to(Sink.head[Int])
>     .run()
>
>   val route: Route =
>     get {
>       path("test") {
>         parameter('test) { case t: String =>
>           requestProcessor ! UserRegisterSource.RegisterUser(t)
>
>           ???
>         }
>       }
>     }
>
>   def fakeSaveUserAndReturnCreatedUserId(param: 
> UserRegisterSource.RegisterUser): Future[Int] =
>     Future.successful {
>       1
>     }
>
>   serverSource.to(Sink.foreach {
>     connection =>
>       connection handleWith Route.handlerFlow(route)
>   }).run()}
>
>
> I want return(obtain) Int value from stream in place with ??? in my code
> example.
>
> Main Idea:
> 1) akka-http accept request for registering user
> 2) Then code I call mySource actor -  do user registration
> 3) I want to answer with user Id to HttpClient that call registration
>
> Now I have code that cover first two step and I need third...
>
> PS: I hope I have described well enough that I want, and that it does not
> get done
>
> вторник, 9 июня 2015 г., 14:46:11 UTC+3 пользователь √ написал:
>>
>> Hi,
>>
>> I don't understand what you mean. What does "Does not solve my problem"
>> mean in this case?
>> What -is- the result of the flow?
>>
>> On Tue, Jun 9, 2015 at 9:30 AM, Владимир Морозов <[email protected]>
>> wrote:
>>
>>> Hi,
>>>
>>> Oh... but if I change Sink.ignore to Sink.head[String] (for example) it
>>> not solve my problem, because ref (my stream) have type ActorRef.
>>> What type of Sink I need to use, for obtain result of flow, if as source
>>> used actorPublisher?
>>>
>>> вторник, 9 июня 2015 г., 10:23:41 UTC+3 пользователь √ написал:
>>>>
>>>> The results will travel to the Sink, so you'll have to not `ignore` the
>>>> result.
>>>>
>>>> On Tue, Jun 9, 2015 at 9:15 AM, Владимир Морозов <[email protected]>
>>>> wrote:
>>>>
>>>>> Hi,
>>>>>
>>>>> I play more with actorPublisher, yes it is great thing, but - when I
>>>>> have stream like this:
>>>>>
>>>>> val jobManagerSource = Source.actorPublisher[UserRegisterSource.
>>>>> RegisterUser](UserRegisterSource.props)
>>>>>
>>>>> val ref = Flow[UserRegisterSource.RegisterUser]
>>>>> .mapAsync(1)(callMe)
>>>>> .to(Sink.ignore)
>>>>> .runWith(jobManagerSource)
>>>>>
>>>>> ref ? UserRegisterSource.RegisterUser("test")
>>>>>
>>>>> And then ask it, like in previous message, in ask result I see what
>>>>> jobManagerSource answer me. I can't understand how I can send some
>>>>> value to actorPublisher source and then obtain result of flow
>>>>> execution
>>>>>
>>>>> понедельник, 8 июня 2015 г., 1:10:01 UTC+3 пользователь Владимир
>>>>> Морозов написал:
>>>>>
>>>>>> I solve this problem, but found new one:
>>>>>>
>>>>>> val ref = Flow[UserRegisterSource.RegisterUser]
>>>>>> .mapAsync(1)(callMe)
>>>>>> .to(Sink.ignore)
>>>>>> .runWith(jobManagerSource)
>>>>>>
>>>>>> val t = (ref ask
>>>>>> UserRegisterSource.RegisterUser("test")).mapTo[UserRegisterSource.RegisterUser]
>>>>>>
>>>>>> but t never complete, always end with Timeout exception
>>>>>>
>>>>>> воскресенье, 7 июня 2015 г., 23:13:29 UTC+3 пользователь Владимир
>>>>>> Морозов написал:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> I try but get error message:
>>>>>>>
>>>>>>> java.lang.IllegalStateException: onNext is not allowed when the
>>>>>>> stream has not requested elements, totalDemand was 0
>>>>>>>
>>>>>>> My code is:
>>>>>>>
>>>>>>> val jobManagerSource =
>>>>>>> Source.actorPublisher[UserRegisterSource.RegisterUser](UserRegisterSource.props)
>>>>>>>
>>>>>>> val ref = Flow[UserRegisterSource.RegisterUser]
>>>>>>> .mapAsync(1)(callMe)
>>>>>>> .to(Sink.ignore)
>>>>>>> .runWith(jobManagerSource)
>>>>>>>
>>>>>>> ref ! UserRegisterSource.RegisterUser("test")
>>>>>>>
>>>>>>>
>>>>>>> class UserRegisterSource extends
>>>>>>> ActorPublisher[UserRegisterSource.RegisterUser] {
>>>>>>>
>>>>>>>     import akka.stream.actor.ActorPublisherMessage._
>>>>>>>     import UserRegisterSource._
>>>>>>>
>>>>>>>     val MaxBufferSize = 100
>>>>>>>     var buf = Vector.empty[RegisterUser]
>>>>>>>
>>>>>>>     override def receive: Receive = {
>>>>>>>         case request: RegisterUser =>
>>>>>>>             onNext(request)
>>>>>>>         case Cancel =>
>>>>>>>             context.stop(self)
>>>>>>>     }
>>>>>>> }
>>>>>>>
>>>>>>>
>>>>>>> воскресенье, 7 июня 2015 г., 14:53:15 UTC+3 пользователь Akka Team
>>>>>>> написал:
>>>>>>>>
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> On Fri, Jun 5, 2015 at 7:53 PM, Владимир Морозов <
>>>>>>>> [email protected]> wrote:
>>>>>>>>
>>>>>>>>> Yes, I know about mapAsync, but my problem with Source. A want to
>>>>>>>>> use single stream for processing some group of events
>>>>>>>>>
>>>>>>>>
>>>>>>>> I am not sure if I understand your question properly, but if you
>>>>>>>> want to run the stream *once* and then reuse the stream for all the
>>>>>>>> requests, then you can try to create an ActorPublisher as your Source, 
>>>>>>>> then
>>>>>>>> run the stream outside of the Http handler logic. You can then use 
>>>>>>>> "ask" on
>>>>>>>> that actor in the Http route.
>>>>>>>>
>>>>>>>> -Endre
>>>>>>>>
>>>>>>>>
>>>>>>>>>
>>>>>>>>> пятница, 5 июня 2015 г., 20:29:16 UTC+3 пользователь Paul Kinsky
>>>>>>>>> написал:
>>>>>>>>>
>>>>>>>>>> Use mapAsync: `def mapAsync[T](parallelism: Int)(f: (Out) ⇒
>>>>>>>>>> Future[T]): Repr[T, Mat]`.
>>>>>>>>>>
>>>>>>>>>> On Friday, June 5, 2015 at 9:23:24 AM UTC-7, Владимир Морозов
>>>>>>>>>> wrote:
>>>>>>>>>>>
>>>>>>>>>>> Hi all,
>>>>>>>>>>>
>>>>>>>>>>> I have some simple application based on akka streams and http:
>>>>>>>>>>>
>>>>>>>>>>> My Flow items:
>>>>>>>>>>>
>>>>>>>>>>> val resultSink = Sink.head[String]
>>>>>>>>>>>
>>>>>>>>>>> val fl0 = Flow[String].map(_.toInt)
>>>>>>>>>>> val fl2 = Flow[Int].map{
>>>>>>>>>>>     case value =>
>>>>>>>>>>>         Thread.sleep(10000)
>>>>>>>>>>>         value.toString
>>>>>>>>>>> }
>>>>>>>>>>> val fl3 = Flow[String].mapAsync(1)(callMe)
>>>>>>>>>>>
>>>>>>>>>>>
>>>>>>>>>>> Part of my akka-http route:
>>>>>>>>>>>
>>>>>>>>>>> val route = get {
>>>>>>>>>>>     path("test") {
>>>>>>>>>>>         path("register") {
>>>>>>>>>>>             parameter('username) { case username: Username =>
>>>>>>>>>>>                 
>>>>>>>>>>> Source.single(username).via(fl0).via(fl2).via(fl3).runWith(resultSink):
>>>>>>>>>>> Future[String]
>>>>>>>>>>>         }
>>>>>>>>>>>     }
>>>>>>>>>>> }
>>>>>>>>>>>
>>>>>>>>>>> I want to use one flow : Source.single(username).
>>>>>>>>>>> via(fl0).via(fl2).via(fl3).runWith(resultSink) for processing
>>>>>>>>>>> all requests. I want this for use streams back-pressure. But I 
>>>>>>>>>>> can't figure
>>>>>>>>>>> out how create Source that can accept values like call def
>>>>>>>>>>> proccess(username: String): Future[String] but with
>>>>>>>>>>> back-pressure, because, for example, a want long-call DB in fl2
>>>>>>>>>>> step, and DB can't accept more than 1 request per time (only for 
>>>>>>>>>>> example)
>>>>>>>>>>>
>>>>>>>>>>> With best regards, Vladimir.
>>>>>>>>>>>
>>>>>>>>>>>  --
>>>>>>>>> >>>>>>>>>> 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 [email protected].
>>>>>>>>> To post to this group, send email to [email protected].
>>>>>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> --
>>>>>>>> Akka Team
>>>>>>>> Typesafe - Reactive apps on the JVM
>>>>>>>> Blog: letitcrash.com
>>>>>>>> Twitter: @akkateam
>>>>>>>>
>>>>>>>  --
>>>>> >>>>>>>>>> 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 [email protected].
>>>>> To post to this group, send email to [email protected].
>>>>> Visit this group at http://groups.google.com/group/akka-user.
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>>
>>>>
>>>>
>>>> --
>>>> Cheers,
>>>> √
>>>>
>>>  --
>>> >>>>>>>>>> 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 [email protected].
>>> To post to this group, send email to [email protected].
>>> Visit this group at http://groups.google.com/group/akka-user.
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Cheers,
>> √
>>
>  --
> >>>>>>>>>> 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 [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/akka-user.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Martynas Mickevičius
Typesafe <http://typesafe.com/> – Reactive
<http://www.reactivemanifesto.org/> Apps on the JVM

-- 
>>>>>>>>>>      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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to