org.springframework.cloud.context.refresh.ContextRefresher Java Examples

The following examples show how to use org.springframework.cloud.context.refresh.ContextRefresher. 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 File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void springMainSourcesEmptyInRefreshCycle() throws Exception {
	this.context = new SpringApplicationBuilder(Empty.class)
			.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
			.properties("spring.cloud.bootstrap.name:none").run();
	RefreshScope scope = new RefreshScope();
	scope.setApplicationContext(this.context);
	// spring.main.sources should be empty when the refresh cycle starts (we don't
	// want any config files from the application context getting into the one used to
	// construct the environment for refresh)
	TestPropertyValues
			.of("spring.main.sources="
					+ ExternalPropertySourceLocator.class.getName())
			.applyTo(this.context);
	ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
	RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
	Collection<String> keys = endpoint.refresh();
	then(keys.contains("external.message")).as("Wrong keys: " + keys).isFalse();
}
 
Example #2
Source File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 6 votes vote down vote up
@Test
public void keysComputedWhenChangesInExternalProperties() throws Exception {
	this.context = new SpringApplicationBuilder(Empty.class)
			.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
			.properties("spring.cloud.bootstrap.name:none").run();
	RefreshScope scope = new RefreshScope();
	scope.setApplicationContext(this.context);
	TestPropertyValues
			.of("spring.cloud.bootstrap.sources="
					+ ExternalPropertySourceLocator.class.getName())
			.applyTo(this.context.getEnvironment(), Type.MAP, "defaultProperties");
	ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
	RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
	Collection<String> keys = endpoint.refresh();
	then(keys.contains("external.message")).isTrue().as("Wrong keys: " + keys);
}
 
Example #3
Source File: FlatMapTests.java    From spring-cloud-sleuth with Apache License 2.0 6 votes vote down vote up
@Test
public void should_work_with_flat_maps_with_on_last_operator_instrumentation(
		CapturedOutput capture) {
	// given
	ConfigurableApplicationContext context = new SpringApplicationBuilder(
			FlatMapTests.TestConfiguration.class, Issue866Configuration.class)
					.web(WebApplicationType.REACTIVE)
					.properties("server.port=0", "spring.jmx.enabled=false",
							"spring.sleuth.reactor.decorate-on-each=false",
							"spring.application.name=TraceWebFlux2Tests",
							"security.basic.enabled=false",
							"management.security.enabled=false")
					.run();
	assertReactorTracing(context, capture);

	try {
		System.setProperty("spring.sleuth.reactor.decorate-on-each", "true");
		// trigger context refreshed
		context.getBean(ContextRefresher.class).refresh();
		assertReactorTracing(context, capture);
	}
	finally {
		System.clearProperty("spring.sleuth.reactor.decorate-on-each");
	}
}
 
Example #4
Source File: ServiceCombConfigAutoConfiguration.java    From spring-cloud-huawei with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(name = "spring.cloud.servicecomb.config.watch.enabled",
    matchIfMissing = true)
public ConfigWatch configWatch(ServiceCombConfigProperties serviceCombConfigProperties,
    ServiceCombConfigClient serviceCombConfigClient,
    ContextRefresher contextRefresher, RefreshRecord refreshRecord,
    ServiceCombAkSkProperties serviceCombAkSkProperties) {
  ConfigWatch watch = new ConfigWatch();
  watch.setProject(serviceCombAkSkProperties.getProject());
  watch.setContextRefresher(contextRefresher);
  watch.setServiceCombConfigClient(serviceCombConfigClient);
  watch.setServiceCombConfigProperties(serviceCombConfigProperties);
  watch.setRefreshRecord(refreshRecord);
  return watch;
}
 
