use of ratpack.func.Function in project ratpack by ratpack.
the class DefaultExecHarness method yield.
@Override
public <T> ExecResult<T> yield(Action<? super RegistrySpec> registry, final Function<? super Execution, ? extends Promise<T>> func) throws Exception {
AtomicReference<ExecResult<T>> reference = new AtomicReference<>();
CountDownLatch latch = new CountDownLatch(1);
controller.fork().register(registry).onError(throwable -> reference.set(new ResultBackedExecResult<>(Result.<T>error(throwable)))).onComplete(exec -> latch.countDown()).start(execution -> {
reference.set(ExecResult.complete());
Promise<T> promise = func.apply(execution);
if (promise == null) {
reference.set(null);
} else {
promise.then(t -> reference.set(new ResultBackedExecResult<>(Result.success(t))));
}
});
latch.await();
return reference.get();
}
use of ratpack.func.Function in project ratpack by ratpack.
the class AbstractPropertiesConfigSource method loadConfigData.
@Override
public ObjectNode loadConfigData(ObjectMapper objectMapper, FileSystemBinding fileSystemBinding) throws Exception {
ObjectNode rootNode = objectMapper.createObjectNode();
Properties properties = loadProperties();
Stream<Pair<String, String>> pairs = properties.stringPropertyNames().stream().map(key -> Pair.of(key, properties.getProperty(key)));
if (prefix.isPresent()) {
pairs = pairs.filter(p -> p.left.startsWith(prefix.get())).map(((Function<Pair<String, String>, Pair<String, String>>) p -> p.mapLeft(s -> s.substring(prefix.get().length()))).toFunction());
}
pairs.forEach(p -> populate(rootNode, p.left, p.right));
return rootNode;
}
use of ratpack.func.Function in project ratpack by ratpack.
the class Guice method buildInjector.
static Injector buildInjector(Registry baseRegistry, Action<? super BindingsSpec> bindingsAction, Function<? super Module, ? extends Injector> injectorFactory) throws Exception {
List<Action<? super Binder>> binderActions = Lists.newLinkedList();
List<Module> modules = Lists.newLinkedList();
ServerConfig serverConfig = baseRegistry.get(ServerConfig.class);
BindingsSpec bindings = new DefaultBindingsSpec(serverConfig, binderActions, modules);
modules.add(new RatpackBaseRegistryModule(baseRegistry));
modules.add(new ConfigModule(serverConfig.getRequiredConfig()));
try {
bindingsAction.execute(bindings);
} catch (Exception e) {
throw uncheck(e);
}
modules.add(new AdHocModule(binderActions));
Optional<BindingsImposition> bindingsImposition = Impositions.current().get(BindingsImposition.class);
if (bindingsImposition.isPresent()) {
BindingsImposition imposition = bindingsImposition.get();
List<Action<? super Binder>> imposedBinderActions = Lists.newLinkedList();
List<Module> imposedModules = Lists.newLinkedList();
BindingsSpec imposedBindings = new DefaultBindingsSpec(serverConfig, imposedBinderActions, imposedModules);
imposition.getBindings().execute(imposedBindings);
imposedModules.add(new AdHocModule(imposedBinderActions));
Module imposedModule = imposedModules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
modules.add(imposedModule);
}
Module masterModule = modules.stream().reduce((acc, next) -> Modules.override(acc).with(next)).get();
return injectorFactory.apply(masterModule);
}
use of ratpack.func.Function in project resilience4j by resilience4j.
the class BulkheadChain method execute.
@Override
public void execute(Chain chain) throws Exception {
String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getBulkheads().getPath();
chain.prefix(prefix, chain1 -> {
chain1.get("events", ctx -> Promise.<BulkheadEventsEndpointResponse>async(d -> {
BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(BulkheadEvent::getCreationTime)).map(BulkheadEventDTO::createEventDTO).toJavaList());
d.success(response);
}).then(r -> ctx.render(Jackson.json(r))));
chain1.get("stream/events", ctx -> {
Seq<Flowable<BulkheadEvent>> eventStreams = bulkheadRegistry.getAllBulkheads().map(bulkhead -> RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()));
Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name", ctx -> {
String bulkheadName = ctx.getPathTokens().get("name");
Promise.<BulkheadEventsEndpointResponse>async(d -> {
BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(bulkheadName).getBufferedEvents().map(BulkheadEventDTO::createEventDTO).toJavaList());
d.success(response);
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name", ctx -> {
String bulkheadName = ctx.getPathTokens().get("name");
Bulkhead bulkhead = bulkheadRegistry.getAllBulkheads().find(b -> b.getName().equals(bulkheadName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("bulkhead with name %s not found", bulkheadName)));
Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
ServerSentEvents events = ServerSentEvents.serverSentEvents(RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()), e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name/:type", ctx -> {
String bulkheadName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Promise.<BulkheadEventsEndpointResponse>async(d -> {
BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(bulkheadName).getBufferedEvents().filter(event -> event.getEventType() == BulkheadEvent.Type.valueOf(eventType.toUpperCase())).map(BulkheadEventDTO::createEventDTO).toJavaList());
d.success(response);
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name/:type", ctx -> {
String bulkheadName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Bulkhead bulkhead = bulkheadRegistry.getAllBulkheads().find(b -> b.getName().equals(bulkheadName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("bulkhead with name %s not found", bulkheadName)));
Flowable<BulkheadEvent> eventStream = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()).filter(event -> event.getEventType() == BulkheadEvent.Type.valueOf(eventType.toUpperCase()));
Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
});
}
use of ratpack.func.Function in project resilience4j by resilience4j.
the class RetryChain method execute.
@Override
public void execute(Chain chain) throws Exception {
String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getRetries().getPath();
chain.prefix(prefix, chain1 -> {
chain1.get("events", ctx -> Promise.<RetryEventsEndpointResponse>async(d -> {
List<RetryEventDTO> eventsList = eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(RetryEvent::getCreationTime)).map(RetryEventDTO::createRetryEventDTO).toJavaList();
d.success(new RetryEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r))));
chain1.get("stream/events", ctx -> {
Seq<Flowable<RetryEvent>> eventStreams = retryRegistry.getAllRetries().map(retry -> toFlowable(retry.getEventPublisher()));
Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name", ctx -> {
String retryName = ctx.getPathTokens().get("name");
Promise.<RetryEventsEndpointResponse>async(d -> {
List<RetryEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(retryName).getBufferedEvents().sorted(Comparator.comparing(RetryEvent::getCreationTime)).map(RetryEventDTO::createRetryEventDTO).toJavaList();
d.success(new RetryEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name", ctx -> {
String rateLimiterName = ctx.getPathTokens().get("name");
Retry retry = retryRegistry.getAllRetries().find(rL -> rL.getName().equals(rateLimiterName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", rateLimiterName)));
Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(toFlowable(retry.getEventPublisher()), e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
chain1.get("events/:name/:type", ctx -> {
String retryName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Promise.<RetryEventsEndpointResponse>async(d -> {
List<RetryEventDTO> eventsList = eventConsumerRegistry.getEventConsumer(retryName).getBufferedEvents().sorted(Comparator.comparing(RetryEvent::getCreationTime)).filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase())).map(RetryEventDTO::createRetryEventDTO).toJavaList();
d.success(new RetryEventsEndpointResponse(eventsList));
}).then(r -> ctx.render(Jackson.json(r)));
});
chain1.get("stream/events/:name/:type", ctx -> {
String retryName = ctx.getPathTokens().get("name");
String eventType = ctx.getPathTokens().get("type");
Retry retry = retryRegistry.getAllRetries().find(rL -> rL.getName().equals(retryName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("rate limiter with name %s not found", retryName)));
Flowable<RetryEvent> eventStream = toFlowable(retry.getEventPublisher()).filter(event -> event.getEventType() == RetryEvent.Type.valueOf(eventType.toUpperCase()));
Function<RetryEvent, String> data = r -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(RetryEventDTO.createRetryEventDTO(r));
ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(RetryEvent::getName).event(c -> c.getEventType().name()).data(data));
ctx.render(events);
});
});
}
Aggregations