Search in sources :

Example 6 with BindingServiceProperties

use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertSimpler.

@Test
public void testConvertSimpler() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    BindingServiceProperties bsps = this.context.getBean(BindingServiceProperties.class);
    BindingProperties props = new BindingProperties();
    props.setContentType("text/plain");
    bsps.setBindings(Collections.singletonMap("foo", props));
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("foo".getBytes()));
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final AtomicReference<Object> payload = new AtomicReference<>();
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
    // test the cache for coverage
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<String>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(String.class);
    assertThat(payload.get()).isEqualTo("foo");
}
Also used : BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) Test(org.junit.Test)

Example 7 with BindingServiceProperties

use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.

the class MessageConverterConfigurerTests method testConfigureOutputChannelCannotConvert.

@Test
@Ignore
public void testConfigureOutputChannelCannotConvert() {
    BindingServiceProperties props = new BindingServiceProperties();
    BindingProperties bindingProps = new BindingProperties();
    bindingProps.setContentType("foo/bar");
    props.setBindings(Collections.singletonMap("foo", bindingProps));
    MessageConverter converter = new AbstractMessageConverter(new MimeType("foo", "bar")) {

        @Override
        protected boolean supports(Class<?> clazz) {
            return true;
        }

        @Override
        protected Object convertToInternal(Object payload, MessageHeaders headers, Object conversionHint) {
            return null;
        }
    };
    CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>singletonList(converter), null);
    MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
    QueueChannel out = new QueueChannel();
    configurer.configureOutputChannel(out, "foo");
    try {
        out.send(new GenericMessage<Foo>(new Foo(), Collections.<String, Object>singletonMap(MessageHeaders.CONTENT_TYPE, "bad/ct")));
        fail("Expected MessageConversionException: " + out.receive(0));
    } catch (MessageConversionException e) {
        assertThat(e.getMessage()).endsWith("to the configured output type: 'foo/bar'");
    }
}
Also used : MessageConversionException(org.springframework.messaging.converter.MessageConversionException) QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MessageConverter(org.springframework.messaging.converter.MessageConverter) AbstractMessageConverter(org.springframework.messaging.converter.AbstractMessageConverter) MimeType(org.springframework.util.MimeType) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) MessageHeaders(org.springframework.messaging.MessageHeaders) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 8 with BindingServiceProperties

use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.

the class BinderAwareChannelResolver method resolveDestination.

@SuppressWarnings("unchecked")
@Override
public MessageChannel resolveDestination(String channelName) {
    try {
        return super.resolveDestination(channelName);
    } catch (DestinationResolutionException e) {
    // intentionally empty; will check again while holding the monitor
    }
    synchronized (this) {
        BindingServiceProperties bindingServiceProperties = this.bindingService.getBindingServiceProperties();
        String[] dynamicDestinations = bindingServiceProperties.getDynamicDestinations();
        boolean dynamicAllowed = ObjectUtils.isEmpty(dynamicDestinations) || ObjectUtils.containsElement(dynamicDestinations, channelName);
        try {
            return super.resolveDestination(channelName);
        } catch (DestinationResolutionException e) {
            if (!dynamicAllowed) {
                throw e;
            }
        }
        MessageChannel channel = this.bindingTargetFactory.createOutput(channelName);
        this.beanFactory.registerSingleton(channelName, channel);
        this.instrumentChannelWithGlobalInterceptors(channel, channelName);
        channel = (MessageChannel) this.beanFactory.initializeBean(channel, channelName);
        if (this.newBindingCallback != null) {
            ProducerProperties producerProperties = bindingServiceProperties.getProducerProperties(channelName);
            Object extendedProducerProperties = this.bindingService.getExtendedProducerProperties(channel, channelName);
            this.newBindingCallback.configure(channelName, channel, producerProperties, extendedProducerProperties);
            bindingServiceProperties.updateProducerProperties(channelName, producerProperties);
        }
        Binding<MessageChannel> binding = this.bindingService.bindProducer(channel, channelName);
        this.dynamicDestinationsBindable.addOutputBinding(channelName, binding);
        return channel;
    }
}
Also used : ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) MessageChannel(org.springframework.messaging.MessageChannel) DestinationResolutionException(org.springframework.messaging.core.DestinationResolutionException) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties)

Example 9 with BindingServiceProperties

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

Example 10 with BindingServiceProperties

use of org.springframework.cloud.stream.config.BindingServiceProperties in project spring-cloud-stream by spring-cloud.

the class BindingServiceTests method testExplicitGroup.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testExplicitGroup() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    props.setDestination("foo");
    props.setGroup("fooGroup");
    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"), eq("fooGroup"), 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"), eq(props.getGroup()), 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)

Aggregations

BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)18 Test (org.junit.Test)14 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)12 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)11 DirectChannel (org.springframework.integration.channel.DirectChannel)11 HashMap (java.util.HashMap)10 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)10 MessageChannel (org.springframework.messaging.MessageChannel)9 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)7 Binder (org.springframework.cloud.stream.binder.Binder)6 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)6 Binding (org.springframework.cloud.stream.binder.Binding)5 CompositeMessageConverterFactory (org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)5 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)4 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Ignore (org.junit.Ignore)2 MessageConverterConfigurer (org.springframework.cloud.stream.binding.MessageConverterConfigurer)2