added /bus/env endpoint and EnvironmentChangeListener

This commit is contained in:
Spencer Gibb
2014-08-27 12:01:32 -06:00
parent 782a06b70e
commit 548c163e0b
7 changed files with 111 additions and 10 deletions

View File

@@ -2,12 +2,18 @@ package org.springframework.platform.bus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.platform.bus.endpoint.BusEndpoint;
import org.springframework.platform.bus.endpoint.EnvironmentBusEndpoint;
import org.springframework.platform.bus.endpoint.RefreshBusEndpoint;
import org.springframework.platform.bus.event.EnvironmentChangeListener;
import org.springframework.platform.bus.event.RefreshListener;
import org.springframework.platform.config.client.RefreshEndpoint;
import org.springframework.platform.context.environment.EnvironmentManager;
/**
* @author Spencer Gibb
@@ -17,23 +23,40 @@ import org.springframework.platform.config.client.RefreshEndpoint;
public class BusAutoConfiguration {
private static final Logger logger = LoggerFactory.getLogger(BusAutoConfiguration.class);
@Bean
public BusEndpoint busEndpoint() {
return new BusEndpoint();
}
@ConditionalOnClass(RefreshEndpoint.class)
@ConditionalOnBean(RefreshEndpoint.class)
protected static class BusRefreshConfiguration {
@Bean
@ConditionalOnExpression("${bus.refresh.enabled:true}")
public RefreshListener refreshListener() {
return new RefreshListener();
}
return new RefreshListener();
}
@Bean
@ConditionalOnExpression("${endpoints.bus.refresh.enabled:true}")
public RefreshBusEndpoint refreshBusEndpoint() {
return new RefreshBusEndpoint();
}
}
@ConditionalOnClass(EnvironmentManager.class)
@ConditionalOnBean(EnvironmentManager.class)
protected static class BusEnvironmentConfiguration {
@Bean
@ConditionalOnExpression("${bus.env.enabled:true}")
public EnvironmentChangeListener environmentChangeListener() {
return new EnvironmentChangeListener();
}
@Bean
public BusEndpoint busEndpoint() {
return new BusEndpoint();
@ConditionalOnExpression("${endpoints.bus.env.enabled:true}")
public EnvironmentBusEndpoint environmentBusEndpoint() {
return new EnvironmentBusEndpoint();
}
}
}

View File

@@ -1,8 +1,9 @@
package org.springframework.platform.bus;
package org.springframework.platform.bus.endpoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;
@@ -21,6 +22,10 @@ public class AbstractBusEndpoint implements MvcEndpoint {
return env.getProperty("spring.application.name");
}
protected void publish(ApplicationEvent event) {
context.publishEvent(event);
}
@Override
public String getPath() {
return "/" + this.delegate.getId();

View File

@@ -1,4 +1,4 @@
package org.springframework.platform.bus;
package org.springframework.platform.bus.endpoint;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.boot.context.properties.ConfigurationProperties;

View File

@@ -0,0 +1,25 @@
package org.springframework.platform.bus.endpoint;
import org.springframework.platform.bus.event.EnvironmentChangeRemoteApplicationEvent;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
/**
* @author Spencer Gibb
*/
public class EnvironmentBusEndpoint extends AbstractBusEndpoint {
@RequestMapping(value = "env", method = RequestMethod.POST)
@ResponseBody
//TODO: make this an abstract method in AbstractBusEndpoint?
public void env(@RequestParam Map<String, String> params,
@RequestParam(value = "destination", required = false) String destination) {
publish(new EnvironmentChangeRemoteApplicationEvent(this, getAppName(), destination, params));
}
}

View File

@@ -1,9 +1,9 @@
package org.springframework.platform.bus;
package org.springframework.platform.bus.endpoint;
import org.springframework.platform.bus.event.RefreshRemoteApplicationEvent;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
/**
@@ -13,8 +13,8 @@ public class RefreshBusEndpoint extends AbstractBusEndpoint {
@RequestMapping(value = "refresh", method = RequestMethod.POST)
@ResponseBody
public void refresh(@RequestBody String destination) {
context.publishEvent(new RefreshRemoteApplicationEvent(this, getAppName(), destination));
public void refresh(@RequestParam(value = "destination", required = false) String destination) {
publish(new RefreshRemoteApplicationEvent(this, getAppName(), destination));
}

View File

@@ -0,0 +1,28 @@
package org.springframework.platform.bus.event;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.platform.context.environment.EnvironmentManager;
import java.util.Map;
/**
* @author Spencer Gibb
*/
public class EnvironmentChangeListener implements ApplicationListener<EnvironmentChangeRemoteApplicationEvent> {
private static final Logger logger = LoggerFactory.getLogger(EnvironmentChangeListener.class);
@Autowired
private EnvironmentManager env;
@Override
public void onApplicationEvent(EnvironmentChangeRemoteApplicationEvent event) {
Map<String, String> values = event.getValues();
logger.info("Received remote environment change request. Keys/values to update {}", values);
for (Map.Entry<String, String> entry: values.entrySet()) {
env.setProperty(entry.getKey(), entry.getValue());
}
}
}

View File

@@ -0,0 +1,20 @@
package org.springframework.platform.bus.event;
import lombok.Data;
import java.util.Map;
/**
* @author Spencer Gibb
*/
@Data
public class EnvironmentChangeRemoteApplicationEvent extends RemoteApplicationEvent {
private final Map<String, String> values;
public EnvironmentChangeRemoteApplicationEvent(Object source, String originService,
String destinationService,
Map<String, String> values) {
super(source, originService, destinationService);
this.values = values;
}
}