1

Just getting my head around Mutiny API (and java stream api)...

I have the following code that reads messages off an AWS SQS queue, ref: quarkus sqs guide

  Uni<List<Quark>> result =Uni.createFrom()
    .completionStage(sqs.receiveMessage(m -> m.maxNumberOfMessages(10).queueUrl(queueUrl)))
    .onItem().transform(ReceiveMessageResponse::messages)
    .onItem().transform(m -> m.stream().map(Message::body).map(this::toQuark).collect(Collectors.toList()));

Next I want to send each element in the list to a method handleMessage(Quark quark). How do I do this in a "mutiny way!". Do i need to transform again or should not collect..or ?

user1843591
  • 1,074
  • 3
  • 17
  • 37

1 Answers1

1

At the moment, you get a Uni<List<Quark>>. The Mutiny way would be to transform this into a Multi and process each item:

Multi<Quark> multi = result.onItem().transformToMulti(list -> Multi.createFrom().items(list));

A Multi is a stream. Each item will be a Quark. Then, you just need to do the following:

multi.onItem().invoke(q -> handleMessage(q))

I used invoke because I don't know what handleMessage is doing. If it's processing the Quark and returning something, use transform. If it does not return anything, use invoke.

BTW, do not forget to subscribe to the returned Multi.

Clement
  • 2,817
  • 1
  • 12
  • 11