Quantcast
Channel: Akka Libraries - Discussion Forum for Akka technologies
Viewing all articles
Browse latest Browse all 1362

Convert a Flow[In, Out, Mat] into a Flow[In, Try[Out], NotUsed]

$
0
0

Hello,

I want to convert a Flow[In, Out, Mat] into a Flow[In, Try[Out]], NotUsed]. The following works for me:

  def `try`[In, Out, Mat](flow: Flow[In, Out, Mat])(implicit ec: ExecutionContext, materializer: Materializer): Flow[In, Try[Out], NotUsed] = {
    def fut(in: In): Future[Try[Out]] = {
      Source.single(in).via(flow).map(Success(_)).runWith(Sink.last)
        .recover[Try[Out]] {
          case t: Throwable => Failure(t)
        }
    }
    Flow[In].mapAsync(1)(fut)
  }

but it involves a mapAsync which I would like to avoid.
I tried writing a graph stage based on Recover, but that turned out to be a dead end because by the time it is executed
the in port is already closed. Using a SupervisionStrategy did not help either. Is there a better solution?

Thanks for any suggestion!

1 post - 1 participant

Read full topic


Viewing all articles
Browse latest Browse all 1362

Trending Articles