Example #5
Source File: EurekaClientAutoConfigurationTests.java    From spring-cloud-netflix with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldReregisterHealthCheckHandlerAfterRefresh() throws Exception {
	TestPropertyValues.of("eureka.client.healthcheck.enabled=true")
			.applyTo(this.context);
	setupContext(RefreshAutoConfiguration.class,
			AutoServiceRegistrationConfiguration.class);

	EurekaClient oldEurekaClient = getLazyInitEurekaClient();

	HealthCheckHandler healthCheckHandler = this.context
			.getBean("eurekaHealthCheckHandler", HealthCheckHandler.class);

	assertThat(healthCheckHandler).isInstanceOf(EurekaHealthCheckHandler.class);
	assertThat(oldEurekaClient.getHealthCheckHandler()).isSameAs(healthCheckHandler);

	ContextRefresher refresher = this.context.getBean("contextRefresher",
			ContextRefresher.class);
	refresher.refresh();

	EurekaClient newEurekaClient = getLazyInitEurekaClient();
	HealthCheckHandler newHealthCheckHandler = this.context
			.getBean("eurekaHealthCheckHandler", HealthCheckHandler.class);

	assertThat(healthCheckHandler).isSameAs(newHealthCheckHandler);
	assertThat(oldEurekaClient).isNotSameAs(newEurekaClient);
	assertThat(newEurekaClient.getHealthCheckHandler()).isSameAs(healthCheckHandler);
}
 
Example #6
Source File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void shutdownHooksCleaned() {
	try (ConfigurableApplicationContext context = new SpringApplicationBuilder(
			Empty.class).web(WebApplicationType.NONE).bannerMode(Mode.OFF).run()) {
		RefreshScope scope = new RefreshScope();
		scope.setApplicationContext(context);
		ContextRefresher contextRefresher = new ContextRefresher(context, scope);
		RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
		int count = countShutdownHooks();
		endpoint.refresh();
		int after = countShutdownHooks();
		then(count).isEqualTo(after).as("Shutdown hooks not cleaned on refresh");
	}
}
 
Example #7
Source File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void eventsPublishedInOrder() throws Exception {
	this.context = new SpringApplicationBuilder(Empty.class)
			.web(WebApplicationType.NONE).bannerMode(Mode.OFF).run();
	RefreshScope scope = new RefreshScope();
	scope.setApplicationContext(this.context);
	ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
	RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
	Empty empty = this.context.getBean(Empty.class);
	endpoint.refresh();
	int after = empty.events.size();
	then(2).isEqualTo(after).as("Shutdown hooks not cleaned on refresh");
	then(empty.events.get(0) instanceof EnvironmentChangeEvent).isTrue();
}
 
Example #8
Source File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void keysComputedWhenOveridden() throws Exception {
	this.context = new SpringApplicationBuilder(Empty.class)
			.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
			.properties("spring.cloud.bootstrap.name:none").run();
	RefreshScope scope = new RefreshScope();
	scope.setApplicationContext(this.context);
	this.context.getEnvironment().setActiveProfiles("override");
	ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
	RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
	Collection<String> keys = endpoint.refresh();
	then(keys.contains("message")).isTrue().as("Wrong keys: " + keys);
}
 
Example #9
Source File: RefreshEndpointTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void keysComputedWhenAdded() throws Exception {
	this.context = new SpringApplicationBuilder(Empty.class)
			.web(WebApplicationType.NONE).bannerMode(Mode.OFF)
			.properties("spring.cloud.bootstrap.name:none").run();
	RefreshScope scope = new RefreshScope();
	scope.setApplicationContext(this.context);
	this.context.getEnvironment().setActiveProfiles("local");
	ContextRefresher contextRefresher = new ContextRefresher(this.context, scope);
	RefreshEndpoint endpoint = new RefreshEndpoint(contextRefresher);
	Collection<String> keys = endpoint.refresh();
	then(keys.contains("added")).isTrue().as("Wrong keys: " + keys);
}
 
Example #10
Source File: RefreshAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void neverRefreshable() {
	try (ConfigurableApplicationContext context = getApplicationContext(
			WebApplicationType.NONE, Config.class, "countingconfig.foo=bar",
			"spring.cloud.refresh.never-refreshable:"
					+ CountingConfigProps.class.getName())) {
		CountingConfigProps configProps = context.getBean(CountingConfigProps.class);
		context.getBean(ContextRefresher.class).refresh();
		assertThat(configProps.count)
				.as("config props was rebound when it should not have been")
				.hasValue(1);
	}
}
 
