org.springframework.boot.actuate.health.CompositeHealthContributor Java Examples

The following examples show how to use org.springframework.boot.actuate.health.CompositeHealthContributor. 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 Project: spring-cloud-stream-binder-rabbit   Author: spring-cloud   File: RabbitBinderModuleTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testParentConnectionFactoryInheritedIfOverridden() {
	context = new SpringApplicationBuilder(SimpleProcessor.class,
			ConnectionFactoryConfiguration.class).web(WebApplicationType.NONE)
					.run("--server.port=0");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isSameAs(MOCK_CONNECTION_FACTORY);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);
	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthIndicator).isNotNull();
	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("rabbit");
	assertThat(indicator).isNotNull();
	// mock connection factory behaves as if down
	assertThat(indicator.health().getStatus())
			.isEqualTo(Status.DOWN);
}
 
Example #2
Source Project: hedera-mirror-node   Author: hashgraph   File: GrpcConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
CompositeHealthContributor grpcServices(GrpcServiceDiscoverer grpcServiceDiscoverer,
                                        HealthStatusManager healthStatusManager) {

    Map<String, HealthIndicator> healthIndicators = new LinkedHashMap<>();

    for (GrpcServiceDefinition grpcService : grpcServiceDiscoverer.findGrpcServices()) {
        String serviceName = grpcService.getDefinition().getServiceDescriptor().getName();
        healthIndicators.put(serviceName, new GrpcHealthIndicator(healthStatusManager, serviceName));
    }

    return CompositeHealthContributor.fromMap(healthIndicators);
}
 
Example #3
private static void checkHealth(ConfigurableApplicationContext context,
		Status expected) throws InterruptedException {
	CompositeHealthContributor healthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	KafkaStreamsBinderHealthIndicator kafkaStreamsBinderHealthIndicator = (KafkaStreamsBinderHealthIndicator) healthIndicator.getContributor("kstream");
	Health health = kafkaStreamsBinderHealthIndicator.health();
	while (waitFor(health.getStatus(), health.getDetails())) {
		TimeUnit.SECONDS.sleep(2);
		health = kafkaStreamsBinderHealthIndicator.health();
	}
	assertThat(health.getStatus()).isEqualTo(expected);
}
 
Example #4
Source Project: spring-cloud-stream   Author: spring-cloud   File: BindersHealthIndicatorAutoConfiguration.java    License: Apache License 2.0 5 votes vote down vote up
private HealthContributor getContributor(Map<String, HealthContributor> binderHealthContributors) {
	if (binderHealthContributors.isEmpty()) {
		return UNKNOWN;
	}
	if (binderHealthContributors.size() == 1) {
		return binderHealthContributors.values().iterator().next();
	}
	return CompositeHealthContributor.fromMap(binderHealthContributors);
}
 
Example #5
Source Project: spring-cloud-stream   Author: spring-cloud   File: HealthIndicatorsConfigurationTests.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheck() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"--spring.jmx.enabled=false");
	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	CompositeHealthContributor bindersHealthContributor = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthContributor).isNotNull();
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();

	assertThat(bindersHealthContributor.stream().map(NamedContributor::getName)).contains("binder1", "binder2");
	assertThat(bindersHealthContributor.getContributor("binder1")).extracting("health").extracting("status")
			.isEqualTo(Status.UP);
	assertThat(bindersHealthContributor.getContributor("binder2")).extracting("health").extracting("status")
			.isEqualTo(Status.UNKNOWN);

	context.close();
}
 
Example #6
Source Project: spring-cloud-stream   Author: spring-cloud   File: HealthIndicatorsConfigurationTests.java    License: Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
@Test
public void healthIndicatorsCheckWhenDisabled() throws Exception {
	ConfigurableApplicationContext context = createBinderTestContext(
			new String[] { "binder1", "binder2" },
			"spring.cloud.stream.defaultBinder:binder2",
			"management.health.binders.enabled:false", "--spring.jmx.enabled=false");

	Binder binder1 = context.getBean(BinderFactory.class).getBinder("binder1",
			MessageChannel.class);
	assertThat(binder1).isInstanceOf(StubBinder1.class);
	Binder binder2 = context.getBean(BinderFactory.class).getBinder("binder2",
			MessageChannel.class);
	assertThat(binder2).isInstanceOf(StubBinder2.class);
	try {
		context.getBean("bindersHealthContributor", CompositeHealthContributor.class);
		fail("The 'bindersHealthContributor' bean should have not been defined");
	}
	catch (NoSuchBeanDefinitionException e) {
	}
	assertThat(
			context.getBean("test1HealthIndicator1", HealthContributor.class))
					.isNotNull();
	assertThat(
			context.getBean("test2HealthIndicator2", HealthContributor.class))
					.isNotNull();
	context.close();
}
 
