Search in sources :

Example 1 with ExtendedPropertiesBinder

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

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

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

Aggregations

ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)3 Binder (org.springframework.cloud.stream.binder.Binder)2 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)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 DataBinder (org.springframework.validation.DataBinder)2 ArrayList (java.util.ArrayList)1 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Test (org.junit.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 NoSuchBeanDefinitionException (org.springframework.beans.factory.NoSuchBeanDefinitionException)1 ConfigurableListableBeanFactory (org.springframework.beans.factory.config.ConfigurableListableBeanFactory)1 Binding (org.springframework.cloud.stream.binder.Binding)1 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)1 ExtendedConsumerProperties (org.springframework.cloud.stream.binder.ExtendedConsumerProperties)1 PollableSource (org.springframework.cloud.stream.binder.PollableSource)1