Search in sources :

Example 91 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.

the class BindingServiceTests method testConsumerPropertiesValidation.

@Test
public void testConsumerPropertiesValidation() {
    BindingServiceProperties serviceProperties = new BindingServiceProperties();
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    ConsumerProperties consumerProperties = new ConsumerProperties();
    consumerProperties.setConcurrency(0);
    props.setDestination("foo");
    props.setConsumer(consumerProperties);
    final String inputChannelName = "input";
    bindingProperties.put(inputChannelName, props);
    serviceProperties.setBindings(bindingProperties);
    DefaultBinderFactory binderFactory = createMockBinderFactory();
    BindingService service = new BindingService(serviceProperties, binderFactory, new ObjectMapper());
    MessageChannel inputChannel = new DirectChannel();
    try {
        service.bindConsumer(inputChannel, inputChannelName);
        fail("Consumer properties should be validated.");
    } catch (IllegalStateException e) {
        assertThat(e).hasMessageContaining("Concurrency should be greater than zero.");
    }
}
Also used : MessageChannel(org.springframework.messaging.MessageChannel) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) Test(org.junit.Test)

Example 92 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.

the class BindingServiceTests method testLateBindingProducer.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testLateBindingProducer() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    properties.setBindingRetryInterval(1);
    Map<String, BindingProperties> bindingProperties = new HashMap<>();
    BindingProperties props = new BindingProperties();
    props.setDestination("foo");
    final String outputChannelName = "output";
    bindingProperties.put(outputChannelName, 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, new ObjectMapper());
    MessageChannel outputChannel = 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).bindProducer(eq("foo"), same(outputChannel), any(ProducerProperties.class));
    Binding<MessageChannel> binding = service.bindProducer(outputChannel, outputChannelName);
    assertThat(fail.await(10, TimeUnit.SECONDS)).isTrue();
    assertThat(binding).isNotNull();
    Binding delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
    int n = 0;
    while (n++ < 300 && delegate == null) {
        Thread.sleep(100);
        delegate = TestUtils.getPropertyValue(binding, "delegate", Binding.class);
    }
    assertThat(delegate).isSameAs(mockBinding);
    service.unbindProducers(outputChannelName);
    verify(binder, times(2)).bindProducer(eq("foo"), same(outputChannel), any(ProducerProperties.class));
    verify(delegate).unbind();
    binderFactory.destroy();
    scheduler.destroy();
}
Also used : Binding(org.springframework.cloud.stream.binder.Binding) ProducerProperties(org.springframework.cloud.stream.binder.ProducerProperties) HashMap(java.util.HashMap) DirectChannel(org.springframework.integration.channel.DirectChannel) BindingProperties(org.springframework.cloud.stream.config.BindingProperties) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Binder(org.springframework.cloud.stream.binder.Binder) MessageChannel(org.springframework.messaging.MessageChannel) BindingServiceProperties(org.springframework.cloud.stream.config.BindingServiceProperties) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 93 with DirectChannel

use of org.springframework.integration.channel.DirectChannel 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.cloud.stream.output-bindings=output", "--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", "--spring.cloud.stream.default-binder=mock");
    DirectChannel messageChannel = context.getBean("output-out-0", DirectChannel.class);
    for (ChannelInterceptor channelInterceptor : messageChannel.getInterceptors()) {
        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);
            assertThat(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class)).isTrue();
            assertThat(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class)).isTrue();
        }
    }
}
Also used : Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler) Test(org.junit.Test)

Example 94 with DirectChannel

use of org.springframework.integration.channel.DirectChannel 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", "--spring.cloud.stream.default-binder=mock");
    DirectChannel messageChannel = context.getBean("output-out-0", DirectChannel.class);
    for (ChannelInterceptor channelInterceptor : messageChannel.getInterceptors()) {
        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);
            assertThat(((PartitionKeyExtractorStrategy) ReflectionUtils.getField(partitonKeyExtractorField, partitionHandler)).getClass().equals(CustomPartitionKeyExtractorClass.class)).isTrue();
            assertThat(((PartitionSelectorStrategy) ReflectionUtils.getField(partitonSelectorField, partitionHandler)).getClass().equals(CustomPartitionSelectorClass.class)).isTrue();
        }
    }
}
Also used : Field(java.lang.reflect.Field) ApplicationContext(org.springframework.context.ApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) PartitionHandler(org.springframework.cloud.stream.binder.PartitionHandler)