Example #7
Source Project: spring-cloud-stream-binder-rabbit   Author: spring-cloud   File: RabbitBinderModuleTests.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParentConnectionFactoryInheritedByDefault() throws Exception {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.rabbit.binder.connection-name-prefix=foo",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.single-active-consumer=true");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	CachingConnectionFactory binderConnectionFactory = (CachingConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isInstanceOf(CachingConnectionFactory.class);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);

	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);

	assertThat(bindersHealthIndicator).isNotNull();

	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("rabbit");
	assertThat(indicator).isNotNull();
	assertThat(indicator.health().getStatus())
			.isEqualTo(Status.UP);

	ConnectionFactory publisherConnectionFactory = binderConnectionFactory
			.getPublisherConnectionFactory();
	assertThat(TestUtils.getPropertyValue(publisherConnectionFactory,
			"connection.target")).isNull();
	DirectChannel checkPf = new DirectChannel();
	Binding<MessageChannel> binding = ((RabbitMessageChannelBinder) binder)
			.bindProducer("checkPF", checkPf,
					new ExtendedProducerProperties<>(new RabbitProducerProperties()));
	checkPf.send(new GenericMessage<>("foo".getBytes()));
	binding.unbind();
	assertThat(TestUtils.getPropertyValue(publisherConnectionFactory,
			"connection.target")).isNotNull();

	CachingConnectionFactory cf = this.context
			.getBean(CachingConnectionFactory.class);
	ConnectionNameStrategy cns = TestUtils.getPropertyValue(cf,
			"connectionNameStrategy", ConnectionNameStrategy.class);
	assertThat(cns.obtainNewConnectionName(cf)).isEqualTo("foo#2");
	new RabbitAdmin(rabbitTestSupport.getResource()).deleteExchange("checkPF");
	checkCustomizedArgs();
	binderConnectionFactory.resetConnection();
	binderConnectionFactory.createConnection();
	checkCustomizedArgs();
}
 
Example #8
Source Project: spring-cloud-stream-binder-rabbit   Author: spring-cloud   File: RabbitBinderModuleTests.java    License: Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
public void testParentConnectionFactoryInheritedByDefaultAndRabbitSettingsPropagated() {
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE).run("--server.port=0",
					"--spring.cloud.stream.bindings.source.group=someGroup",
					"--spring.cloud.stream.bindings.input.group=someGroup",
					"--spring.cloud.stream.rabbit.bindings.input.consumer.transacted=true",
					"--spring.cloud.stream.rabbit.bindings.output.producer.transacted=true");
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	Binder<?, ?, ?> binder = binderFactory.getBinder(null, MessageChannel.class);
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	BindingService bindingService = context.getBean(BindingService.class);
	DirectFieldAccessor channelBindingServiceAccessor = new DirectFieldAccessor(
			bindingService);
	// @checkstyle:off
	Map<String, List<Binding<MessageChannel>>> consumerBindings = (Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor
			.getPropertyValue("consumerBindings");
	// @checkstyle:on
	Binding<MessageChannel> inputBinding = consumerBindings.get("input").get(0);
	assertThat(TestUtils.getPropertyValue(inputBinding, "lifecycle.beanName"))
			.isEqualTo("setByCustomizer:someGroup");
	SimpleMessageListenerContainer container = TestUtils.getPropertyValue(
			inputBinding, "lifecycle.messageListenerContainer",
			SimpleMessageListenerContainer.class);
	assertThat(TestUtils.getPropertyValue(container, "beanName"))
			.isEqualTo("setByCustomizerForQueue:input.someGroup,andGroup:someGroup");
	assertThat(TestUtils.getPropertyValue(container, "transactional", Boolean.class))
			.isTrue();
	Map<String, Binding<MessageChannel>> producerBindings = (Map<String, Binding<MessageChannel>>) TestUtils
			.getPropertyValue(bindingService, "producerBindings");
	Binding<MessageChannel> outputBinding = producerBindings.get("output");
	assertThat(TestUtils.getPropertyValue(outputBinding,
			"lifecycle.amqpTemplate.transactional", Boolean.class)).isTrue();
	assertThat(TestUtils.getPropertyValue(outputBinding, "lifecycle.beanName"))
			.isEqualTo("setByCustomizer:output");
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	assertThat(binderConnectionFactory).isInstanceOf(CachingConnectionFactory.class);
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isSameAs(connectionFactory);
	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);

	assertThat(bindersHealthIndicator).isNotNull();

	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("rabbit");
	assertThat(indicator).isNotNull();
	assertThat(indicator.health().getStatus())
			.isEqualTo(Status.UP);

	CachingConnectionFactory cf = this.context
			.getBean(CachingConnectionFactory.class);
	ConnectionNameStrategy cns = TestUtils.getPropertyValue(cf,
			"connectionNameStrategy", ConnectionNameStrategy.class);
	assertThat(cns.obtainNewConnectionName(cf)).startsWith("rabbitConnectionFactory");
	assertThat(TestUtils.getPropertyValue(consumerBindings.get("source").get(0),
			"target.source.h.advised.targetSource.target.beanName"))
		.isEqualTo("setByCustomizer:someGroup");
}
 
