Search in sources :

Example 86 with DirectChannel

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

the class BindingServiceConfiguration method errorBridgeChannel.

@Bean
@ConditionalOnProperty("spring.cloud.stream.bindings." + ERROR_KEY_NAME + ".destination")
public MessageChannel errorBridgeChannel(@Qualifier(IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME) PublishSubscribeChannel errorChannel) {
    SubscribableChannel errorBridgeChannel = new DirectChannel();
    BridgeHandler handler = new BridgeHandler();
    handler.setOutputChannel(errorBridgeChannel);
    errorChannel.subscribe(handler);
    return errorBridgeChannel;
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) BridgeHandler(org.springframework.integration.handler.BridgeHandler) SubscribableChannel(org.springframework.messaging.SubscribableChannel) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean) ConditionalOnProperty(org.springframework.boot.autoconfigure.condition.ConditionalOnProperty)

Example 87 with DirectChannel

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

the class MessageChannelToInputFluxParameterAdapterTests method testWrapperFluxSupportsMultipleSubscriptions.

@Test
public void testWrapperFluxSupportsMultipleSubscriptions() throws Exception {
    List<String> results = Collections.synchronizedList(new ArrayList<>());
    CountDownLatch latch = new CountDownLatch(4);
    final MessageChannelToInputFluxParameterAdapter messageChannelToInputFluxParameterAdapter = new MessageChannelToInputFluxParameterAdapter(new CompositeMessageConverter(Collections.singleton(new MappingJackson2MessageConverter())));
    final Method processMethod = ReflectionUtils.findMethod(MessageChannelToInputFluxParameterAdapterTests.class, "process", Flux.class);
    final DirectChannel adaptedChannel = new DirectChannel();
    @SuppressWarnings("unchecked") final Flux<Message<?>> adapterFlux = (Flux<Message<?>>) messageChannelToInputFluxParameterAdapter.adapt(adaptedChannel, new MethodParameter(processMethod, 0));
    String uuid1 = UUID.randomUUID().toString();
    String uuid2 = UUID.randomUUID().toString();
    adapterFlux.map(m -> m.getPayload() + uuid1).subscribe(s -> {
        results.add(s);
        latch.countDown();
    });
    adapterFlux.map(m -> m.getPayload() + uuid2).subscribe(s -> {
        results.add(s);
        latch.countDown();
    });
    adaptedChannel.send(MessageBuilder.withPayload("A").build());
    adaptedChannel.send(MessageBuilder.withPayload("B").build());
    assertThat(latch.await(5000, TimeUnit.MILLISECONDS)).isTrue();
    assertThat(results).containsExactlyInAnyOrder("A" + uuid1, "B" + uuid1, "A" + uuid2, "B" + uuid2);
}
Also used : Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Test(org.junit.Test) UUID(java.util.UUID) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) CountDownLatch(java.util.concurrent.CountDownLatch) Flux(reactor.core.publisher.Flux) List(java.util.List) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) ReflectionUtils(org.springframework.util.ReflectionUtils) MethodParameter(org.springframework.core.MethodParameter) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) Method(java.lang.reflect.Method) Collections(java.util.Collections) MessageBuilder(org.springframework.messaging.support.MessageBuilder) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) Flux(reactor.core.publisher.Flux) Method(java.lang.reflect.Method) CountDownLatch(java.util.concurrent.CountDownLatch) CompositeMessageConverter(org.springframework.messaging.converter.CompositeMessageConverter) MappingJackson2MessageConverter(org.springframework.messaging.converter.MappingJackson2MessageConverter) MethodParameter(org.springframework.core.MethodParameter) Test(org.junit.Test)

Example 88 with DirectChannel

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

the class AggregateApplicationUtils method prepareSharedBindingTargetRegistry.

static void prepareSharedBindingTargetRegistry(SharedBindingTargetRegistry sharedBindingTargetRegistry, LinkedHashMap<Class<?>, String> appsWithNamespace) {
    int i = 0;
    SubscribableChannel sharedChannel = null;
    for (Entry<Class<?>, String> appEntry : appsWithNamespace.entrySet()) {
        String namespace = appEntry.getValue();
        if (i > 0) {
            sharedBindingTargetRegistry.register(namespace + "." + INPUT_BINDING_NAME, sharedChannel);
        }
        sharedChannel = new DirectChannel();
        if (i < appsWithNamespace.size() - 1) {
            sharedBindingTargetRegistry.register(namespace + "." + OUTPUT_BINDING_NAME, sharedChannel);
        }
        i++;
    }
}
Also used : DirectChannel(org.springframework.integration.channel.DirectChannel) SubscribableChannel(org.springframework.messaging.SubscribableChannel)

Example 89 with DirectChannel

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

the class PollableConsumerTests method testRequeueFromErrorFlow.

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

        @Override
        public Message<?> preSend(Message<?> message, MessageChannel channel) {
            return MessageBuilder.fromMessage(message).setHeader(IntegrationMessageHeaderAccessor.ACKNOWLEDGMENT_CALLBACK, callback).build();
        }
    });
    ExtendedConsumerProperties<Object> properties = new ExtendedConsumerProperties<>(null);
    properties.setMaxAttempts(1);
    binder.bindPollableConsumer("foo", "bar", pollableSource, properties);
    SubscribableChannel errorChannel = new DirectChannel();
    errorChannel.subscribe(msg -> {
        throw new RequeueCurrentMessageException((Throwable) msg.getPayload());
    });
    pollableSource.setErrorChannel(errorChannel);
    try {
        pollableSource.poll(received -> {
            throw new RuntimeException("test requeue from error flow");
        });
    } catch (Exception e) {
    // no op
    }
    verify(callback).acknowledge(Status.REQUEUE);
}
Also used : Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) AcknowledgmentCallback(org.springframework.integration.acks.AcknowledgmentCallback) MessageChannel(org.springframework.messaging.MessageChannel) 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 90 with DirectChannel

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

the class BindingServiceTests method testDefaultGroup.

@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testDefaultGroup() throws Exception {
    BindingServiceProperties properties = new BindingServiceProperties();
    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);
    BindingService service = new BindingService(properties, binderFactory, new ObjectMapper());
    MessageChannel inputChannel = new DirectChannel();
    Binding<MessageChannel> mockBinding = Mockito.mock(Binding.class);
    when(binder.bindConsumer(eq("foo"), isNull(), 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"), isNull(), 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) ConsumerProperties(org.springframework.cloud.stream.binder.ConsumerProperties) DefaultBinderFactory(org.springframework.cloud.stream.binder.DefaultBinderFactory) 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)

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