Example 95 with DirectChannel

use of org.springframework.integration.channel.DirectChannel in project spring-cloud-stream by spring-cloud.

the class AbstractMessageChannelBinder method registerErrorInfrastructure.

/**
 * Register an error channel for the destination when an async send error is received.
 * Bridge the channel to the global error channel (if present).
 * @param destination the destination.
 * @return the channel.
 */
private SubscribableChannel registerErrorInfrastructure(ProducerDestination destination) {
    String errorChannelName = errorsBaseName(destination);
    SubscribableChannel errorChannel = new PublishSubscribeChannel();
    if (getApplicationContext().containsBean(errorChannelName)) {
        Object errorChannelObject = getApplicationContext().getBean(errorChannelName);
        if (!(errorChannelObject instanceof SubscribableChannel)) {
            throw new IllegalStateException("Error channel '" + errorChannelName + "' must be a SubscribableChannel");
        }
        if (errorChannelObject instanceof DirectChannel) {
            errorChannelName = "bridged." + errorChannelName;
            BridgeHandler bridge = new BridgeHandler();
            bridge.setOutputChannel((MessageChannel) errorChannelObject);
            errorChannel.subscribe(bridge);
        }
    } else {
        ((GenericApplicationContext) getApplicationContext()).registerBean(errorChannelName, SubscribableChannel.class, () -> errorChannel);
    }
    MessageChannel defaultErrorChannel = null;
    if (getApplicationContext().containsBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME)) {
        defaultErrorChannel = getApplicationContext().getBean(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME, MessageChannel.class);
    }
    if (defaultErrorChannel != null) {
        BridgeHandler errorBridge = new BridgeHandler();
        errorBridge.setOutputChannel(defaultErrorChannel);
        errorChannel.subscribe(errorBridge);
        String errorBridgeHandlerName = getErrorBridgeName(destination);
        ((GenericApplicationContext) getApplicationContext()).registerBean(errorBridgeHandlerName, BridgeHandler.class, () -> errorBridge);
    }
    return errorChannel;
}
Also used : GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) PublishSubscribeChannel(org.springframework.integration.channel.PublishSubscribeChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) AbstractMessageChannel(org.springframework.integration.channel.AbstractMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) DirectChannel(org.springframework.integration.channel.DirectChannel) BridgeHandler(org.springframework.integration.handler.BridgeHandler) AbstractSubscribableChannel(org.springframework.integration.channel.AbstractSubscribableChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Aggregations

DirectChannel (org.springframework.integration.channel.DirectChannel)215 Test (org.junit.Test)182 MessageChannel (org.springframework.messaging.MessageChannel)71 Message (org.springframework.messaging.Message)68 QueueChannel (org.springframework.integration.channel.QueueChannel)63 BeanFactory (org.springframework.beans.factory.BeanFactory)45 GenericMessage (org.springframework.messaging.support.GenericMessage)38 MessageHandler (org.springframework.messaging.MessageHandler)32 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)28 CountDownLatch (java.util.concurrent.CountDownLatch)27 ConfigurableApplicationContext (org.springframework.context.ConfigurableApplicationContext)26 BindingProperties (org.springframework.cloud.stream.config.BindingProperties)25 AbstractReplyProducingMessageHandler (org.springframework.integration.handler.AbstractReplyProducingMessageHandler)23 HashMap (java.util.HashMap)22 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)22 MessagingException (org.springframework.messaging.MessagingException)18 Properties (java.util.Properties)15 AtomicReference (java.util.concurrent.atomic.AtomicReference)15 SubscribableChannel (org.springframework.messaging.SubscribableChannel)15 Expression (org.springframework.expression.Expression)14