use of org.springframework.cloud.stream.binder.Binding in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method testDefaultGroup.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDefaultGroup() throws Exception {
BindingServiceProperties properties = new BindingServiceProperties();
Map<String, BindingProperties> bindingProperties = new HashMap<>();
BindingProperties props = new BindingProperties();
props.setDestination("foo");
final String inputChannelName = "input";
bindingProperties.put(inputChannelName, props);
properties.setBindings(bindingProperties);
DefaultBinderFactory binderFactory = createMockBinderFactory();
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
BindingService service = new BindingService(properties, binderFactory);
MessageChannel inputChannel = new DirectChannel();
Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
when(binder.bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class))).thenReturn(mockBinding);
Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
assertThat(bindings).hasSize(1);
Binding<MessageChannel> binding = bindings.iterator().next();
assertThat(binding).isSameAs(mockBinding);
service.unbindConsumers(inputChannelName);
verify(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
verify(binding).unbind();
binderFactory.destroy();
}
use of org.springframework.cloud.stream.binder.Binding in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method testLateBindingProducer.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testLateBindingProducer() throws Exception {
BindingServiceProperties properties = new BindingServiceProperties();
properties.setBindingRetryInterval(1);
Map<String, BindingProperties> bindingProperties = new HashMap<>();
BindingProperties props = new BindingProperties();
props.setDestination("foo");
final String outputChannelName = "output";
bindingProperties.put(outputChannelName, props);
properties.setBindings(bindingProperties);
DefaultBinderFactory binderFactory = createMockBinderFactory();
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
BindingService service = new BindingService(properties, binderFactory, scheduler);
MessageChannel outputChannel = new DirectChannel();
final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final CountDownLatch fail = new CountDownLatch(2);
doAnswer(i -> {
fail.countDown();
if (fail.getCount() == 1) {
throw new RuntimeException("fail");
}
return mockBinding;
}).when(binder).bindProducer(eq("foo"), same(outputChannel), any(ProducerProperties.class));
Binding<MessageChannel> binding = service.bindProducer(outputChannel, outputChannelName);
assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(binding).isNotNull();
Binding delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
int n = 0;
while (n++ < 300 && delegate == null) {
Thread.sleep(100);
delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
}
assertThat(delegate).isSameAs(mockBinding);
service.unbindProducers(outputChannelName);
verify(binder, times(2)).bindProducer(eq("foo"), same(outputChannel), any(ProducerProperties.class));
verify(delegate).unbind();
binderFactory.destroy();
scheduler.destroy();
}
use of org.springframework.cloud.stream.binder.Binding in project spring-cloud-stream by spring-cloud.
the class BindingsEndpoint method gatherInputBindings.
@SuppressWarnings("unchecked")
private List<Binding<?>> gatherInputBindings() {
List<Binding<?>> inputBindings = new ArrayList<>();
for (InputBindingLifecycle inputBindingLifecycle : this.inputBindingLifecycles) {
Collection<Binding<?>> lifecycleInputBindings = (Collection<Binding<?>>) new DirectFieldAccessor(inputBindingLifecycle).getPropertyValue("inputBindings");
inputBindings.addAll(lifecycleInputBindings);
}
return inputBindings;
}
use of org.springframework.cloud.stream.binder.Binding in project spring-cloud-stream by spring-cloud.
the class BindingService method bindConsumer.
@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Collection<Binding<T>> bindConsumer(T input, String inputName) {
String bindingTarget = this.bindingServiceProperties.getBindingDestination(inputName);
String[] bindingTargets = StringUtils.commaDelimitedListToStringArray(bindingTarget);
Collection<Binding<T>> bindings = new ArrayList<>();
Binder<T, ConsumerProperties, ?> binder = (Binder<T, ConsumerProperties, ?>) getBinder(inputName, input.getClass());
ConsumerProperties consumerProperties = this.bindingServiceProperties.getConsumerProperties(inputName);
if (binder instanceof ExtendedPropertiesBinder) {
Object extension = ((ExtendedPropertiesBinder) binder).getExtendedConsumerProperties(inputName);
ExtendedConsumerProperties extendedConsumerProperties = new ExtendedConsumerProperties(extension);
BeanUtils.copyProperties(consumerProperties, extendedConsumerProperties);
consumerProperties = extendedConsumerProperties;
}
validate(consumerProperties);
for (String target : bindingTargets) {
Binding<T> binding;
if (input instanceof PollableSource) {
binding = doBindPollableConsumer(input, inputName, binder, consumerProperties, target);
} else {
binding = doBindConsumer(input, inputName, binder, consumerProperties, target);
}
bindings.add(binding);
}
bindings = Collections.unmodifiableCollection(bindings);
this.consumerBindings.put(inputName, new ArrayList<Binding<?>>(bindings));
return bindings;
}
use of org.springframework.cloud.stream.binder.Binding in project spring-cloud-stream by spring-cloud.
the class BindingServiceTests method testLateBindingConsumer.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
// Inconsistent, needs fixing
@Ignore
public void testLateBindingConsumer() throws Exception {
BindingServiceProperties properties = new BindingServiceProperties();
properties.setBindingRetryInterval(1);
Map<String, BindingProperties> bindingProperties = new HashMap<>();
BindingProperties props = new BindingProperties();
props.setDestination("foo");
final String inputChannelName = "input";
bindingProperties.put(inputChannelName, props);
properties.setBindings(bindingProperties);
DefaultBinderFactory binderFactory = createMockBinderFactory();
Binder binder = binderFactory.getBinder("mock", MessageChannel.class);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
BindingService service = new BindingService(properties, binderFactory, scheduler);
MessageChannel inputChannel = new DirectChannel();
final Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
final CountDownLatch fail = new CountDownLatch(2);
doAnswer(i -> {
fail.countDown();
if (fail.getCount() == 1) {
throw new RuntimeException("fail");
}
return mockBinding;
}).when(binder).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
Collection<Binding<MessageChannel>> bindings = service.bindConsumer(inputChannel, inputChannelName);
assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
assertThat(bindings).hasSize(1);
Binding<MessageChannel> delegate = TestUtils.getPropertyValue(bindings.iterator().next(), "delegate", Binding.class);
int n = 0;
while (n++ < 300 && delegate == null) {
Thread.sleep(100);
}
assertThat(delegate).isSameAs(mockBinding);
service.unbindConsumers(inputChannelName);
verify(binder, times(2)).bindConsumer(eq("foo"), isNull(), same(inputChannel), any(ConsumerProperties.class));
verify(delegate).unbind();
binderFactory.destroy();
}
Aggregations