use of lombok.Data in project completable-reactor by ru-fix.
the class CompletableReactorHandlerArgumentsTest method arguments_test_5.
@Test
public void arguments_test_5() throws Exception {
@Reactored("Payload with string")
@Data
class Payload {
String data;
}
class Service {
@Reactored("method with 6 arguments")
public CompletableFuture<String> foo(String arg1, short arg2, boolean arg3, BigInteger arg4, long arg5) {
return CompletableFuture.completedFuture(arg1 + arg2 + arg3 + arg4 + arg5);
}
}
final Service service = new Service();
class Config {
ReactorGraphBuilder graphBuilder = new ReactorGraphBuilder(this);
Processor<Payload> processor = graphBuilder.processor().forPayload(Payload.class).passArg(pld -> "1").passArg(pld -> (short) 3).passArg(pld -> true).passArg(pld -> BigInteger.valueOf(6L)).passArg(pld -> 7L).withHandler(service::foo).withMerger((payload, result) -> {
payload.data = result;
return CompletableReactorTest.Status.OK;
}).buildProcessor();
ReactorGraph<Payload> graph() {
return graphBuilder.payload(Payload.class).handle(processor).mergePoint(processor).onAny().complete().coordinates().buildGraph();
}
}
val graph = new Config().graph();
reactor.registerReactorGraph(graph);
assertEquals("13true67", reactor.submit(new Payload()).getResultFuture().get(5, TimeUnit.SECONDS).getData());
}
use of lombok.Data in project completable-reactor by ru-fix.
the class CompletableReactorHandlerArgumentsTest method arguments_test_1.
@Test
public void arguments_test_1() throws Exception {
@Reactored("Payload with string")
@Data
class Payload {
String data;
}
class Service {
@Reactored("method with 6 arguments")
public CompletableFuture<String> foo(long arg) {
return CompletableFuture.completedFuture("" + arg);
}
}
final Service service = new Service();
class Config {
ReactorGraphBuilder graphBuilder = new ReactorGraphBuilder(this);
Processor<Payload> processor = graphBuilder.processor().forPayload(Payload.class).passArg(pld -> 7L).withHandler(service::foo).withMerger((payload, result) -> {
payload.data = result;
return CompletableReactorTest.Status.OK;
}).buildProcessor();
ReactorGraph<Payload> graph() {
return graphBuilder.payload(Payload.class).handle(processor).mergePoint(processor).onAny().complete().coordinates().buildGraph();
}
}
reactor.registerReactorGraph(new Config().graph());
assertEquals("7", reactor.submit(new Payload()).getResultFuture().get(5, TimeUnit.SECONDS).getData());
}
use of lombok.Data in project completable-reactor by ru-fix.
the class ReactorGraphExecutionBuilder method build.
/**
* @param reactorGraph
* @param <PayloadType>
* @return
*/
public <PayloadType> ReactorGraphExecution<PayloadType> build(ReactorGraph<PayloadType> reactorGraph) {
val crReactorGraph = (CRReactorGraph<PayloadType>) reactorGraph;
/**
* Internal representation of processing graph based on processing vertices.
*/
final Map<CRProcessingItem, ProcessingVertex> processingVertices = new HashMap<>();
final CompletableFuture<PayloadType> submitFuture = new CompletableFuture<>();
/**
* Will be completed on payload submission to processor chain
*/
final CompletableFuture<ReactorGraphExecutionBuilder.TransitionPayloadContext> startPointTransitionFuture = submitFuture.thenApplyAsync(payload -> new TransitionPayloadContext().setPayload(payload));
/**
* Will be completed with payload when terminal graph state would be reached.
*/
final CompletableFuture<PayloadType> executionResultFuture = new CompletableFuture<>();
/**
* Init Processing Vertices.
*/
crReactorGraph.getProcessingItems().forEach((proc, info) -> {
ProcessingVertex vertex = new ProcessingVertex().setProcessingItem(proc).setProcessingItemInfo(info);
if (info.getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
/**
* Detached merge point does not uses {@code {@link ProcessingVertex#getProcessorFuture()}
*/
vertex.getProcessorFuture().completeExceptionally(new IllegalStateException(String.format("Detached Merge Point %s should not use processorFuture.", vertex.getProcessingItem().getDebugName())));
}
processingVertices.put(proc, vertex);
});
/**
* Populate start point transition
*/
for (CRProcessingItem processor : crReactorGraph.getStartPoint().getProcessingItems()) {
ProcessingVertex vertex = processingVertices.get(processor);
if (vertex.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
/**
* In case of Detached merge point transition from start point is being converted
* to a {@link MergePayloadContext}
*/
vertex.getIncomingMergeFlows().add(new TransitionFuture<>(startPointTransitionFuture.thenApplyAsync(transitionPayloadContext -> new MergePayloadContext().setDeadTransition(transitionPayloadContext.isDeadTransition()).setTerminal(transitionPayloadContext.isTerminal()).setPayload(transitionPayloadContext.getPayload()).setMergeResult(null))));
} else {
vertex.getIncomingProcessorFlows().add(new TransitionFuture<>(startPointTransitionFuture));
}
}
/**
* Populate MergePoints
*/
crReactorGraph.getMergePoints().forEach(mergePoint -> {
ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
mergePoint.getTransitions().forEach(mergePointVertex.getMergePointTransition()::add);
});
/**
* Populate transitions to all MergePoints
*/
crReactorGraph.getMergePoints().forEach(mergePoint -> {
ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
mergePoint.getTransitions().forEach(mergePointVertex.getMergePointTransition()::add);
});
/**
* Populate outgoing flows
*/
for (CRReactorGraph.MergePoint mergePoint : crReactorGraph.getMergePoints()) {
/**
* MergeGroup can contain more that one MergePoint, so we have to wait all merge points to complete before
* invoke processors.
* We should not wait all merge point to complete if we are invoking other merge point within mergeGroup.
*/
ProcessingVertex mergePointVertex = processingVertices.get(mergePoint.asProcessingItem());
CompletableFuture<MergePayloadContext> mergePointFuture = mergePointVertex.getMergePointFuture();
for (CRReactorGraph.Transition transition : mergePoint.getTransitions()) {
/**
* Terminal merge point vertex handled synchronously with merging process
* Skip that kind of transition during pre-processing.
*/
if (!transition.isComplete()) {
/**
* Populate outgoing processor flows
* activates when all merge points within merge group is completed
*/
if (transition.getHandleBy() != null) {
CRProcessingItem proc = transition.getHandleBy();
processingVertices.get(proc).getIncomingProcessorFlows().add(new TransitionFuture<>(mergePointFuture.thenApplyAsync(context -> {
if (context.isTerminal()) {
return new TransitionPayloadContext().setTerminal(true);
} else if (context.isDeadTransition()) {
return new TransitionPayloadContext().setDeadTransition(true);
} else if (transition.isOnAny() || transition.getMergeStatuses().contains(context.mergeResult)) {
return new TransitionPayloadContext().setPayload(context.payload);
} else {
return new TransitionPayloadContext().setPayload(context.payload).setDeadTransition(true);
}
}).exceptionally(exc -> {
log.error("Failed to activate");
return null;
})));
}
/**
* Populate outgoing merge flows
*/
if (transition.getMerge() != null) {
CRProcessingItem proc = transition.getMerge();
ProcessingVertex transitionDestinationVertex = processingVertices.get(proc);
transitionDestinationVertex.getIncomingMergeFlows().add(new TransitionFuture<>(mergePointFuture.thenApplyAsync(context -> {
if (context.isTerminal()) {
return new MergePayloadContext().setTerminal(true);
} else if (context.isDeadTransition()) {
return new MergePayloadContext().setDeadTransition(true);
} else if (transition.isOnAny() || transition.getMergeStatuses().contains(context.mergeResult)) {
return new MergePayloadContext().setPayload(context.payload).setMergeResult(context.mergeResult);
} else {
return new MergePayloadContext().setDeadTransition(true);
}
})));
}
}
}
}
// TODO FIX!! allow detached processor to read data from payload and only after that execute merge point
// down the flow, so merge point and detached processor would not run concurrently
/**
* Join incoming processor flows to single processor invocation
*/
processingVertices.forEach((processor, processingItem) -> {
if (processingItem.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
/**
* Detached merge point does not have graph processor, only merge point.
* No processor invocation is needed
*/
if (processingItem.getIncomingProcessorFlows().size() != 0) {
throw new IllegalStateException(String.format("Invalid graph state. Detached merge point %s have more than 0 incoming flows.", processor.getDebugName()));
}
return;
}
if (processingItem.getIncomingProcessorFlows().size() <= 0) {
throw new IllegalArgumentException(String.format("Invalid graph descriptor. Processor %s does not have incoming flows." + " Probably missing handleBy directive for this processor.", processor.getDebugName()));
}
CompletableFuture.allOf(processingItem.getIncomingProcessorFlows().stream().map(TransitionFuture::getFuture).toArray(CompletableFuture[]::new)).thenRunAsync(() -> {
List<TransitionPayloadContext> incomingFlows = processingItem.getIncomingProcessorFlows().stream().map(future -> {
try {
/**
* Future should be already complete
*/
if (!future.getFuture().isDone()) {
Exception resultException = new Exception(String.format("Illegal graph execution state." + " Future is not completed. Processor: %s", processingItem.getProcessingItem().getDebugName()));
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_TRANSITION_PAYLOAD_CONTEXT;
} else {
return future.getFuture().get();
}
} catch (Exception exc) {
Exception resultException = new Exception(String.format("Failed to get incoming processor flow future result" + " for processor: %s", processingItem.getProcessingItem().getDebugName()), exc);
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_TRANSITION_PAYLOAD_CONTEXT;
}
}).collect(Collectors.toList());
if (incomingFlows.stream().anyMatch(context -> context == INVALID_TRANSITION_PAYLOAD_CONTEXT)) {
/**
* Invalid graph execution state
* Mark as terminal all outgoing flows from processor
*/
processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
} else if (incomingFlows.stream().anyMatch(TransitionPayloadContext::isTerminal)) {
/**
* Terminal state reached.
* Mark as terminal all outgoing flows from processor
*/
processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
} else {
List<TransitionPayloadContext> activeIncomingFlows = incomingFlows.stream().filter(context -> !context.isDeadTransition()).collect(Collectors.toList());
if (activeIncomingFlows.size() <= 0) {
/**
* There is no active incoming flow for given processor.
* Processor will not be invoked.
* All outgoing flows from processor will be marked as dead.
*/
processingItem.getProcessorFuture().complete(new HandlePayloadContext().setDeadTransition(true));
} else {
if (activeIncomingFlows.size() > 1) {
/**
* Illegal graph state. Too many active incoming flows.
* Mark as terminal all outgoing flows
* Complete graph with exception
*/
Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for processor %s." + " Reactor can not determinate from which of transitions" + " take payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", processingItem.getProcessingItem().getDebugName()));
executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
processingItem.getProcessorFuture().complete(new HandlePayloadContext().setTerminal(true));
} else {
handle(processingItem, activeIncomingFlows.get(0), executionResultFuture);
}
}
}
}).exceptionally(throwable -> {
log.error("Join incoming processor flows failed.", throwable);
return null;
});
});
// processingVertices
/**
* Join incoming merge flows and processor handing future with single merging invocation
*/
processingVertices.forEach((processor, vertex) -> {
List<CompletableFuture> incomingFlows = new ArrayList<>();
vertex.getIncomingMergeFlows().stream().map(TransitionFuture::getFuture).forEach(incomingFlows::add);
if (vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT) {
/**
* Ignore processor future for detached merge point
* And use it for all other cases
*/
incomingFlows.add(vertex.getProcessorFuture());
}
CompletableFuture.allOf(incomingFlows.toArray(new CompletableFuture[incomingFlows.size()])).thenRunAsync(() -> {
/**
* Processor result, could be INVALID_HANDLE_PAYLOAD_CONTEXT in case of exception
* Could be NULL in case of detached merge point
*/
HandlePayloadContext handlePayloadContext = null;
if (vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT) {
handlePayloadContext = Optional.of(vertex.getProcessorFuture()).map(future -> {
try {
if (!future.isDone()) {
RuntimeException resultException = new RuntimeException(String.format("Illegal graph execution state. Processor future" + " is not completed. Processor %s", vertex.getProcessingItem().getDebugName()));
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_HANDLE_PAYLOAD_CONTEXT;
} else {
return future.get();
}
} catch (Exception exc) {
RuntimeException resultException = new RuntimeException(String.format("Failed to get processor future result for processor: %s", vertex.getProcessingItem().getDebugName()), exc);
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_HANDLE_PAYLOAD_CONTEXT;
}
}).orElse(INVALID_HANDLE_PAYLOAD_CONTEXT);
if (handlePayloadContext == INVALID_HANDLE_PAYLOAD_CONTEXT) {
/**
* Failed to get processor result.
* Merging will not be applied to payload.
* All outgoing flows from merge point will be marked as terminal.
* executionResult completed by exception
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
return;
} else if (handlePayloadContext.isTerminal()) {
/**
* Processor was marked as terminal during flow by terminal transition.
* Merging will not be applied to payload.
* All outgoing flows from merge point will be marked as terminal.
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
return;
} else if (handlePayloadContext.isDeadTransition()) {
/**
* Processor was disabled during flow by dead transition.
* Merging will not be applied to payload.
* All outgoing flows from merge point will be marked as dead.
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
return;
}
}
/**
* Incoming merge flows, could be empty for processors Merge Point
*/
List<MergePayloadContext> incomingMergeFlows = vertex.getIncomingMergeFlows().stream().map(future -> {
try {
if (!future.getFuture().isDone()) {
RuntimeException resultException = new RuntimeException(String.format("Illegal graph execution state. Incoming merge future" + " is not complete." + " ProcessingVertex: %s", vertex));
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_MERGE_PAYLOAD_CONTEXT;
} else {
return future.getFuture().get();
}
} catch (Exception exc) {
RuntimeException resultException = new RuntimeException(String.format("Failed to get incoming merge flow future result for processor: %s", vertex.getProcessingItem().getDebugName()), exc);
log.error(resultException.getMessage(), resultException);
executionResultFuture.completeExceptionally(resultException);
return INVALID_MERGE_PAYLOAD_CONTEXT;
}
}).collect(Collectors.toList());
if (incomingMergeFlows.stream().anyMatch(context -> context == INVALID_MERGE_PAYLOAD_CONTEXT)) {
/**
* Exception during merging
* Mark as terminal all outgoing flows from merge point
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
} else if (incomingMergeFlows.stream().anyMatch(MergePayloadContext::isTerminal)) {
/**
* Terminal state reached.
* Mark as terminal all outgoing flows from merge point
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
} else {
final List<MergePayloadContext> activeIncomingMergeFlows = incomingMergeFlows.stream().filter(context -> !context.isDeadTransition()).collect(Collectors.toList());
if (vertex.getProcessingItemInfo().getProcessingItemType() == CRReactorGraph.ProcessingItemType.MERGE_POINT) {
/**
* Detached merge point
*/
if (activeIncomingMergeFlows.size() == 0) {
/**
* Check that there are at least one incoming transition that marked as dead
*/
if (incomingMergeFlows.stream().anyMatch(MergePayloadContext::isDeadTransition)) {
/**
* Detached MergePoint marked as Dead, because there are no active incoming
* flows and there is at least one incoming dead transition
* Mark as dead all outgoing flows from merge point
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
} else {
throw new IllegalStateException(String.format("There is no incoming merge flows for detached merge point %s." + " At least dead incoming transition expected.", vertex.getProcessingItem().getDebugName()));
}
} else {
if (activeIncomingMergeFlows.size() > 1) {
/**
* Illegal graph state. Too many active incoming flows.
* Mark as terminal all outgoing flows from merge point
* Complete graph with exception
*/
Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for routing point %s." + " Reactor can not determinate from which of transitions take" + " payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", vertex.getProcessingItem().getDebugName()));
executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
} else {
/**
* Single active incoming merge flow
*/
merge(vertex, Optional.empty(), activeIncomingMergeFlows.get(0).getPayload(), executionResultFuture);
}
}
} else {
/**
* Processors MergePoint
*/
if (incomingMergeFlows.size() == 0) {
/**
* No incoming merge flows, only one flow from processors handle
*/
merge(vertex, handlePayloadContext.getProcessorResult(), handlePayloadContext.getPayload(), executionResultFuture);
} else {
/**
* Incoming merge flows exists. But some of them can be marked as dead.
*/
if (activeIncomingMergeFlows.size() == 0) {
/**
* There is no active incoming merge flow for given merge point.
* Mark merge point as dead.
*/
vertex.getMergePointFuture().complete(new MergePayloadContext().setDeadTransition(true));
} else {
if (activeIncomingMergeFlows.size() > 1) {
/**
* Illegal graph state. Too many active incoming flows.
* Mark as terminal all outgoing flows from merge point
* Complete graph with exception
*/
Exception tooManyActiveIncomingFlowsExc = new Exception(String.format("There is more than one active incoming flow for merge point for" + " processor %s." + " Reactor can not determinate from which of transitions" + " take payload." + " Possible loss of computation results." + " Possible concurrent modifications of payload.", vertex.getProcessingItem().getDebugName()));
executionResultFuture.completeExceptionally(tooManyActiveIncomingFlowsExc);
vertex.getMergePointFuture().complete(new MergePayloadContext().setTerminal(true));
} else {
merge(vertex, handlePayloadContext.getProcessorResult(), activeIncomingMergeFlows.get(0).getPayload(), executionResultFuture);
}
}
}
}
}
}).exceptionally(throwable -> {
log.error("Joining incoming merge flows failed.", throwable);
return null;
});
});
// processingVertices
/**
* Handle terminal vertices.
* When execution reaches 'complete' vertex all transitions should be marked dead and complete.
*/
executionResultFuture.thenRunAsync(() -> {
processingVertices.entrySet().stream().map(Map.Entry::getValue).map(ProcessingVertex::getIncomingProcessorFlows).flatMap(Collection::stream).map(TransitionFuture::getFuture).forEach(future -> future.complete(new TransitionPayloadContext().setDeadTransition(true)));
processingVertices.entrySet().stream().map(Map.Entry::getValue).map(ProcessingVertex::getIncomingProcessorFlows).flatMap(Collection::stream).map(TransitionFuture::getFuture).forEach(future -> future.complete(new TransitionPayloadContext().setDeadTransition(true)));
}).exceptionally(throwable -> {
log.error("Marking transitions as dead is failed.", throwable);
return null;
});
/**
* Collect chain execution processor handling futures
* Processors futures holds handle invocation result or dead context.
* Exception is an detached merge point which processorFuture is not used
* Then all processors futures completes chainExecutionFuture completes too.
*/
CompletableFuture<Void> chainExecutionFuture = CompletableFuture.allOf(processingVertices.entrySet().stream().map(Map.Entry::getValue).filter(vertex -> vertex.getProcessingItemInfo().getProcessingItemType() != CRReactorGraph.ProcessingItemType.MERGE_POINT).map(ProcessingVertex::getProcessorFuture).toArray(CompletableFuture[]::new));
return new ReactorGraphExecution<PayloadType>(submitFuture, executionResultFuture, chainExecutionFuture, debugProcessingVertexGraphState ? processingVertices.values() : null);
}
use of lombok.Data in project tomee by apache.
the class GenerateBoms method asDistribution.
/**
* Extract the zip that represents a TomEE distribution. Find
* all jar files. Match them up with jars in the local Maven
* repository. Identify any jars we could not match. And finally,
* return all the data so we have a canonical representation of
* the distribution.
*/
private Distribution asDistribution(final File zip) {
final File tmpdir = Files.tmpdir();
try {
Zips.unzip(zip, tmpdir);
} catch (IOException e) {
throw new UncheckedIOException("Cannot unzip " + zip.getAbsolutePath(), e);
}
final File serverDir = getServerDir(tmpdir);
final Distribution distribution = new Distribution(zip, serverDir);
final List<File> jars = Files.collect(serverDir, ".*.jar");
final Function<File, Artifact> from = file1 -> {
try {
return repository.from(file1);
} catch (IllegalStateException e) {
distribution.missing.add(file1);
return null;
}
};
jars.stream().filter(jar -> !jar.getName().equals("bootstrap.jar")).filter(jar -> !jar.getName().equals("catalina-ant.jar")).filter(jar -> !jar.getName().startsWith("tomcat-i18n")).map(from).filter(Objects::nonNull).sorted().distinct().forEach(distribution.artifacts::add);
return distribution;
}
use of lombok.Data in project metacat by Netflix.
the class MysqlUserMetadataService method searchDefinitionMetadata.
@Override
@Transactional(readOnly = true)
public List<DefinitionMetadataDto> searchDefinitionMetadata(@Nullable final Set<String> propertyNames, @Nullable final String type, @Nullable final String name, @Nullable final HasMetadata holder, @Nullable final String sortBy, @Nullable final String sortOrder, @Nullable final Integer offset, @Nullable final Integer limit) {
final List<DefinitionMetadataDto> result = Lists.newArrayList();
final SearchMetadataQuery queryObj = new SearchMetadataQuery(SQL.SEARCH_DEFINITION_METADATAS).buildSearchMetadataQuery(propertyNames, type, name, sortBy, sortOrder, offset, limit);
try {
// Handler for reading the result set
final ResultSetExtractor<Void> handler = rs -> {
while (rs.next()) {
final String definitionName = rs.getString("name");
final String data = rs.getString("data");
final DefinitionMetadataDto definitionMetadataDto = new DefinitionMetadataDto();
definitionMetadataDto.setName(QualifiedName.fromString(definitionName));
definitionMetadataDto.setDefinitionMetadata(metacatJson.parseJsonObject(data));
result.add(definitionMetadataDto);
}
return null;
};
jdbcTemplate.query(queryObj.getSearchQuery().toString(), queryObj.getSearchParamList().toArray(), handler);
} catch (Exception e) {
log.error("Failed to search definition data", e);
throw new UserMetadataServiceException("Failed to search definition data", e);
}
return result;
}
Aggregations