Search in sources :

Example 6 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducer.

@Test
public void testCustomPartitionedProducer() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSource.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorClass=org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass", "--spring.cloud.stream.bindings.output.producer.partitionSelectorClass=org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext) Test(org.junit.Test)

Example 7 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class CustomPartitionedProducerTest method testCustomPartitionedProducerMultipleInstances.

public void testCustomPartitionedProducerMultipleInstances() {
    ApplicationContext context = SpringApplication.run(CustomPartitionedProducerTest.TestSourceMultipleStrategies.class, "--spring.jmx.enabled=false", "--spring.main.web-application-type=none", "--spring.cloud.stream.bindings.output.producer.partitionKeyExtractorName=customPartitionKeyExtractorOne", "--spring.cloud.stream.bindings.output.producer.partitionSelectorName=customPartitionSelectorTwo");
    Source testSource = context.getBean(Source.class);
    DirectChannel messageChannel = (DirectChannel) testSource.output();
    for (ChannelInterceptor channelInterceptor : messageChannel.getChannelInterceptors()) {
        if (channelInterceptor instanceof MessageConverterConfigurer.PartitioningInterceptor) {
            Field partitionHandlerField = ReflectionUtils.findField(MessageConverterConfigurer.PartitioningInterceptor.class, "partitionHandler");
            ReflectionUtils.makeAccessible(partitionHandlerField);
            PartitionHandler partitionHandler = (PartitionHandler) ReflectionUtils.getField(partitionHandlerField, channelInterceptor);
            Field partitonKeyExtractorField = ReflectionUtils.findField(PartitionHandler.class, "partitionKeyExtractorStrategy");
            ReflectionUtils.makeAccessible(partitonKeyExtractorField);
            Field partitonSelectorField = ReflectionUtils.findField(PartitionHandler.class, "partitionSelectorStrategy");
            ReflectionUtils.makeAccessible(partitonSelectorField);
            Assert.assertTrue(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class));
            Assert.assertTrue(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class));
        }
    }
}
Also used : PartitionSelectorStrategy(org.springframework.cloud.stream.binder.PartitionSelectorStrategy) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) CustomPartitionSelectorClass(org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass) PropertySource(org.springframework.context.annotation.PropertySource) MessageSource(org.springframework.integration.core.MessageSource) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) PartitionKeyExtractorStrategy(org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy) CustomPartitionKeyExtractorClass(org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass) ApplicationContext(org.springframework.context.ApplicationContext)

Example 8 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class AggregationTest method testBindableProxyFactoryCaching.

@Test
public void testBindableProxyFactoryCaching() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MockBinderRegistryConfiguration.class, TestSource2.class, TestProcessor.class);
    Map<String, BindableProxyFactory> factories = context.getBeansOfType(BindableProxyFactory.class);
    assertThat(factories).hasSize(2);
    Map<String, Source> sources = context.getBeansOfType(Source.class);
    assertThat(sources).hasSize(1);
    for (Source source : sources.values()) {
        source.output();
    }
    Map<String, FooSource> fooSources = context.getBeansOfType(FooSource.class);
    assertThat(fooSources).hasSize(1);
    for (FooSource source : fooSources.values()) {
        source.output();
    }
    Map<String, Processor> processors = context.getBeansOfType(Processor.class);
    assertThat(processors).hasSize(1);
    for (Processor processor : processors.values()) {
        processor.input();
        processor.output();
    }
    for (BindableProxyFactory factory : factories.values()) {
        Field field = ReflectionUtils.findField(BindableProxyFactory.class, "targetCache");
        ReflectionUtils.makeAccessible(field);
        Map<?, ?> targetCache = (Map<?, ?>) ReflectionUtils.getField(field, factory);
        if (factory.getObjectType() == Source.class) {
            assertThat(targetCache).hasSize(1);
        }
        if (factory.getObjectType() == FooSource.class) {
            assertThat(targetCache).hasSize(1);
        } else if (factory.getObjectType() == Processor.class) {
            assertThat(targetCache).hasSize(2);
        } else {
            Assert.fail("Found unexpected type");
        }
    }
    context.close();
}
Also used : AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) Processor(org.springframework.cloud.stream.messaging.Processor) Source(org.springframework.cloud.stream.messaging.Source) Field(java.lang.reflect.Field) BindableProxyFactory(org.springframework.cloud.stream.binding.BindableProxyFactory) Map(java.util.Map) Test(org.junit.Test)

Example 9 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class StreamEmitterBasicTests method receiveAndValidate.

private static void receiveAndValidate(ConfigurableApplicationContext context) throws InterruptedException {
    Source source = context.getBean(Source.class);
    MessageCollector messageCollector = context.getBean(MessageCollector.class);
    List<String> messages = new ArrayList<>();
    for (int i = 0; i < 1000; i++) {
        messages.add((String) messageCollector.forChannel(source.output()).poll(5000, TimeUnit.MILLISECONDS).getPayload());
    }
    for (int i = 0; i < 1000; i++) {
        assertThat(new String(messages.get(i))).isEqualTo("HELLO WORLD!!" + i);
    }
}
Also used : MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) ArrayList(java.util.ArrayList) Source(org.springframework.cloud.stream.messaging.Source)

Example 10 with Source

use of org.springframework.cloud.stream.messaging.Source in project spring-cloud-stream by spring-cloud.

the class AvroSchemaMessageConverterTests method testSendMessageWithoutLocation.