Example #11
Source File: RefreshAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void extraRefreshables() {
	try (ConfigurableApplicationContext context = getApplicationContext(
			WebApplicationType.NONE, Config.class, "sealedconfig.foo=bar",
			"spring.cloud.refresh.extra-refreshable:"
					+ SealedConfigProps.class.getName())) {
		context.getBean(SealedConfigProps.class);
		context.getBean(ContextRefresher.class).refresh();
	}
}
 
Example #12
Source File: RefreshAutoConfigurationTests.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Test
public void refreshables() {
	try (ConfigurableApplicationContext context = getApplicationContext(
			WebApplicationType.NONE, Config.class, "config.foo=bar",
			"spring.cloud.refresh.refreshable:"
					+ SealedConfigProps.class.getName())) {
		context.getBean(SealedConfigProps.class);
		context.getBean(ContextRefresher.class).refresh();
	}
}
 
Example #13
Source File: RefreshEndpointAutoConfiguration.java    From spring-cloud-commons with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnBean(ContextRefresher.class)
@ConditionalOnAvailableEndpoint
@ConditionalOnMissingBean
public RefreshEndpoint refreshEndpoint(ContextRefresher contextRefresher) {
	return new RefreshEndpoint(contextRefresher);
}
 
Example #14
Source File: BusAutoConfigurationTests.java    From spring-cloud-bus with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore // TODO: replicate problem
public void serviceMatcherIdIsConstantAfterRefresh() {
	this.context = SpringApplication.run(new Class[] { RefreshConfig.class, },
			new String[] { "--spring.main.allow-bean-definition-overriding=true" });
	String originalServiceId = this.context.getBean(ServiceMatcher.class)
			.getServiceId();
	this.context.getBean(ContextRefresher.class).refresh();
	String newServiceId = this.context.getBean(ServiceMatcher.class).getServiceId();
	assertThat(newServiceId).isEqualTo(originalServiceId);
}
 
Example #15
Source File: BusRefreshAutoConfiguration.java    From spring-cloud-bus with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnProperty(value = "spring.cloud.bus.refresh.enabled",
		matchIfMissing = true)
@ConditionalOnBean(ContextRefresher.class)
public RefreshListener refreshListener(ContextRefresher contextRefresher,
		ServiceMatcher serviceMatcher) {
	return new RefreshListener(contextRefresher, serviceMatcher);
}
 
Example #16
Source File: ConfigReloadAutoConfiguration.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * Provides the action to execute when the configuration changes.
 */
@Bean
@ConditionalOnMissingBean
public ConfigurationUpdateStrategy configurationUpdateStrategy(ConfigReloadProperties properties, ConfigurableApplicationContext ctx, RestartEndpoint restarter, ContextRefresher refresher) {
    switch (properties.getStrategy()) {
    case RESTART_CONTEXT:
        return new ConfigurationUpdateStrategy(properties.getStrategy().name(), restarter::restart);
    case REFRESH:
        return new ConfigurationUpdateStrategy(properties.getStrategy().name(), refresher::refresh);
    case SHUTDOWN:
        return new ConfigurationUpdateStrategy(properties.getStrategy().name(), ctx::close);
    }
    throw new IllegalStateException("Unsupported configuration update strategy: " + properties.getStrategy());
}
 
Example #17
Source File: PeriodRefreshAutoConfiguration.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public PeriodCheckRefreshEventEmitter refreshEventEmitter(ConfigServicePropertySourceLocator locator,
                                                          ContextRefresher refresher, Environment environment) {
    return new PeriodCheckRefreshEventEmitter((MultiModuleConfigServicePropertySourceLocator) locator,
            refresher, environment);
}
 
Example #18
Source File: ConfigReloadAutoConfiguration.java    From spring-cloud-kubernetes with Apache License 2.0 5 votes vote down vote up
/**
 * @param properties config reload properties
 * @param ctx application context
 * @param restarter restart endpoint
 * @param refresher context refresher
 * @return provides the action to execute when the configuration changes.
 */
