Search in sources :

Example 1 with DefaultBinderFactory

use of org.springframework.cloud.stream.binder.DefaultBinderFactory 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 DefaultBinderFactory

use of org.springframework.cloud.stream.binder.DefaultBinderFactory 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 DefaultBinderFactory

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

the class BindingServiceTests method testConsumerPropertiesValidation.

@Test
public void testConsumerPropertiesValidation() {
    BindingServiceProperties serviceProperties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    ConsumerProperties consumerProperties = new ConsumerProperties();
    consumerProperties.setConcurrency(0);
    props.setDestination("foo");
    props.setConsumer(consumerProperties);
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    serviceProperties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    BindingService service = new BindingService(serviceProperties, binderFactory);
    MessageChannel inputChannel = new DirectChannel();
    try {
        service.bindConsumer(inputChannel, inputChannelName);
        fail("Consumer properties should be validated.");
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageContaining("Concurrency should be greater than zero.");
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) Test(org.junit.Test)

Example 4 with DefaultBinderFactory

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

the class BindingServiceTests method checkDynamicBinding.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void checkDynamicBinding() {
    BindingServiceProperties properties = new BindingServiceProperties();
    BindingProperties bindingProperties = new BindingProperties();
    bindingProperties.setProducer(new ProducerProperties());
    properties.setBindings(Collections.singletonMap("foo", bindingProperties));
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    final ExtendedPropertiesBinder binder = mock(ExtendedPropertiesBinder.class);
    Properties extendedProps = new Properties();
    when(binder.getExtendedProducerProperties(anyString())).thenReturn(extendedProps);
    Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
    final AtomicReference<MessageChannel> dynamic = new AtomicReference<>();
    when(binder.bindProducer(matches("foo"), any(DirectChannel.class), any(ProducerProperties.class))).thenReturn(mockBinding);
    BindingService bindingService = new BindingService(properties, binderFactory) {

        @Override
        protected <T> Binder<T, ?, ?> getBinder(String channelName, Class<T> bindableType) {
            return binder;
        }
    };
    SubscribableChannelBindingTargetFactory bindableSubscribableChannelFactory = new SubscribableChannelBindingTargetFactory(new MessageConverterConfigurer(properties, new CompositeMessageConverterFactory()));
    final AtomicBoolean callbackInvoked = new AtomicBoolean();
    BinderAwareChannelResolver resolver = new BinderAwareChannelResolver(bindingService, bindableSubscribableChannelFactory, new DynamicDestinationsBindable(), (name, channel, props, extended) -> {
        callbackInvoked.set(true);
        assertThat(name).isEqualTo("foo");
        assertThat(channel).isNotNull();
        assertThat(props).isNotNull();
        assertThat(extended).isSameAs(extendedProps);
        props.setUseNativeEncoding(true);
        extendedProps.setProperty("bar", "baz");
    });
    ConfigurableListableBeanFactory beanFactory = mock(ConfigurableListableBeanFactory.class);
    when(beanFactory.getBean("foo", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
    when(beanFactory.getBean("bar", MessageChannel.class)).thenThrow(new NoSuchBeanDefinitionException(MessageChannel.class));
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            dynamic.set(invocation.getArgument(1));
            return null;
        }
    }).when(beanFactory).registerSingleton(eq("foo"), any(MessageChannel.class));
    doAnswer(new Answer<Object>() {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            return dynamic.get();
        }
    }).when(beanFactory).initializeBean(any(MessageChannel.class), eq("foo"));
    resolver.setBeanFactory(beanFactory);
    MessageChannel resolved = resolver.resolveDestination("foo");
    assertThat(resolved).isSameAs(dynamic.get());
    ArgumentCaptor<ProducerProperties> captor = ArgumentCaptor.forClass(ProducerProperties.class);
    verify(binder).bindProducer(eq("foo"), eq(dynamic.get()), captor.capture());
    assertThat(captor.getValue().isUseNativeEncoding()).isTrue();
    assertThat(captor.getValue()).isInstanceOf(ExtendedProducerProperties.class);
    assertThat(((ExtendedProducerProperties) captor.getValue()).getExtension()).isSameAs(extendedProps);
    doReturn(dynamic.get()).when(beanFactory).getBean("foo", MessageChannel.class);
    properties.setDynamicDestinations(new String[] { "foo" });
    resolved = resolver.resolveDestination("foo");
    assertThat(resolved).isSameAs(dynamic.get());
    properties.setDynamicDestinations(new String[] { "test" });
    try {
        resolved = resolver.resolveDestination("bar");
        fail();
    } catch (DestinationResolutionException e) {
        assertThat(e).hasMessageContaining("Failed to find MessageChannel bean with name 'bar'");
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) Properties(java.util.Properties) AtomicReference(java.util.concurrent.atomic.AtomicReference) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MessageChannel(org.springframework.messaging.MessageChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ExtendedPropertiesBinder(org.springframework.cloud.stream.binder.ExtendedPropertiesBinder) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException) ConfigurableListableBeanFactory(org.springframework.beans.factory.config.ConfigurableListableBeanFactory) ExtendedProducerProperties(org.springframework.cloud.stream.binder.ExtendedProducerProperties) Test(org.junit.Test)

Example 5 with DefaultBinderFactory

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

the class BinderFactoryConfiguration method binderFactory.

@Bean
@ConditionalOnMissingBean(BinderFactory.class)
public DefaultBinderFactory binderFactory(BinderTypeRegistry binderTypeRegistry, BindingServiceProperties bindingServiceProperties) {
    DefaultBinderFactory binderFactory = new DefaultBinderFactory(getBinderConfigurations(binderTypeRegistry, bindingServiceProperties), binderTypeRegistry);
    binderFactory.setDefaultBinder(bindingServiceProperties.getDefaultBinder());
    binderFactory.setListeners(binderFactoryListeners);
    return binderFactory;
}
Also used : DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Aggregations

DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)11 Test (org.junit.Test)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)10 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)10 DirectChannel (org.springframework.integration.channel.DirectChannel)10 HashMap (java.util.HashMap)9 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)8 MessageChannel (org.springframework.messaging.MessageChannel)8 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)6 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)6 Binder (org.springframework.cloud.stream.binder.Binder)5 Binding (org.springframework.cloud.stream.binder.Binding)5 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)3 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 BinderFactoryConfiguration (org.springframework.cloud.stream.config.BinderFactoryConfiguration)2 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)2 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1