@Test
public void testSendMessageWithoutLocation() throws Exception {
    ConfigurableApplicationContext sourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.schemaRegistryClient.enabled=false", "--spring.cloud.stream.bindings.output.contentType=avro/bytes");
    Source source = sourceContext.getBean(Source.class);
    User1 firstOutboundFoo = new User1();
    firstOutboundFoo.setName("foo" + UUID.randomUUID().toString());
    firstOutboundFoo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(firstOutboundFoo).build());
    MessageCollector sourceMessageCollector = sourceContext.getBean(MessageCollector.class);
    Message<?> outboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext barSourceContext = SpringApplication.run(AvroSourceApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.schemaRegistryClient.enabled=false", "--spring.cloud.stream.bindings.output.contentType=avro/bytes");
    Source barSource = barSourceContext.getBean(Source.class);
    User2 firstOutboundUser2 = new User2();
    firstOutboundUser2.setFavoriteColor("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setFavoritePlace("foo" + UUID.randomUUID().toString());
    firstOutboundUser2.setName("foo" + UUID.randomUUID().toString());
    barSource.output().send(MessageBuilder.withPayload(firstOutboundUser2).build());
    MessageCollector barSourceMessageCollector = barSourceContext.getBean(MessageCollector.class);
    Message<?> barOutboundMessage = barSourceMessageCollector.forChannel(barSource.output()).poll(1000, TimeUnit.MILLISECONDS);
    assertThat(barOutboundMessage).isNotNull();
    User2 secondUser2OutboundPojo = new User2();
    secondUser2OutboundPojo.setFavoriteColor("foo" + UUID.randomUUID().toString());
    secondUser2OutboundPojo.setFavoritePlace("foo" + UUID.randomUUID().toString());
    secondUser2OutboundPojo.setName("foo" + UUID.randomUUID().toString());
    source.output().send(MessageBuilder.withPayload(secondUser2OutboundPojo).build());
    Message<?> secondBarOutboundMessage = sourceMessageCollector.forChannel(source.output()).poll(1000, TimeUnit.MILLISECONDS);
    ConfigurableApplicationContext sinkContext = SpringApplication.run(AvroSinkApplication.class, "--server.port=0", "--spring.jmx.enabled=false", "--spring.cloud.stream.schemaRegistryClient.enabled=false");
    Sink sink = sinkContext.getBean(Sink.class);
    sink.input().send(outboundMessage);
    sink.input().send(barOutboundMessage);
    sink.input().send(secondBarOutboundMessage);
    List<User1> receivedUsers = sinkContext.getBean(AvroSinkApplication.class).receivedUsers;
    assertThat(receivedUsers).hasSize(3);
    assertThat(receivedUsers.get(0)).isNotSameAs(firstOutboundFoo);
    assertThat(receivedUsers.get(0).getFavoriteColor()).isEqualTo(firstOutboundFoo.getFavoriteColor());
    assertThat(receivedUsers.get(0).getName()).isEqualTo(firstOutboundFoo.getName());
    assertThat(receivedUsers.get(1)).isNotSameAs(firstOutboundUser2);
    assertThat(receivedUsers.get(1).getFavoriteColor()).isEqualTo(firstOutboundUser2.getFavoriteColor());
    assertThat(receivedUsers.get(1).getName()).isEqualTo(firstOutboundUser2.getName());
    assertThat(receivedUsers.get(2)).isNotSameAs(secondUser2OutboundPojo);
    assertThat(receivedUsers.get(2).getFavoriteColor()).isEqualTo(secondUser2OutboundPojo.getFavoriteColor());
    assertThat(receivedUsers.get(2).getName()).isEqualTo(secondUser2OutboundPojo.getName());
    sourceContext.close();
}
Also used : ConfigurableApplicationContext(org.springframework.context.ConfigurableApplicationContext) Sink(org.springframework.cloud.stream.messaging.Sink) MessageCollector(org.springframework.cloud.stream.test.binder.MessageCollector) Source(org.springframework.cloud.stream.messaging.Source) Test(org.junit.Test)

Aggregations

Source (org.springframework.cloud.stream.messaging.Source)21 Test (org.junit.Test)19 MessageCollector (org.springframework.cloud.stream.test.binder.MessageCollector)16 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)15 Message (org.springframework.messaging.Message)10 Field (java.lang.reflect.Field)5 PartitionHandler (org.springframework.cloud.stream.binder.PartitionHandler)4 PartitionKeyExtractorStrategy (org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy)4 PartitionSelectorStrategy (org.springframework.cloud.stream.binder.PartitionSelectorStrategy)4 Sink (org.springframework.cloud.stream.messaging.Sink)4 CustomPartitionKeyExtractorClass (org.springframework.cloud.stream.partitioning.CustomPartitionKeyExtractorClass)4 CustomPartitionSelectorClass (org.springframework.cloud.stream.partitioning.CustomPartitionSelectorClass)4 ApplicationContext (org.springframework.context.ApplicationContext)4 PropertySource (org.springframework.context.annotation.PropertySource)4 DirectChannel (org.springframework.integration.channel.DirectChannel)4 MessageSource (org.springframework.integration.core.MessageSource)4 ChannelInterceptor (org.springframework.messaging.support.ChannelInterceptor)4 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1 BindableProxyFactory (org.springframework.cloud.stream.binding.BindableProxyFactory)1