Sets binding destination if null or if default destination.
fixes gh-139
This commit is contained in:
@@ -73,21 +73,22 @@ public class BusAutoConfiguration implements ApplicationEventPublisherAware {
|
||||
|
||||
public static final String BUS_PATH_MATCHER_NAME = "busPathMatcher";
|
||||
|
||||
@Autowired
|
||||
@Output(SpringCloudBusClient.OUTPUT)
|
||||
private MessageChannel cloudBusOutboundChannel;
|
||||
|
||||
@Autowired
|
||||
private ServiceMatcher serviceMatcher;
|
||||
|
||||
@Autowired
|
||||
private BindingServiceProperties bindings;
|
||||
|
||||
@Autowired
|
||||
private BusProperties bus;
|
||||
|
||||
private ApplicationEventPublisher applicationEventPublisher;
|
||||
|
||||
private final ServiceMatcher serviceMatcher;
|
||||
|
||||
private final BindingServiceProperties bindings;
|
||||
|
||||
private final BusProperties bus;
|
||||
|
||||
public BusAutoConfiguration(ServiceMatcher serviceMatcher, BindingServiceProperties bindings, BusProperties bus) {
|
||||
this.serviceMatcher = serviceMatcher;
|
||||
this.bindings = bindings;
|
||||
this.bus = bus;
|
||||
}
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
BindingProperties inputBinding = this.bindings.getBindings()
|
||||
@@ -98,7 +99,7 @@ public class BusAutoConfiguration implements ApplicationEventPublisherAware {
|
||||
}
|
||||
BindingProperties input = this.bindings.getBindings()
|
||||
.get(SpringCloudBusClient.INPUT);
|
||||
if (input.getDestination() == null) {
|
||||
if (input.getDestination() == null || input.getDestination().equals(SpringCloudBusClient.INPUT)) {
|
||||
input.setDestination(this.bus.getDestination());
|
||||
}
|
||||
BindingProperties outputBinding = this.bindings.getBindings()
|
||||
@@ -109,7 +110,7 @@ public class BusAutoConfiguration implements ApplicationEventPublisherAware {
|
||||
}
|
||||
BindingProperties output = this.bindings.getBindings()
|
||||
.get(SpringCloudBusClient.OUTPUT);
|
||||
if (output.getDestination() == null) {
|
||||
if (output.getDestination() == null || output.getDestination().equals(SpringCloudBusClient.OUTPUT)) {
|
||||
output.setDestination(this.bus.getDestination());
|
||||
}
|
||||
}
|
||||
@@ -120,6 +121,12 @@ public class BusAutoConfiguration implements ApplicationEventPublisherAware {
|
||||
this.applicationEventPublisher = applicationEventPublisher;
|
||||
}
|
||||
|
||||
@Autowired
|
||||
@Output(SpringCloudBusClient.OUTPUT)
|
||||
public void setCloudBusOutboundChannel(MessageChannel cloudBusOutboundChannel) {
|
||||
this.cloudBusOutboundChannel = cloudBusOutboundChannel;
|
||||
}
|
||||
|
||||
@EventListener(classes = RemoteApplicationEvent.class)
|
||||
public void acceptLocal(RemoteApplicationEvent event) {
|
||||
if (this.serviceMatcher.isFromSelf(event)
|
||||
|
||||
@@ -17,6 +17,7 @@
|
||||
|
||||
package org.springframework.cloud.bus;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@@ -36,6 +37,8 @@ import org.springframework.cloud.bus.event.SentApplicationEvent;
|
||||
import org.springframework.cloud.bus.event.UnknownRemoteApplicationEvent;
|
||||
import org.springframework.cloud.context.refresh.ContextRefresher;
|
||||
import org.springframework.cloud.stream.annotation.Output;
|
||||
import org.springframework.cloud.stream.config.BindingProperties;
|
||||
import org.springframework.cloud.stream.config.BindingServiceProperties;
|
||||
import org.springframework.cloud.stream.test.binder.TestSupportBinderAutoConfiguration;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
@@ -55,6 +58,8 @@ import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
public class BusAutoConfigurationTests {
|
||||
|
||||
@@ -231,6 +236,67 @@ public class BusAutoConfigurationTests {
|
||||
// No Exception expected
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initSetsBindingDestinationIfNullDefault() {
|
||||
HashMap<String, BindingProperties> properties = new HashMap<>();
|
||||
properties.put(SpringCloudBusClient.INPUT, new BindingProperties());
|
||||
properties.put(SpringCloudBusClient.OUTPUT, new BindingProperties());
|
||||
|
||||
testDestinations(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initSetsBindingDestinationIfNotNullDefault() {
|
||||
HashMap<String, BindingProperties> properties = new HashMap<>();
|
||||
BindingProperties input = new BindingProperties();
|
||||
input.setDestination(SpringCloudBusClient.INPUT);
|
||||
properties.put(SpringCloudBusClient.INPUT, input);
|
||||
BindingProperties output = new BindingProperties();
|
||||
output.setDestination(SpringCloudBusClient.OUTPUT);
|
||||
properties.put(SpringCloudBusClient.OUTPUT, output);
|
||||
|
||||
testDestinations(properties);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initDoesNotOverrideCustomDestination() {
|
||||
HashMap<String, BindingProperties> properties = new HashMap<>();
|
||||
BindingProperties input = new BindingProperties();
|
||||
input.setDestination("mydestination");
|
||||
properties.put(SpringCloudBusClient.INPUT, input);
|
||||
BindingProperties output = new BindingProperties();
|
||||
output.setDestination("mydestination");
|
||||
properties.put(SpringCloudBusClient.OUTPUT, output);
|
||||
|
||||
setupBusAutoConfig(properties);
|
||||
|
||||
BindingProperties inputProps = properties.get(SpringCloudBusClient.INPUT);
|
||||
assertThat(inputProps.getDestination()).isEqualTo("mydestination");
|
||||
|
||||
BindingProperties outputProps = properties.get(SpringCloudBusClient.OUTPUT);
|
||||
assertThat(outputProps.getDestination()).isEqualTo("mydestination");
|
||||
}
|
||||
|
||||
private void testDestinations(HashMap<String, BindingProperties> properties) {
|
||||
BusProperties bus = setupBusAutoConfig(properties);
|
||||
|
||||
BindingProperties input = properties.get(SpringCloudBusClient.INPUT);
|
||||
assertThat(input.getDestination()).isEqualTo(bus.getDestination());
|
||||
|
||||
BindingProperties output = properties.get(SpringCloudBusClient.OUTPUT);
|
||||
assertThat(output.getDestination()).isEqualTo(bus.getDestination());
|
||||
}
|
||||
|
||||
private BusProperties setupBusAutoConfig(HashMap<String, BindingProperties> properties) {
|
||||
BindingServiceProperties serviceProperties = mock(BindingServiceProperties.class);
|
||||
when(serviceProperties.getBindings()).thenReturn(properties);
|
||||
|
||||
BusProperties bus = new BusProperties();
|
||||
BusAutoConfiguration configuration = new BusAutoConfiguration(mock(ServiceMatcher.class), serviceProperties, bus);
|
||||
configuration.init();
|
||||
return bus;
|
||||
}
|
||||
|
||||
// see https://github.com/spring-cloud/spring-cloud-bus/issues/101
|
||||
@Test
|
||||
@Ignore // TODO: replicate problem
|
||||
|
||||
Reference in New Issue
Block a user