Search in sources :

Example 6 with MessageConverterConfigurer

use of org.springframework.cloud.stream.binding.MessageConverterConfigurer 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 MessageConverterConfigurer

use of org.springframework.cloud.stream.binding.MessageConverterConfigurer in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testErrors.

@Test
public void testErrors() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(((String) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(2);
    properties.setBackOffInitialInterval(0);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    final CountDownLatch latch = new CountDownLatch(1);
    context.getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, SubscribableChannel.class).subscribe(m -> {
        latch.countDown();
    });
    final AtomicInteger count = new AtomicInteger();
    assertThat(pollableSource.poll(received -> {
        count.incrementAndGet();
        throw new RuntimeException("test recoverer");
    })).isTrue();
    assertThat(count.get()).isEqualTo(2);
    Message<?> lastError = binder.getLastError();
    assertThat(lastError).isNotNull();
    assertThat(((Exception) lastError.getPayload()).getCause().getMessage()).isEqualTo("test recoverer");
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) CountDownLatch(java.util.concurrent.CountDownLatch) MessageChannel(org.springframework.messaging.MessageChannel) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) SubscribableChannel(org.springframework.messaging.SubscribableChannel) Test(org.junit.Test)

Example 8 with MessageConverterConfigurer

use of org.springframework.cloud.stream.binding.MessageConverterConfigurer in project spring-cloud-stream by spring-cloud.

the class AbstractBinderTests method createBindableChannel.

protected DirectChannel createBindableChannel(String channelName, BindingProperties bindingProperties, boolean inputChannel) throws Exception {
    MessageConverterConfigurer messageConverterConfigurer = createConverterConfigurer(channelName, bindingProperties);
    DirectChannel channel = new DirectChannel();
    channel.setBeanName(channelName);
    if (inputChannel) {
        messageConverterConfigurer.configureInputChannel(channel, channelName);
    } else {
        messageConverterConfigurer.configureOutputChannel(channel, channelName);
    }
    return channel;
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer)

Example 9 with MessageConverterConfigurer

use of org.springframework.cloud.stream.binding.MessageConverterConfigurer in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testEmbedded.

@Test
public void testEmbedded() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    binder.setMessageSourceDelegate(() -> {
        MessageValues original = new MessageValues("foo".getBytes(), Collections.singletonMap(MessageHeaders.CONTENT_TYPE, "application/octet-stream"));
        byte[] payload = new byte[0];
        try {
            payload = EmbeddedHeaderUtils.embedHeaders(original, MessageHeaders.CONTENT_TYPE);
        } catch (Exception e) {
            fail(e.getMessage());
        }
        return new GenericMessage<>(payload);
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setHeaderMode(HeaderMode.embeddedHeaders);
    DefaultPollableMessageSource pollableSource = new DefaultPollableMessageSource(this.messageConverter);
    configurer.configurePolledMessageSource(pollableSource, "foo");
    pollableSource.addInterceptor(new ChannelInterceptorAdapter() {

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.withPayload(new String((byte[]) message.getPayload()).toUpperCase()).copyHeaders(message.getHeaders()).build();
        }
    });
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    assertThat(pollableSource.poll(received -> {
        assertThat(received.getPayload()).isEqualTo("FOO");
        assertThat(received.getHeaders().get(MessageHeaders.CONTENT_TYPE)).isEqualTo("application/octet-stream");
    })).isTrue();
}
Also used : ChannelInterceptorAdapter(org.springframework.messaging.support.ChannelInterceptorAdapter) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageChannel(org.springframework.messaging.MessageChannel) MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Test(org.junit.Test)

Example 10 with MessageConverterConfigurer

use of org.springframework.cloud.stream.binding.MessageConverterConfigurer in project spring-cloud-stream by spring-cloud.

the class PollableConsumerTests method testConvertSimple.

@Test
public void testConvertSimple() {
    TestChannelBinder binder = createBinder();
    MessageConverterConfigurer configurer = context.getBean(MessageConverterConfigurer.class);
    binder.setMessageSourceDelegate(() -> new GenericMessage<>("{\"foo\":\"bar\"}".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<Foo>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(Foo.class);
    assertThat(((Foo) payload.get()).getFoo()).isEqualTo("bar");
    // test the cache for coverage
    assertThat(pollableSource.poll(received -> {
        payload.set(received.getPayload());
    }, new ParameterizedTypeReference<Foo>() {
    })).isTrue();
    assertThat(payload.get()).isInstanceOf(Foo.class);
    assertThat(((Foo) payload.get()).getFoo()).isEqualTo("bar");
}
Also used : MessageConverterConfigurer(org.springframework.cloud.stream.binding.MessageConverterConfigurer) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestChannelBinder(org.springframework.cloud.stream.binder.test.TestChannelBinder) Test(org.junit.Test)

Aggregations

MessageConverterConfigurer (org.springframework.cloud.stream.binding.MessageConverterConfigurer)10 Test (org.junit.Test)8 TestChannelBinder (org.springframework.cloud.stream.binder.test.TestChannelBinder)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Message (org.springframework.messaging.Message)4 MessageChannel (org.springframework.messaging.MessageChannel)4 ChannelInterceptorAdapter (org.springframework.messaging.support.ChannelInterceptorAdapter)4 GenericMessage (org.springframework.messaging.support.GenericMessage)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 BindingServiceProperties (org.springframework.cloud.stream.config.BindingServiceProperties)2 SubscribableChannel (org.springframework.messaging.SubscribableChannel)2 List (java.util.List)1 Map (java.util.Map)1 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)1 CompositeMessageConverterFactory (org.springframework.cloud.stream.converter.CompositeMessageConverterFactory)1 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)1 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)1 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)1 DirectChannel (org.springframework.integration.channel.DirectChannel)1