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

The following examples show how to use org.springframework.cloud.context.refresh.ContextRefresher. These examples are extracted from open source projects. 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-sleuth   Source File: FlatMapTests.java    License: 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 2
Source Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 4
@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
@Bean
@ConditionalOnMissingBean
public PeriodCheckRefreshEventEmitter refreshEventEmitter(ConfigServicePropertySourceLocator locator,
                                                          ContextRefresher refresher, Environment environment) {
    return new PeriodCheckRefreshEventEmitter((MultiModuleConfigServicePropertySourceLocator) locator,
            refresher, environment);
}
 
Example 6
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 7
/**
 * @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 8
/**
 * 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 9
@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 10
@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 11
@Bean
@ConditionalOnBean(ContextRefresher.class)
@ConditionalOnAvailableEndpoint
@ConditionalOnMissingBean
public RefreshEndpoint refreshEndpoint(ContextRefresher contextRefresher) {
	return new RefreshEndpoint(contextRefresher);
}
 
Example 12
@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
@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 14
@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 15
Source Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 16
Source Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 17
Source Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 18
Source Project: spring-cloud-commons   Source File: RefreshEndpointTests.java    License: 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 19
@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 20
Source Project: spring-cloud-huawei   Source File: ConfigWatch.java    License: Apache License 2.0 4 votes vote down vote up
public void setContextRefresher(ContextRefresher contextRefresher) {
  this.contextRefresher = contextRefresher;
}
 
Example 21
public ConfigurationEventProcessor(TopicProcessor<CloudEventImpl> eventProcessor, ContextRefresher contextRefresher, String applicationName) {
    this.eventProcessor = eventProcessor;
    this.contextRefresher = contextRefresher;
    this.applicationName = applicationName;
}
 
Example 22
@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@Bean
public ConfigurationEventProcessor rSocketConfigListener(@Autowired ContextRefresher contextRefresher,
                                                         @Autowired @Qualifier("reactiveCloudEventProcessor") TopicProcessor<CloudEventImpl> eventProcessor) {
    return new ConfigurationEventProcessor(eventProcessor, contextRefresher, applicationName);
}
 
Example 23
Source Project: spring-cloud-formula   Source File: RefreshEventEmitter.java    License: 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 24
Source Project: spring-cloud-formula   Source File: RefreshEventEmitter.java    License: Apache License 2.0 4 votes vote down vote up
ContextRefresher getRefresher() {
    return refresher;
}
 
Example 25
Source Project: spring-s3-properties-loader   Source File: S3PropertiesContext.java    License: MIT License 4 votes vote down vote up
public S3PropertiesContext(ApplicationContext applicationContext, EnvironmentManager environment, ContextRefresher contextRefresher, S3Service s3Service) {
	this.applicationContext = applicationContext;
	this.environment = environment;
	this.contextRefresher = contextRefresher;
	this.s3Service = s3Service;
}
 
Example 26
@Bean
S3PropertiesContext refreshProperties(ApplicationContext applicationContext, EnvironmentManager environmentManager, ContextRefresher contextRefresher, S3Service s3Service) {
	return new S3PropertiesContext(applicationContext, environmentManager, contextRefresher, s3Service);
}
 
Example 27
@Bean
public ContextRefresher contextRefresher(ConfigurableApplicationContext context,
		RefreshScope scope) {
	return new ContextRefresher(context, scope);
}
 
Example 28
Source Project: spring-cloud-bus   Source File: RefreshListener.java    License: Apache License 2.0 4 votes vote down vote up
public RefreshListener(ContextRefresher contextRefresher,
		ServiceMatcher serviceMatcher) {
	this.contextRefresher = contextRefresher;
	this.serviceMatcher = serviceMatcher;
}
 
Example 29
@Bean
@ConditionalOnMissingBean
public ContextRefresher contextRefresher(ConfigurableApplicationContext context,
		RefreshScope scope) {
	return new ContextRefresher(context, scope);
}
 
Example 30
@Bean
public RefreshEventListener refreshEventListener(ContextRefresher contextRefresher) {
	return new RefreshEventListener(contextRefresher);
}