Search in sources :

Example 1 with Binder

use of org.springframework.cloud.stream.binder.Binder 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();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 2 with Binder

use of org.springframework.cloud.stream.binder.Binder 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();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 3 with Binder

use of org.springframework.cloud.stream.binder.Binder in project spring-cloud-stream by spring-cloud.

the class BindingService method bindProducer.

@SuppressWarnings({ "unchecked", "rawtypes" })
public <T> Binding<T> bindProducer(T output, String outputName) {
    String bindingTarget = this.bindingServiceProperties.getBindingDestination(outputName);
    Binder<T, ?, ProducerProperties> binder = (Binder<T, ?, ProducerProperties>) getBinder(outputName, output.getClass());
    ProducerProperties producerProperties = this.bindingServiceProperties.getProducerProperties(outputName);
    if (binder instanceof ExtendedPropertiesBinder) {
        Object extension = ((ExtendedPropertiesBinder) binder).getExtendedProducerProperties(outputName);
        ExtendedProducerProperties extendedProducerProperties = new ExtendedProducerProperties<>(extension);
        BeanUtils.copyProperties(producerProperties, extendedProducerProperties);
        producerProperties = extendedProducerProperties;
    }
    validate(producerProperties);
    Binding<T> binding = doBindProducer(output, bindingTarget, binder, producerProperties);
    this.producerBindings.put(outputName, binding);
    return binding;
}
Also used : PollableConsumerBinder(org.springframework.cloud.stream.binder.PollableConsumerBinder) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) DataBinder(org.springframework.validation.DataBinder) Binder(org.springframework.cloud.stream.binder.Binder) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties)

Example 4 with Binder

use of org.springframework.cloud.stream.binder.Binder 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;
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) ExtendedConsumerProperties(org.springframework.cloud.stream.binder.ExtendedConsumerProperties) ArrayList(java.util.ArrayList) ExtendedConsumerProperties(org.springframework.cloud.stream.binder.ExtendedConsumerProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) PollableSource(org.springframework.cloud.stream.binder.PollableSource) PollableConsumerBinder(org.springframework.cloud.stream.binder.PollableConsumerBinder) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) DataBinder(org.springframework.validation.DataBinder) Binder(org.springframework.cloud.stream.binder.Binder) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)

Example 5 with Binder

use of org.springframework.cloud.stream.binder.Binder 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();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

Binder (org.springframework.cloud.stream.binder.Binder)8 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)7 Test (org.junit.Test)6 Binding (org.springframework.cloud.stream.binder.Binding)6 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)6 HashMap (java.util.HashMap)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)5 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)5 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)5 DirectChannel (org.springframework.integration.channel.DirectChannel)5 MessageChannel (org.springframework.messaging.MessageChannel)5 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)2 PollableConsumerBinder (org.springframework.cloud.stream.binder.PollableConsumerBinder)2 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 DataBinder (org.springframework.validation.DataBinder)2 ArrayList (java.util.ArrayList)1 Ignore (org.junit.Ignore)1