@Test public void testParentConnectionFactoryInheritedByDefault() { context = SpringApplication.run(SimpleProcessor.class, "--server.port=0"); BinderFactory<?> binderFactory = context.getBean(BinderFactory.class); Binder binder = binderFactory.getBinder(null); assertThat(binder, instanceOf(RedisMessageChannelBinder.class)); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); RedisConnectionFactory binderConnectionFactory = (RedisConnectionFactory) binderFieldAccessor.getPropertyValue("connectionFactory"); assertThat(binderConnectionFactory, instanceOf(RedisConnectionFactory.class)); RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class); assertThat(binderConnectionFactory, is(connectionFactory)); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertNotNull(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String,HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor.getPropertyValue("indicators"); assertThat(healthIndicators, hasKey("redis")); assertThat(healthIndicators.get("redis").health().getStatus(), equalTo(Status.UP)); }
@Test public void testParentConnectionFactoryInheritedIfOverridden() { context = new SpringApplication(SimpleProcessor.class, ConnectionFactoryConfiguration.class).run("--server.port=0"); BinderFactory<?> binderFactory = context.getBean(BinderFactory.class); Binder binder = binderFactory.getBinder(null); assertThat(binder, instanceOf(RedisMessageChannelBinder.class)); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); RedisConnectionFactory binderConnectionFactory = (RedisConnectionFactory) binderFieldAccessor.getPropertyValue("connectionFactory"); assertThat(binderConnectionFactory, is(MOCK_CONNECTION_FACTORY)); RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class); assertThat(binderConnectionFactory, is(connectionFactory)); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertNotNull(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String,HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor.getPropertyValue("indicators"); assertThat(healthIndicators, hasKey("redis")); assertThat(healthIndicators.get("redis").health().getStatus(), equalTo(Status.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); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertThat(bindersHealthIndicator).isNotNull(); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey("rabbit"); // mock connection factory behaves as if down assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo(Status.DOWN); }
@SuppressWarnings("rawtypes") @Test public void healthIndicatorsCheck() throws Exception { ConfigurableApplicationContext context = createBinderTestContext(new String[] { "binder1", "binder2" }, "spring.cloud.stream.defaultBinder:binder2"); 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); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); assertThat(bindersHealthIndicator).isNotNull(); assertThat(context.getBean("testHealthIndicator1", CompositeHealthIndicator.class)).isNotNull(); assertThat(context.getBean("testHealthIndicator2", CompositeHealthIndicator.class)).isNotNull(); @SuppressWarnings("unchecked") Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey("binder1"); assertThat(healthIndicators.get("binder1").health().getStatus()).isEqualTo(Status.UP); assertThat(healthIndicators).containsKey("binder2"); assertThat(healthIndicators.get("binder2").health().getStatus()).isEqualTo(Status.UNKNOWN); context.close(); }
@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"); 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("bindersHealthIndicator", CompositeHealthIndicator.class); fail("The 'bindersHealthIndicator' bean should have not been defined"); } catch (NoSuchBeanDefinitionException e) { } assertThat(context.getBean("testHealthIndicator1", CompositeHealthIndicator.class)).isNotNull(); assertThat(context.getBean("testHealthIndicator2", CompositeHealthIndicator.class)).isNotNull(); context.close(); }
private Health internalInvokeHealthIndicators() { CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator( healthAggregator); for (Map.Entry<String, T> h : collectIndicators().entrySet()) { healthIndicator.addHealthIndicator(getKey(h.getKey()), h.getValue()); } return healthIndicator.health(); }
public HealthService(HealthAggregator healthAggregator, Map<String, org.springframework.boot.actuate.health.HealthIndicator> healthIndicators) { Assert.notNull(healthAggregator, "HealthAggregator must not be null"); Assert.notNull(healthIndicators, "HealthIndicators must not be null"); CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator( healthAggregator); for (Map.Entry<String, org.springframework.boot.actuate.health.HealthIndicator> entry : healthIndicators.entrySet()) { healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue()); } this.healthIndicators = healthIndicators; this.healthIndicator = healthIndicator; }
@Test public void testParentConnectionFactoryNotInheritedByCustomizedBinders() { 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=redis"); params.add("--spring.cloud.stream.binders.custom.environment.foo=bar"); params.add("--server.port=0"); context = SpringApplication.run(SimpleProcessor.class, params.toArray(new String[]{})); BinderFactory<?> binderFactory = context.getBean(BinderFactory.class); Binder binder = binderFactory.getBinder(null); assertThat(binder, instanceOf(RedisMessageChannelBinder.class)); DirectFieldAccessor binderFieldAccessor = new DirectFieldAccessor(binder); RedisConnectionFactory binderConnectionFactory = (RedisConnectionFactory) binderFieldAccessor.getPropertyValue("connectionFactory"); RedisConnectionFactory connectionFactory = context.getBean(RedisConnectionFactory.class); assertThat(binderConnectionFactory, not(is(connectionFactory))); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertNotNull(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String,HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor.getPropertyValue("indicators"); assertThat(healthIndicators, hasKey("custom")); assertThat(healthIndicators.get("custom").health().getStatus(), equalTo(Status.UP)); }
protected HealthIndicator createHealthIndicator(Map<String, S> beans) { if (beans.size() == 1) { return createHealthIndicator(beans.values().iterator().next()); } CompositeHealthIndicator composite = new CompositeHealthIndicator( this.healthAggregator); for (Map.Entry<String, S> entry : beans.entrySet()) { composite.addHealthIndicator(entry.getKey(), createHealthIndicator(entry.getValue())); } return composite; }
/** * Create a new {@link HealthIndicator} instance. * @param healthAggregator the health aggregator * @param healthIndicators the health indicators */ public HealthEndpoint(HealthAggregator healthAggregator, Map<String, HealthIndicator> healthIndicators) { super("health", false); Assert.notNull(healthAggregator, "HealthAggregator must not be null"); Assert.notNull(healthIndicators, "HealthIndicators must not be null"); CompositeHealthIndicator healthIndicator = new CompositeHealthIndicator( healthAggregator); for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { healthIndicator.addHealthIndicator(getKey(entry.getKey()), entry.getValue()); } this.healthIndicator = healthIndicator; }
@Test public void testParentConnectionFactoryInheritedByDefault() { context = new SpringApplicationBuilder(SimpleProcessor.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).isInstanceOf(CachingConnectionFactory.class); ConnectionFactory connectionFactory = context.getBean(ConnectionFactory.class); assertThat(binderConnectionFactory).isSameAs(connectionFactory); ConnectionFactory producerConnectionFactory = (ConnectionFactory) binderFieldAccessor .getPropertyValue("producerConnectionFactory"); assertThat(producerConnectionFactory).isNotSameAs(connectionFactory); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); assertThat(bindersHealthIndicator).isNotNull(); @SuppressWarnings("unchecked") Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey(("rabbit")); assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo((Status.UP)); }
@Test @SuppressWarnings("unchecked") public void testParentConnectionFactoryInheritedByDefaultAndRabbitSettingsPropagated() { context = new SpringApplicationBuilder(SimpleProcessor.class) .web(WebApplicationType.NONE) .run("--server.port=0", "--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); Map<String, List<Binding<MessageChannel>>> consumerBindings = (Map<String, List<Binding<MessageChannel>>>) channelBindingServiceAccessor .getPropertyValue("consumerBindings"); Binding<MessageChannel> inputBinding = consumerBindings.get("input").get(0); SimpleMessageListenerContainer container = TestUtils.getPropertyValue(inputBinding, "lifecycle.messageListenerContainer", SimpleMessageListenerContainer.class); 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(); 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); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); assertThat(bindersHealthIndicator).isNotNull(); Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey("rabbit"); assertThat(healthIndicators.get("rabbit").health().getStatus()).isEqualTo(Status.UP); }
@Bean @ConditionalOnMissingBean(RabbitMQHealthIndicator.class) public HealthIndicator rabbitMQHealthIndicator() { if (this.rabbitTemplates.size() == 1) { return new RabbitMQHealthIndicator(this.rabbitTemplates.values().iterator().next(), rabbitMQManagement, properties); } CompositeHealthIndicator composite = new CompositeHealthIndicator(this.healthAggregator); rabbitTemplates.entrySet().stream(). forEach(entry -> composite.addHealthIndicator(entry.getKey(), new RabbitMQHealthIndicator(entry.getValue(), rabbitMQManagement, properties))); return composite; }
@Override public void afterBinderContextInitialized(String binderConfigurationName, ConfigurableApplicationContext binderContext) { if (this.bindersHealthIndicator != null) { OrderedHealthAggregator healthAggregator = new OrderedHealthAggregator(); Map<String, HealthIndicator> indicators = binderContext.getBeansOfType(HealthIndicator.class); // if there are no health indicators in the child context, we just mark // the binder's health as unknown // this can happen due to the fact that configuration is inherited HealthIndicator binderHealthIndicator = indicators.isEmpty() ? new DefaultHealthIndicator() : new CompositeHealthIndicator(healthAggregator, indicators); this.bindersHealthIndicator.addHealthIndicator(binderConfigurationName, binderHealthIndicator); } }
@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); @SuppressWarnings("unchecked") Binder<MessageChannel, ExtendedConsumerProperties<RabbitConsumerProperties>, ExtendedProducerProperties<RabbitProducerProperties>> binder = (Binder<MessageChannel, ExtendedConsumerProperties<RabbitConsumerProperties>, ExtendedProducerProperties<RabbitProducerProperties>>) binderFactory .getBinder(null, MessageChannel.class); 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); CompositeHealthIndicator bindersHealthIndicator = context.getBean("bindersHealthIndicator", CompositeHealthIndicator.class); assertThat(bindersHealthIndicator); DirectFieldAccessor directFieldAccessor = new DirectFieldAccessor(bindersHealthIndicator); @SuppressWarnings("unchecked") Map<String, HealthIndicator> healthIndicators = (Map<String, HealthIndicator>) directFieldAccessor .getPropertyValue("indicators"); assertThat(healthIndicators).containsKey("custom"); assertThat(healthIndicators.get("custom").health().getStatus()).isEqualTo(Status.UP); Binding<MessageChannel> binding = binder.bindProducer("foo", 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(); context.close(); }
@Bean @ConditionalOnMissingBean(name = "bindersHealthIndicator") public CompositeHealthIndicator bindersHealthIndicator() { return new CompositeHealthIndicator(new OrderedHealthAggregator()); }
@Bean public DefaultBinderFactory.Listener bindersHealthIndicatorListener( @Qualifier("bindersHealthIndicator") CompositeHealthIndicator compositeHealthIndicator) { return new BindersHealthIndicatorListener(compositeHealthIndicator); }
BindersHealthIndicatorListener(CompositeHealthIndicator bindersHealthIndicator) { this.bindersHealthIndicator = bindersHealthIndicator; }
@Bean public CompositeHealthIndicator testHealthIndicator1() { return new CompositeHealthIndicator(new OrderedHealthAggregator()); }
@Bean public CompositeHealthIndicator testHealthIndicator2() { return new CompositeHealthIndicator(new OrderedHealthAggregator()); }
public BootHealthCheckHandler(ApplicationInfoManager applicationInfoManager, HealthAggregator aggregator, Map<String, HealthIndicator> healthIndicators) { this.applicationInfoManager = Objects.requireNonNull(applicationInfoManager, "applicationInfoManager"); this.aggregateHealth = new CompositeHealthIndicator(aggregator, healthIndicators); }
public EurekaHealthCheckHandler(HealthAggregator healthAggregator) { Assert.notNull(healthAggregator, "HealthAggregator must not be null"); this.healthIndicator = new CompositeHealthIndicator(healthAggregator); }
protected CompositeHealthIndicator getHealthIndicator() { return healthIndicator; }