Search in sources :

Example 1 with ApplicationEventListener

use of io.micronaut.context.event.ApplicationEventListener in project micronaut-core by micronaut-projects.

the class ServiceHttpClientFactory method healthCheckStarter.

/**
 * Creates a {@link ApplicationEventListener} that listens to {@link ServerStartupEvent} for each configured HTTP client
 * in order to register a health check if necessary.
 *
 * @param configuration The configuration
 * @param instanceList  The instance list
 * @return The event listener
 */
@EachBean(ServiceHttpClientConfiguration.class)
@Requires(condition = ServiceHttpClientCondition.class)
ApplicationEventListener<ServerStartupEvent> healthCheckStarter(@Parameter ServiceHttpClientConfiguration configuration, @Parameter StaticServiceInstanceList instanceList) {
    if (configuration.isHealthCheck()) {
        return event -> {
            final List<URI> originalURLs = configuration.getUrls();
            Collection<URI> loadBalancedURIs = instanceList.getLoadBalancedURIs();
            final HttpClient httpClient = clientFactory.get().getClient(configuration.getHttpVersion(), configuration.getServiceId(), configuration.getPath().orElse(null));
            final Duration initialDelay = configuration.getHealthCheckInterval();
            Duration delay = configuration.getHealthCheckInterval();
            taskScheduler.scheduleWithFixedDelay(initialDelay, delay, () -> Flux.fromIterable(originalURLs).flatMap(originalURI -> {
                URI healthCheckURI = originalURI.resolve(configuration.getHealthCheckUri());
                return Flux.from(httpClient.exchange(HttpRequest.GET(healthCheckURI))).onErrorResume(throwable -> {
                    if (throwable instanceof HttpClientResponseException) {
                        HttpClientResponseException responseException = (HttpClientResponseException) throwable;
                        return Flux.just((HttpResponse<ByteBuffer>) responseException.getResponse());
                    }
                    return Flux.just(HttpResponse.serverError());
                }).map(response -> Collections.singletonMap(originalURI, response.getStatus()));
            }).subscribe(uriToStatusMap -> {
                Map.Entry<URI, HttpStatus> entry = uriToStatusMap.entrySet().iterator().next();
                URI uri = entry.getKey();
                HttpStatus status = entry.getValue();
                if (status.getCode() >= 300) {
                    loadBalancedURIs.remove(uri);
                } else if (!loadBalancedURIs.contains(uri)) {
                    loadBalancedURIs.add(uri);
                }
            }));
        };
    }
    throw new DisabledBeanException("HTTP Client Health Check not enabled");
}
Also used : ServerStartupEvent(io.micronaut.runtime.server.event.ServerStartupEvent) Parameter(io.micronaut.context.annotation.Parameter) Collection(java.util.Collection) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) StaticServiceInstanceList(io.micronaut.discovery.StaticServiceInstanceList) Internal(io.micronaut.core.annotation.Internal) Flux(reactor.core.publisher.Flux) List(java.util.List) EachBean(io.micronaut.context.annotation.EachBean) Factory(io.micronaut.context.annotation.Factory) HttpStatus(io.micronaut.http.HttpStatus) BeanProvider(io.micronaut.context.BeanProvider) Duration(java.time.Duration) Map(java.util.Map) Requires(io.micronaut.context.annotation.Requires) ByteBuffer(io.micronaut.core.io.buffer.ByteBuffer) HttpResponse(io.micronaut.http.HttpResponse) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) TaskScheduler(io.micronaut.scheduling.TaskScheduler) HttpRequest(io.micronaut.http.HttpRequest) URI(java.net.URI) ApplicationEventListener(io.micronaut.context.event.ApplicationEventListener) Collections(java.util.Collections) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) HttpClientResponseException(io.micronaut.http.client.exceptions.HttpClientResponseException) HttpStatus(io.micronaut.http.HttpStatus) Collection(java.util.Collection) StaticServiceInstanceList(io.micronaut.discovery.StaticServiceInstanceList) List(java.util.List) Duration(java.time.Duration) URI(java.net.URI) ByteBuffer(io.micronaut.core.io.buffer.ByteBuffer) Map(java.util.Map) DisabledBeanException(io.micronaut.context.exceptions.DisabledBeanException) Requires(io.micronaut.context.annotation.Requires) EachBean(io.micronaut.context.annotation.EachBean)

Aggregations

BeanProvider (io.micronaut.context.BeanProvider)1 EachBean (io.micronaut.context.annotation.EachBean)1 Factory (io.micronaut.context.annotation.Factory)1 Parameter (io.micronaut.context.annotation.Parameter)1 Requires (io.micronaut.context.annotation.Requires)1 ApplicationEventListener (io.micronaut.context.event.ApplicationEventListener)1 DisabledBeanException (io.micronaut.context.exceptions.DisabledBeanException)1 Internal (io.micronaut.core.annotation.Internal)1 ByteBuffer (io.micronaut.core.io.buffer.ByteBuffer)1 StaticServiceInstanceList (io.micronaut.discovery.StaticServiceInstanceList)1 HttpRequest (io.micronaut.http.HttpRequest)1 HttpResponse (io.micronaut.http.HttpResponse)1 HttpStatus (io.micronaut.http.HttpStatus)1 HttpClientResponseException (io.micronaut.http.client.exceptions.HttpClientResponseException)1 ServerStartupEvent (io.micronaut.runtime.server.event.ServerStartupEvent)1 TaskScheduler (io.micronaut.scheduling.TaskScheduler)1 URI (java.net.URI)1 Duration (java.time.Duration)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1