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));
});
}
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");
}
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;
}
Aggregations