allow defaults for spring-cloud-stream bindings

This commit is contained in:
Spencer Gibb
2015-09-22 16:15:54 -06:00
parent 639a87bfa1
commit 0d92cb0803
6 changed files with 43 additions and 18 deletions

View File

@@ -16,6 +16,11 @@
package org.springframework.cloud.netflix.hystrix.stream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -28,10 +33,6 @@ import org.springframework.scheduling.annotation.EnableScheduling;
import com.netflix.hystrix.HystrixCircuitBreaker;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
/**
* Autoconfiguration for a Spring Cloud Hystrix on AMQP. Enabled by default if
* spring-rabbit is on the classpath, and can be switched off with
@@ -59,11 +60,21 @@ public class HystrixStreamAutoConfiguration {
@Autowired
private ChannelBindingProperties bindings;
@Autowired
private HystrixStreamProperties properties;
@PostConstruct
public void init() {
this.bindings.getBindings().put(HystrixStreamClient.OUTPUT,
new HashMap<>(Collections.singletonMap("destination",
HystrixStreamClient.HYSTRIX_STREAM_DESTINATION)));
Object outputBinding = this.bindings.getBindings().get(HystrixStreamClient.OUTPUT);
if (outputBinding == null || outputBinding instanceof String) {
this.bindings.getBindings().put(HystrixStreamClient.OUTPUT,
new HashMap<String, Object>());
}
@SuppressWarnings("unchecked")
Map<String, Object> output = (Map<String, Object>) this.bindings.getBindings().get(HystrixStreamClient.OUTPUT);
if (!output.containsKey("destination") || HystrixStreamClient.OUTPUT.equals(outputBinding)) {
output.put("destination", this.properties.getDestination());
}
}
@Bean

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.netflix.hystrix.stream;
import org.springframework.cloud.netflix.hystrix.HystrixConstants;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
@@ -24,7 +23,7 @@ import org.springframework.messaging.MessageChannel;
* @author Dave Syer
*
*/
public interface HystrixStreamClient extends HystrixConstants {
public interface HystrixStreamClient {
String OUTPUT = "hystrixStreamOutput";

View File

@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.hystrix.stream;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.netflix.hystrix.HystrixConstants;
/**
* @author Spencer Gibb
@@ -33,4 +34,6 @@ public class HystrixStreamProperties {
private boolean sendId = true;
private String destination = HystrixConstants.HYSTRIX_STREAM_DESTINATION;
}

View File

@@ -16,6 +16,11 @@
package org.springframework.cloud.netflix.turbine.stream;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -24,10 +29,6 @@ import org.springframework.cloud.stream.config.ChannelBindingProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.Collections;
import java.util.HashMap;
/**
* Autoconfiguration for a Spring Cloud Turbine using Spring Cloud Stream.
* Enabled by default if spring-cloud-stream is on the classpath, and can be
@@ -55,11 +56,21 @@ public class TurbineStreamAutoConfiguration {
@Autowired
private ChannelBindingProperties bindings;
@Autowired
private TurbineStreamProperties properties;
@PostConstruct
public void init() {
this.bindings.getBindings().put(TurbineStreamClient.INPUT,
new HashMap<>(Collections.singletonMap("destination",
TurbineStreamClient.HYSTRIX_STREAM_DESTINATION)));
Object inputBinding = this.bindings.getBindings().get(TurbineStreamClient.INPUT);
if (inputBinding == null || inputBinding instanceof String) {
this.bindings.getBindings().put(TurbineStreamClient.INPUT,
new HashMap<String, Object>());
}
@SuppressWarnings("unchecked")
Map<String, Object> input = (Map<String, Object>) this.bindings.getBindings().get(TurbineStreamClient.INPUT);
if (!input.containsKey("destination") || TurbineStreamClient.INPUT.equals(inputBinding)) {
input.put("destination", properties.getDestination());
}
}
@Bean

View File

@@ -16,7 +16,6 @@
package org.springframework.cloud.netflix.turbine.stream;
import org.springframework.cloud.netflix.hystrix.HystrixConstants;
import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel;
@@ -24,7 +23,7 @@ import org.springframework.messaging.SubscribableChannel;
* @author Dave Syer
*
*/
public interface TurbineStreamClient extends HystrixConstants {
public interface TurbineStreamClient {
String INPUT = "turbineStreamInput";

View File

@@ -20,6 +20,7 @@ import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.netflix.hystrix.HystrixConstants;
/**
* @author Dave Syer
@@ -31,4 +32,5 @@ public class TurbineStreamProperties {
@Value("${server.port:8989}")
private int port = 8989;
private String destination = HystrixConstants.HYSTRIX_STREAM_DESTINATION;
}