use of io.smallrye.mutiny.Multi in project smallrye-reactive-messaging by smallrye.
the class ReactiveKafkaConsumerTest method testGroupingRecordsByPartition.
@Test
// seems to fail on CI once in a while
@Tag(TestTags.FLAKY)
public void testGroupingRecordsByPartition() throws Exception {
int count = 10000;
String groupId = UUID.randomUUID().toString();
MapBasedConfig config = createConsumerConfig(groupId).put("topic", topic);
KafkaSource<Integer, String> source = createSource(config, groupId);
Multi<IncomingKafkaRecord<Integer, String>> stream = source.getStream();
CountDownLatch latch = new CountDownLatch(count);
AtomicInteger concurrentPartitionExecutions = new AtomicInteger();
Map<Integer, String> inProgressMap = new ConcurrentHashMap<>();
int maxProcessingMs = 5;
this.receiveTimeoutMillis = maxProcessingMs * count + 5000;
stream.group().by(IncomingKafkaRecord::getPartition).subscribe().with(recFromPartition -> recFromPartition.emitOn(Infrastructure.getDefaultWorkerPool()).subscribe().with(record -> {
int partition = record.getPartition();
String current = Thread.currentThread().getName() + ":" + record.getOffset();
String inProgress = inProgressMap.putIfAbsent(partition, current);
if (inProgress != null) {
concurrentPartitionExecutions.incrementAndGet();
}
onReceive(record);
latch.countDown();
record.ack();
inProgressMap.remove(partition);
}));
waitForPartitionAssignment();
sendMessages(0, count);
waitForMessages(latch);
assertThat(concurrentPartitionExecutions.get()).isEqualTo(0);
checkConsumedMessages(0, count);
}
use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.
the class ReflectionInvoker method invoke.
public <T> T invoke(Object... arguments) throws Exception {
if (this.injectContextAt > -1) {
arguments = injectContext(arguments);
}
try {
ManagedInstance<?> operationInstance = lookupService.getInstance(operationClass);
Object operationInstance1 = operationInstance.get();
eventEmitter.fireBeforeMethodInvoke(new InvokeInfo(operationInstance1, method, arguments));
T result = (T) method.invoke(operationInstance1, arguments);
if (result instanceof Uni) {
return (T) ((Uni) result).onTermination().invoke(() -> {
operationInstance.destroyIfNecessary();
});
} else if (result instanceof Multi) {
return (T) ((Multi) result).onTermination().invoke(() -> {
operationInstance.destroyIfNecessary();
});
} else {
operationInstance.destroyIfNecessary();
return result;
}
} catch (InvocationTargetException ex) {
// Invoked method has thrown something, unwrap
Throwable throwable = ex.getCause();
if (throwable instanceof Error) {
throw (Error) throwable;
} else if (throwable instanceof GraphQLException) {
throw (GraphQLException) throwable;
} else if (throwable instanceof Exception) {
throw (Exception) throwable;
} else {
throw msg.generalDataFetcherException(operationClass.getName() + ": " + method.getName(), throwable);
}
}
}
use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.
the class MultiDataFetcher method invokeAndTransform.
@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
try {
SmallRyeContext.setContext(context);
Multi<?> multi = operationInvoker.invoke(transformedArguments);
return (O) multi.onItem().transform((t) -> {
try {
Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
resultBuilder.data(resultFromTransform);
return (O) resultBuilder.build();
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
return (O) resultBuilder.build();
}
}).onFailure().recoverWithItem(new Function<Throwable, O>() {
public O apply(Throwable throwable) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
} else if (throwable instanceof Exception) {
DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
} else if (throwable instanceof Error) {
errorResultHelper.appendException(resultBuilder, dfe, throwable);
}
return (O) resultBuilder.build();
}
});
} finally {
SmallRyeContext.remove();
}
}
use of io.smallrye.mutiny.Multi in project smallrye-graphql by smallrye.
the class PublisherDataFetcher method invokeAndTransform.
@Override
@SuppressWarnings("unchecked")
protected <O> O invokeAndTransform(DataFetchingEnvironment dfe, DataFetcherResult.Builder<Object> resultBuilder, Object[] transformedArguments) throws Exception {
SmallRyeContext context = contextHelper.getSmallRyeContext(dfe);
try {
SmallRyeContext.setContext(context);
Publisher<?> publisher = operationInvoker.invoke(transformedArguments);
Multi<?> multi = Multi.createFrom().publisher(publisher);
return (O) multi.onItem().transform((t) -> {
try {
Object resultFromTransform = fieldHelper.transformOrAdaptResponse(t, dfe);
resultBuilder.data(resultFromTransform);
return (O) resultBuilder.build();
} catch (AbstractDataFetcherException abstractDataFetcherException) {
// Arguments or result couldn't be transformed
abstractDataFetcherException.appendDataFetcherResult(resultBuilder, dfe);
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), abstractDataFetcherException);
return (O) resultBuilder.build();
}
}).onFailure().recoverWithItem(new Function<Throwable, O>() {
@Override
public O apply(Throwable throwable) {
eventEmitter.fireOnDataFetchError(dfe.getExecutionId().toString(), throwable);
if (throwable instanceof GraphQLException) {
GraphQLException graphQLException = (GraphQLException) throwable;
errorResultHelper.appendPartialResult(resultBuilder, dfe, graphQLException);
} else if (throwable instanceof Exception) {
DataFetcherException dataFetcherException = SmallRyeGraphQLServerMessages.msg.dataFetcherException(operation, throwable);
errorResultHelper.appendException(resultBuilder, dfe, dataFetcherException);
} else if (throwable instanceof Error) {
errorResultHelper.appendException(resultBuilder, dfe, throwable);
}
return (O) resultBuilder.build();
}
});
} finally {
SmallRyeContext.remove();
}
}
use of io.smallrye.mutiny.Multi in project smallrye-mutiny-vertx-bindings by smallrye.
the class OtherWebClientTest method testPost.
@Test
public void testPost() {
int times = 5;
waitFor(times);
HttpServer server = vertx.createHttpServer(new HttpServerOptions().setPort(8080));
server.requestStream().handler(req -> req.bodyHandler(buff -> {
assertEquals("onetwothree", buff.toString());
req.response().endAndForget();
}));
try {
server.listenAndAwait();
client = WebClient.wrap(vertx.createHttpClient(new HttpClientOptions()));
Multi<Buffer> stream = Multi.createFrom().items(Buffer.buffer("one"), Buffer.buffer("two"), Buffer.buffer("three"));
Uni<HttpResponse<Buffer>> uni = client.post(8080, "localhost", "/the_uri").sendStream(stream);
for (int i = 0; i < times; i++) {
uni.subscribe().with(resp -> complete(), this::fail);
}
await();
} finally {
server.closeAndAwait();
}
}
Aggregations