@Bean
@ConditionalOnMissingBean
public ConfigurationUpdateStrategy configurationUpdateStrategy(
		ConfigReloadProperties properties, ConfigurableApplicationContext ctx,
		@Autowired(required = false) RestartEndpoint restarter,
		ContextRefresher refresher) {
	switch (properties.getStrategy()) {
	case RESTART_CONTEXT:
		Assert.notNull(restarter, "Restart endpoint is not enabled");
		return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
				() -> {
					wait(properties);
					restarter.restart();
				});
	case REFRESH:
		return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
				refresher::refresh);
	case SHUTDOWN:
		return new ConfigurationUpdateStrategy(properties.getStrategy().name(),
				() -> {
					wait(properties);
					ctx.close();
				});
	}
	throw new IllegalStateException("Unsupported configuration update strategy: "
			+ properties.getStrategy());
}
 
Example #19
Source File: PeriodCheckRefreshEventEmitter.java    From spring-cloud-formula with Apache License 2.0 5 votes vote down vote up
public PeriodCheckRefreshEventEmitter(MultiModuleConfigServicePropertySourceLocator locator,
                                      ContextRefresher refresher, Environment environment) {
    super(locator, refresher, environment);

    thread = new Thread(this);
    thread.setDaemon(true);
    thread.setName("Config-Client-Period-Checker");
}
 
Example #20
Source File: ZookeeperPropertySourceLocatorTests.java    From spring-cloud-zookeeper with Apache License 2.0 4 votes vote down vote up
@Bean
public ContextRefresher contextRefresher(ConfigurableApplicationContext context,
		RefreshScope scope) {
	return new ContextRefresher(context, scope);
}
 
Example #21
Source File: ConfigClientWatch.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public ConfigClientWatch(ContextRefresher refresher) {
	this.refresher = refresher;
}
 
Example #22
Source File: ConfigClientAutoConfiguration.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
@Bean
public ConfigClientWatch configClientWatch(ContextRefresher contextRefresher) {
	return new ConfigClientWatch(contextRefresher);
}
 
Example #23
Source File: ConfigWatch.java    From spring-cloud-huawei with Apache License 2.0 4 votes vote down vote up
public void setContextRefresher(ContextRefresher contextRefresher) {
  this.contextRefresher = contextRefresher;
}
 
Example #24
Source File: ConfigurationEventProcessor.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
public ConfigurationEventProcessor(TopicProcessor<CloudEventImpl> eventProcessor, ContextRefresher contextRefresher, String applicationName) {
    this.eventProcessor = eventProcessor;
    this.contextRefresher = contextRefresher;
    this.applicationName = applicationName;
}
 
Example #25
Source File: RSocketCloudConfigAutoConfiguration.java    From alibaba-rsocket-broker with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean
public ConfigurationEventProcessor rSocketConfigListener(@Autowired ContextRefresher contextRefresher,
                                                         @Autowired @Qualifier("reactiveCloudEventProcessor") TopicProcessor<CloudEventImpl> eventProcessor) {
    return new ConfigurationEventProcessor(eventProcessor, contextRefresher, applicationName);
}
 
Example #26
Source File: RefreshEventEmitter.java    From spring-cloud-formula with Apache License 2.0 4 votes vote down vote up
public RefreshEventEmitter(ConfigServicePropertySourceLocator locator, ContextRefresher refresher, Environment environment) {
    this.locator = locator;
    this.refresher = refresher;
    this.environment = environment;
}
 
Example #27
Source File: RefreshEventEmitter.java    From spring-cloud-formula with Apache License 2.0 4 votes vote down vote up
ContextRefresher getRefresher() {
    return refresher;
}
 
Example #28
Source File: RefreshEventListener.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
public RefreshEventListener(ContextRefresher refresh) {
	this.refresh = refresh;
}
 
Example #29
Source File: RefreshEndpoint.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
public RefreshEndpoint(ContextRefresher contextRefresher) {
	this.contextRefresher = contextRefresher;
}
 
Example #30
Source File: RefreshAutoConfiguration.java    From spring-cloud-commons with Apache License 2.0 4 votes vote down vote up
@Bean
public RefreshEventListener refreshEventListener(ContextRefresher contextRefresher) {
	return new RefreshEventListener(contextRefresher);
}