ratpack.func.Function Java Examples

The following examples show how to use ratpack.func.Function. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: LogServiceApplication.java    From Hands-On-Reactive-Programming-in-Spring-5 with MIT License 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    ConfigurableApplicationContext context =
            SpringApplication.run(LogServiceApplication.class, args);

    LogService logsService = context.getBean(LogService.class);
    FileService fileService = context.getBean(FileService.class);

    File tempFile = File.createTempFile("LogServiceApplication", ".tmp");
    tempFile.deleteOnExit();

    fileService.writeTo(tempFile.getAbsolutePath(), logsService.stream());

    RatpackServer.start(server ->
            server.handlers(chain ->
                    chain.all(ctx -> {

                        Publisher<String> logs = logsService.stream();

                        ServerSentEvents events = serverSentEvents(
                                logs,
                                event -> event.id(Objects::toString)
                                              .event("log")
                                              .data(Function.identity())
                        );

                        ctx.render(events);
                    })
            )
    );
}
 
Example #2
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig timeLimiters(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        timelimiter = configure.apply(new EndpointConfig("timelimiter"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #3
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig threadPoolBulkheads(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        threadpoolbulkhead = configure.apply(new EndpointConfig("threadpoolbulkhead"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #4
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig bulkheads(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        bulkhead = configure.apply(new EndpointConfig("bulkhead"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #5
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig retries(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        retry = configure.apply(new EndpointConfig("retry"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #6
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig rateLimiters(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        ratelimiter = configure.apply(new EndpointConfig("ratelimiter"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #7
Source File: EndpointsConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public EndpointsConfig circuitBreakers(
    Function<? super EndpointConfig, ? extends EndpointConfig> configure) {
    try {
        circuitbreaker = configure.apply(new EndpointConfig("circuitbreaker"));
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #8
Source File: BulkheadChain.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
private ServerSentEvents serverSentEvents(Chain chain, Seq<Flux<BulkheadEvent>> eventStreams) {
    Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain.getRegistry())
        .writeValueAsString(BulkheadEventDTOFactory.createBulkheadEventDTO(b));
    return ServerSentEvents.serverSentEvents(Flux.merge(eventStreams),
        e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name())
            .data(data));
}
 
Example #9
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig endpoints(
    Function<? super EndpointsConfig, ? extends EndpointsConfig> configure) {
    try {
        endpoints = configure.apply(new EndpointsConfig());
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #10
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig timeLimiter(String name,
                                      Function<? super TimeLimiterConfigurationProperties.InstanceProperties, ? extends TimeLimiterConfigurationProperties.InstanceProperties> configure) {
    try {
        TimeLimiterConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new TimeLimiterConfigurationProperties.InstanceProperties());
        timeLimiter.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #11
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig threadPoolBulkhead(String name,
    Function<? super ThreadPoolBulkheadConfigurationProperties.InstanceProperties, ? extends ThreadPoolBulkheadConfigurationProperties.InstanceProperties> configure) {
    try {
        ThreadPoolBulkheadConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new ThreadPoolBulkheadConfigurationProperties.InstanceProperties());
        threadpoolbulkhead.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #12
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig bulkhead(String name,
    Function<? super BulkheadConfigurationProperties.InstanceProperties, ? extends BulkheadConfigurationProperties.InstanceProperties> configure) {
    try {
        BulkheadConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new BulkheadConfigurationProperties.InstanceProperties());
        bulkhead.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #13
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig retry(String name,
    Function<? super RetryConfigurationProperties.InstanceProperties, ? extends RetryConfigurationProperties.InstanceProperties> configure) {
    try {
        RetryConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new RetryConfigurationProperties.InstanceProperties());
        retry.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #14
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig rateLimiter(String name,
    Function<? super RateLimiterConfigurationProperties.InstanceProperties, ? extends RateLimiterConfigurationProperties.InstanceProperties> configure) {
    try {
        RateLimiterConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new RateLimiterConfigurationProperties.InstanceProperties());
        ratelimiter.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #15
Source File: Resilience4jConfig.java    From resilience4j with Apache License 2.0 5 votes vote down vote up
public Resilience4jConfig circuitBreaker(String name,
    Function<? super CircuitBreakerConfigurationProperties.InstanceProperties, ? extends CircuitBreakerConfigurationProperties.InstanceProperties> configure) {
    try {
        CircuitBreakerConfigurationProperties.InstanceProperties finalConfig = configure
            .apply(new CircuitBreakerConfigurationProperties.InstanceProperties());
        circuitbreaker.getInstances().put(name, finalConfig);
        return this;
    } catch (Exception e) {
        throw uncheck(e);
    }
}
 
Example #16
Source File: ZipkinHttpClientImpl.java    From ratpack-zipkin with Apache License 2.0 5 votes vote down vote up
@Override
public RequestSpec onRedirect(Function<? super ReceivedResponse, Action<? super RequestSpec>> function) {

    Function<? super ReceivedResponse, Action<? super RequestSpec>> wrapped =
        (ReceivedResponse response) -> redirectHandler(response).append(function.apply(response));

    this.delegate.onRedirect(wrapped);
    return this;
}
 
Example #17
Source File: RatpackITest.java    From java-specialagent with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception {
  final RatpackServer server = RatpackServer.start(new Action<RatpackServerSpec>() {
    @Override
    public void execute(final RatpackServerSpec ratpackServerSpec) {
      ratpackServerSpec.handlers(new Action<Chain>() {
        @Override
        public void execute(final Chain chain) {
          chain.get(new Handler() {
            @Override
            public void handle(final Context context) {
              TestUtil.checkActiveSpan();
              context.render("Test");
            }
          });
        }
      });
    }
  });

  final HttpClient client = HttpClient.of(new Action<HttpClientSpec>() {
    @Override
    public void execute(final HttpClientSpec httpClientSpec) {
      httpClientSpec
        .poolSize(10)
        .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH)
        .readTimeout(Duration.of(60, ChronoUnit.SECONDS))
        .byteBufAllocator(PooledByteBufAllocator.DEFAULT);
    }
  });

  try (final ExecHarness harness = ExecHarness.harness()) {
    final ExecResult<ReceivedResponse> result = harness.yield(new Function<Execution,Promise<ReceivedResponse>>() {
      @Override
      public Promise<ReceivedResponse> apply(final Execution execution) {
        return client.get(URI.create("http://localhost:5050"));
      }
    });

    final int statusCode = result.getValue().getStatusCode();
    if (200 != statusCode)
      throw new AssertionError("ERROR: response: " + statusCode);
  }

  server.stop();
  TestUtil.checkSpan(new ComponentSpanCount("netty", 2, true));
}
 
Example #18
Source File: Application.java    From tutorials with MIT License 4 votes vote down vote up
public static void main(String[] args) throws Exception {

        final Action<HikariConfig> hikariConfigAction = hikariConfig -> {
            hikariConfig.setDataSourceClassName("org.h2.jdbcx.JdbcDataSource");
            hikariConfig.addDataSourceProperty("URL", "jdbc:h2:mem:baeldung;INIT=RUNSCRIPT FROM 'classpath:/DDL.sql'");
        };

        final Action<BindingsSpec> bindingsSpecAction = bindings -> bindings.module(HikariModule.class, hikariConfigAction);
        final HttpClient httpClient = HttpClient.of(httpClientSpec -> {
            httpClientSpec.poolSize(10)
                .connectTimeout(Duration.of(60, ChronoUnit.SECONDS))
                .maxContentLength(ServerConfig.DEFAULT_MAX_CONTENT_LENGTH)
                .responseMaxChunkSize(16384)
                .readTimeout(Duration.of(60, ChronoUnit.SECONDS))
                .byteBufAllocator(PooledByteBufAllocator.DEFAULT);
        });
        final Function<Registry, Registry> registryFunction = Guice.registry(bindingsSpecAction);

        final Action<Chain> chainAction = chain -> chain.all(new RequestValidatorFilter())
            .get(ctx -> ctx.render("Welcome to baeldung ratpack!!!"))
            .get("data/employees", ctx -> ctx.render(Jackson.json(createEmpList())))
            .get(":name", ctx -> ctx.render("Hello " + ctx.getPathTokens()
                .get("name") + "!!!"))
            .post(":amount", ctx -> ctx.render(" Amount $" + ctx.getPathTokens()
                .get("amount") + " added successfully !!!"));

        final Action<Chain> routerChainAction = routerChain -> {
            routerChain.path("redirect", new RedirectHandler())
                .prefix("employee", empChain -> {
                    empChain.get(":id", new EmployeeHandler());
                });
        };
        final Action<RatpackServerSpec> ratpackServerSpecAction = serverSpec -> serverSpec.registry(registryFunction)
            .registryOf(registrySpec -> {
                registrySpec.add(EmployeeRepository.class, new EmployeeRepositoryImpl());
                registrySpec.add(HttpClient.class, httpClient);
            })
            .handlers(chain -> chain.insert(routerChainAction)
                .insert(chainAction));

        RatpackServer.start(ratpackServerSpecAction);
    }
 
Example #19
Source File: CircuitBreakerTransformer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set a recovery function that will execute when the circuit breaker is open.
 *
 * @param recoverer the recovery function
 * @return the transformer
 */
public CircuitBreakerTransformer<T> recover(Function<Throwable, ? extends T> recoverer) {
    this.recoverer = recoverer;
    return this;
}
 
Example #20
Source File: BulkheadTransformer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set a recovery function that will execute when the rateLimiter limit is exceeded.
 *
 * @param recoverer the recovery function
 * @return the transformer
 */
public BulkheadTransformer<T> recover(Function<Throwable, ? extends T> recoverer) {
    this.recoverer = recoverer;
    return this;
}
 
Example #21
Source File: TimeLimiterTransformer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set a recovery function that will execute when the timeLimiter timeout is exceeded.
 *
 * @param recoverer the recovery function
 * @return the transformer
 */
public TimeLimiterTransformer<T> recover(Function<Throwable, ? extends T> recoverer) {
    this.recoverer = recoverer;
    return this;
}
 
Example #22
Source File: RetryTransformer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set a recovery function that will execute when the retry limit is exceeded.
 *
 * @param recoverer the recovery function
 * @return the transformer
 */
public RetryTransformer<T> recover(@Nullable Function<Throwable, ? extends T> recoverer) {
    this.recoverer = recoverer;
    return this;
}
 
Example #23
Source File: RateLimiterTransformer.java    From resilience4j with Apache License 2.0 2 votes vote down vote up
/**
 * Set a recovery function that will execute when the rateLimiter limit is exceeded.
 *
 * @param recoverer the recovery function
 * @return the transformer
 */
public RateLimiterTransformer<T> recover(Function<Throwable, ? extends T> recoverer) {
    this.recoverer = recoverer;
    return this;
}