Commit 15556a44 by Johannes Stelzer

Add optional automatic deregistration on shutdown - turned off by default; only…

Add optional automatic deregistration on shutdown - turned off by default; only periodically try to register when the context is active
parent e9bba5d6
......@@ -30,6 +30,8 @@ public class AdminProperties {
private String password;
private boolean autoDeregistration;
public void setUrl(String url) {
this.url = url;
}
......@@ -87,4 +89,17 @@ public class AdminProperties {
public String getPassword() {
return password;
}
/**
*
* @return wether the application deregisters automatically on shutdown.
*/
public boolean isAutoDeregistration() {
return autoDeregistration;
}
public void setAutoDeregistration(boolean autoDeregistration) {
this.autoDeregistration = autoDeregistration;
}
}
......@@ -21,8 +21,15 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.ApplicationContextEvent;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.ContextStartedEvent;
import org.springframework.context.event.ContextStoppedEvent;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
......@@ -46,17 +53,18 @@ public class SpringBootAdminClientAutoConfiguration {
*/
@Bean
@ConditionalOnMissingBean
public ApplicationRegistrator registrator(AdminProperties adminProps, AdminClientProperties clientProps) {
return new ApplicationRegistrator(createRestTemplate(adminProps), adminProps, clientProps);
public ApplicationRegistrator registrator(AdminProperties admin,
AdminClientProperties client) {
return new ApplicationRegistrator(createRestTemplate(admin), admin, client);
}
protected RestTemplate createRestTemplate(AdminProperties adminProps) {
protected RestTemplate createRestTemplate(AdminProperties admin) {
RestTemplate template = new RestTemplate();
template.getMessageConverters().add(new MappingJackson2HttpMessageConverter());
if (adminProps.getUsername() != null) {
if (admin.getUsername() != null) {
template.setInterceptors(Arrays.<ClientHttpRequestInterceptor> asList(new BasicAuthHttpRequestInterceptor(
adminProps.getUsername(), adminProps.getPassword())));
admin.getUsername(), admin.getPassword())));
}
return template;
......@@ -66,20 +74,45 @@ public class SpringBootAdminClientAutoConfiguration {
* TaskRegistrar that triggers the RegistratorTask every ten seconds.
*/
@Bean
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator, AdminProperties adminProps) {
public ScheduledTaskRegistrar taskRegistrar(final ApplicationRegistrator registrator,
final ConfigurableApplicationContext context,
AdminProperties admin) {
ScheduledTaskRegistrar registrar = new ScheduledTaskRegistrar();
Runnable registratorTask = new Runnable() {
@Override
public void run() {
if (context.isActive()) {
registrator.register();
}
}
};
registrar.addFixedRateTask(registratorTask, adminProps.getPeriod());
registrar.addFixedRateTask(registratorTask, admin.getPeriod());
return registrar;
}
/**
* ApplicationListener triggering rigestration after refresh/shutdown
*/
@Bean
public ApplicationListener<ApplicationContextEvent> RegistrationListener(
final ApplicationRegistrator registrator, final AdminProperties admin) {
return new ApplicationListener<ApplicationContextEvent>() {
public void onApplicationEvent(ApplicationContextEvent event) {
if (event instanceof ContextRefreshedEvent
|| event instanceof ContextStartedEvent) {
registrator.register();
}
else if (admin.isAutoDeregistration()
&& (event instanceof ContextClosedEvent || event instanceof ContextStoppedEvent)) {
registrator.deregister();
}
}
};
}
@Configuration
@ConditionalOnExpression("${endpoints.logfile.enabled:true}")
@ConditionalOnProperty("logging.file")
......
......@@ -68,10 +68,11 @@ public class ApplicationRegistrator {
* @return true if successful
*/
public boolean register() {
Application self = createApplication();
Application self = null;
String adminUrl = admin.getUrl() + '/' + admin.getContextPath();
try {
self = createApplication();
ResponseEntity<Application> response = template.postForEntity(adminUrl,
new HttpEntity<Application>(self, HTTP_HEADERS), Application.class);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment