Skip to content

Instantly share code, notes, and snippets.

@frank9615
Created November 25, 2022 21:12
Show Gist options
  • Save frank9615/374352d9d6cb447df1dd876896b58fc7 to your computer and use it in GitHub Desktop.
Save frank9615/374352d9d6cb447df1dd876896b58fc7 to your computer and use it in GitHub Desktop.
package com.example;
import io.quarkus.runtime.annotations.QuarkusMain;
import io.smallrye.mutiny.Context;
import io.smallrye.mutiny.Multi;
import io.smallrye.mutiny.Uni;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.util.stream.Collectors;
@QuarkusMain
@Slf4j
public class ExampleResource {
public static void main(String[] args) throws IOException {
Context context = Context.empty();
Multi.createFrom().range(0,10)
.withContext((integerMulti, context1) ->
integerMulti
.onItem()
.call(integer -> {
if (integer == 1) {
context.put("cycle-element", integer);
return Uni.createFrom().failure(new Throwable("prova"));
}
return Uni.createFrom().item(integer);
})
)
.onItem()
.call(integer -> {
System.out.println("Element cycled is:" + integer);
return Uni.createFrom().item(integer);
}).onFailure().recoverWithCompletion()
.collect()
.with(Collectors.summingInt(value -> {
return value.intValue();
})).onItem().transform(integer -> {
if(context.isEmpty()){
return integer;
}
System.out.println(context.<Integer>get("cycle-element"));
return context.<Integer>get("cycle-element");
})
.onItem().call(integer -> {
System.out.println("Last item is: " + integer );
return Uni.createFrom().item(integer);
})
.await().indefinitely();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment