Search in sources :

Example 1 with ServerStartupEvent

use of io.micronaut.runtime.server.event.ServerStartupEvent in project micronaut-core by micronaut-projects.

the class NettyHttpServer method fireStartupEvents.

private void fireStartupEvents() {
    Optional<String> applicationName = serverConfiguration.getApplicationConfiguration().getName();
    applicationContext.getEventPublisher(ServerStartupEvent.class).publishEvent(new ServerStartupEvent(this));
    applicationName.ifPresent(id -> {
        this.serviceInstance = applicationContext.createBean(NettyEmbeddedServerInstance.class, id, this);
        applicationContext.getEventPublisher(ServiceReadyEvent.class).publishEvent(new ServiceReadyEvent(serviceInstance));
    });
}
Also used : ServiceReadyEvent(io.micronaut.discovery.event.ServiceReadyEvent) AsciiString(io.netty.util.AsciiString) ServerStartupEvent(io.micronaut.runtime.server.event.ServerStartupEvent)

Example 2 with ServerStartupEvent

use of io.micronaut.runtime.server.event.ServerStartupEvent 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)

Example 3 with ServerStartupEvent

use of io.micronaut.runtime.server.event.ServerStartupEvent in project micronaut-grpc by micronaut-projects.

the class GrpcEmbeddedServer method start.

@Override
public EmbeddedServer start() {
    if (running.compareAndSet(false, true)) {
        try {
            server.start();
            eventPublisher.publishEvent(new ServerStartupEvent(this));
            getApplicationConfiguration().getName().ifPresent(id -> {
                Map<String, String> metadata = new LinkedHashMap<>();
                if (computeInstanceMetadataResolver != null) {
                    final Optional<ComputeInstanceMetadata> cim = computeInstanceMetadataResolver.resolve(applicationContext.getEnvironment());
                    cim.ifPresent(computeInstanceMetadata -> metadata.putAll(computeInstanceMetadata.getMetadata()));
                }
                this.serviceInstance = new GrpcServerInstance(this, id, getURI(), metadata, metadataContributors, grpcConfiguration);
                applicationContext.publishEvent(new ServiceReadyEvent(serviceInstance));
            });
        } catch (IOException e) {
            throw new ApplicationStartupException("Unable to start GRPC server: " + e.getMessage(), e);
        }
    }
    return this;
}
Also used : ServiceReadyEvent(io.micronaut.discovery.event.ServiceReadyEvent) IOException(java.io.IOException) ServerStartupEvent(io.micronaut.runtime.server.event.ServerStartupEvent) ApplicationStartupException(io.micronaut.runtime.exceptions.ApplicationStartupException) ComputeInstanceMetadata(io.micronaut.discovery.cloud.ComputeInstanceMetadata) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ServerStartupEvent (io.micronaut.runtime.server.event.ServerStartupEvent)3 ServiceReadyEvent (io.micronaut.discovery.event.ServiceReadyEvent)2 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 ComputeInstanceMetadata (io.micronaut.discovery.cloud.ComputeInstanceMetadata)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 ApplicationStartupException (io.micronaut.runtime.exceptions.ApplicationStartupException)1 TaskScheduler (io.micronaut.scheduling.TaskScheduler)1 AsciiString (io.netty.util.AsciiString)1