Search in sources :

Example 1 with CompositeMessageConverterFactory

use of org.springframework.cloud.stream.converter.CompositeMessageConverterFactory in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method createConverterConfigurer.

private MessageConverterConfigurer createConverterConfigurer(String channelName, BindingProperties bindingProperties) throws Exception {
    BindingServiceProperties bindingServiceProperties = new BindingServiceProperties();
    bindingServiceProperties.getBindings().put(channelName, bindingProperties);
    ConfigurableApplicationContext applicationContext = new GenericApplicationContext();
    applicationContext.refresh();
    bindingServiceProperties.setApplicationContext(applicationContext);
    bindingServiceProperties.setConversionService(new DefaultConversionService());
    bindingServiceProperties.afterPropertiesSet();
    MessageConverterConfigurer messageConverterConfigurer = new MessageConverterConfigurer(bindingServiceProperties, new CompositeMessageConverterFactory(null, null));
    messageConverterConfigurer.setBeanFactory(applicationContext.getBeanFactory());
    return messageConverterConfigurer;
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)

Example 2 with CompositeMessageConverterFactory

use of org.springframework.cloud.stream.converter.CompositeMessageConverterFactory in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method buildStreamListener.

private StreamListenerMessageHandler buildStreamListener(Class<?> handlerClass, String handlerMethodName, Class<?>... parameters) throws Exception {
    String channelName = "reply_" + System.nanoTime();
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerSingleton(channelName, new QueueChannel());
    Method m = ReflectionUtils.findMethod(handlerClass, handlerMethodName, parameters);
    InvocableHandlerMethod method = new InvocableHandlerMethod(this, m);
    HandlerMethodArgumentResolverComposite resolver = new HandlerMethodArgumentResolverComposite();
    CompositeMessageConverterFactory factory = new CompositeMessageConverterFactory();
    resolver.addResolver(new PayloadArgumentResolver(factory.getMessageConverterForAllRegistered()));
    method.setMessageMethodArgumentResolvers(resolver);
    Constructor<?> c = ReflectionUtils.accessibleConstructor(StreamListenerMessageHandler.class, InvocableHandlerMethod.class, boolean.class, String[].class);
    StreamListenerMessageHandler handler = (StreamListenerMessageHandler) c.newInstance(method, false, new String[] {});
    handler.setOutputChannelName(channelName);
    handler.setBeanFactory(context);
    handler.afterPropertiesSet();
    context.refresh();
    return handler;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) QueueChannel(org.springframework.integration.channel.QueueChannel) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) InvocableHandlerMethod(org.springframework.messaging.handler.invocation.InvocableHandlerMethod) Method(java.lang.reflect.Method) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) PayloadArgumentResolver(org.springframework.messaging.handler.annotation.support.PayloadArgumentResolver) HandlerMethodArgumentResolverComposite(org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolverComposite) StreamListenerMessageHandler(org.springframework.cloud.stream.binding.StreamListenerMessageHandler)

Example 3 with CompositeMessageConverterFactory

use of org.springframework.cloud.stream.converter.CompositeMessageConverterFactory 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 4 with CompositeMessageConverterFactory

use of org.springframework.cloud.stream.converter.CompositeMessageConverterFactory 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 5 with CompositeMessageConverterFactory

use of org.springframework.cloud.stream.converter.CompositeMessageConverterFactory in project spring-cloud-stream by spring-cloud.

the class MessageConverterConfigurerTests method testConfigureInputChannelWithLegacyContentType.

@Test
public void testConfigureInputChannelWithLegacyContentType() {
    BindingServiceProperties props = new BindingServiceProperties();
    BindingProperties bindingProps = new BindingProperties();
    bindingProps.setContentType("foo/bar");
    props.setBindings(Collections.singletonMap("foo", bindingProps));
    CompositeMessageConverterFactory converterFactory = new CompositeMessageConverterFactory(Collections.<MessageConverter>emptyList(), null);
    MessageConverterConfigurer configurer = new MessageConverterConfigurer(props, converterFactory);
    QueueChannel in = new QueueChannel();
    configurer.configureInputChannel(in, "foo");
    Foo foo = new Foo();
    in.send(MessageBuilder.withPayload(foo).setHeader(BinderHeaders.BINDER_ORIGINAL_CONTENT_TYPE, "application/json").setHeader(BinderHeaders.SCST_VERSION, "1.x").build());
    Message<?> received = in.receive(0);
    assertThat(received).isNotNull();
    assertThat(received.getPayload()).isEqualTo(foo);
    assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE).toString()).isEqualTo("application/json");
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) CompositeMessageConverterFactory(org.springframework.cloud.stream.converter.CompositeMessageConverterFactory) Test(org.junit.Test)

Aggregations

CompositeMessageConverterFactory (org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)6 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)5 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)4 QueueChannel (org.springframework.integration.channel.QueueChannel)4 Test (org.junit.Test)3 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Ignore (org.junit.Ignore)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 ConsumerProperties (org.springframework.cloud.stream.binder.ConsumerProperties)1 DefaultBinderFactory (org.springframework.cloud.stream.binder.DefaultBinderFactory)1 ExtendedProducerProperties (org.springframework.cloud.stream.binder.ExtendedProducerProperties)1 ExtendedPropertiesBinder (org.springframework.cloud.stream.binder.ExtendedPropertiesBinder)1 ProducerProperties (org.springframework.cloud.stream.binder.ProducerProperties)1