Example #9
Source Project: spring-cloud-stream-binder-rabbit   Author: spring-cloud   File: RabbitBinderModuleTests.java    License: Apache License 2.0 4 votes vote down vote up
@Test
public void testParentConnectionFactoryNotInheritedByCustomizedBindersAndProducerRetryBootProperties() {
	List<String> params = new ArrayList<>();
	params.add("--spring.cloud.stream.input.binder=custom");
	params.add("--spring.cloud.stream.output.binder=custom");
	params.add("--spring.cloud.stream.binders.custom.type=rabbit");
	params.add("--spring.cloud.stream.binders.custom.environment.foo=bar");
	params.add("--server.port=0");
	params.add("--spring.rabbitmq.template.retry.enabled=true");
	params.add("--spring.rabbitmq.template.retry.maxAttempts=2");
	params.add("--spring.rabbitmq.template.retry.initial-interval=1000");
	params.add("--spring.rabbitmq.template.retry.multiplier=1.1");
	params.add("--spring.rabbitmq.template.retry.max-interval=3000");
	context = new SpringApplicationBuilder(SimpleProcessor.class)
			.web(WebApplicationType.NONE)
			.run(params.toArray(new String[params.size()]));
	BinderFactory binderFactory = context.getBean(BinderFactory.class);
	// @checkstyle:off
	@SuppressWarnings("unchecked")
	Binder<MessageChannel, ExtendedConsumerProperties<RabbitConsumerProperties>, ExtendedProducerProperties<RabbitProducerProperties>> binder = (Binder<MessageChannel, ExtendedConsumerProperties<RabbitConsumerProperties>, ExtendedProducerProperties<RabbitProducerProperties>>) binderFactory
			.getBinder(null, MessageChannel.class);
	// @checkstyle:on
	assertThat(binder).isInstanceOf(RabbitMessageChannelBinder.class);
	DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder);
	ConnectionFactory binderConnectionFactory = (ConnectionFactory) binderFieldAccessor
			.getPropertyValue("connectionFactory");
	ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class);
	assertThat(binderConnectionFactory).isNotSameAs(connectionFactory);
	CompositeHealthContributor bindersHealthIndicator = context
			.getBean("bindersHealthContributor", CompositeHealthContributor.class);
	assertThat(bindersHealthIndicator);

	RabbitHealthIndicator indicator = (RabbitHealthIndicator) bindersHealthIndicator.getContributor("custom");
	assertThat(indicator).isNotNull();
	assertThat(indicator.health().getStatus()).isEqualTo(Status.UP);
	String name = UUID.randomUUID().toString();
	Binding<MessageChannel> binding = binder.bindProducer(name, new DirectChannel(),
			new ExtendedProducerProperties<>(new RabbitProducerProperties()));
	RetryTemplate template = TestUtils.getPropertyValue(binding,
			"lifecycle.amqpTemplate.retryTemplate", RetryTemplate.class);
	assertThat(template).isNotNull();
	SimpleRetryPolicy retryPolicy = TestUtils.getPropertyValue(template,
			"retryPolicy", SimpleRetryPolicy.class);
	ExponentialBackOffPolicy backOff = TestUtils.getPropertyValue(template,
			"backOffPolicy", ExponentialBackOffPolicy.class);
	assertThat(retryPolicy.getMaxAttempts()).isEqualTo(2);
	assertThat(backOff.getInitialInterval()).isEqualTo(1000L);
	assertThat(backOff.getMultiplier()).isEqualTo(1.1);
	assertThat(backOff.getMaxInterval()).isEqualTo(3000L);
	binding.unbind();
	new RabbitAdmin(rabbitTestSupport.getResource()).deleteExchange(name);
	context.close();
}
 
Example #10
Source Project: spring-cloud-commons   Author: spring-cloud   File: DiscoveryClientHealthIndicatorTests.java    License: Apache License 2.0 4 votes vote down vote up
private Health getHealth(String name) {
	HealthContributor delegate = ((CompositeHealthContributor) this.healthContributor)
			.getContributor(name);
	return ((HealthIndicator) delegate).health();
}