Search in sources :

Example 6 with FluxMessageChannel

use of org.springframework.integration.channel.FluxMessageChannel in project spring-integration by spring-projects.

the class FileSplitterTests method testFileSplitterReactive.

@Test
public void testFileSplitterReactive() {
    FluxMessageChannel outputChannel = new FluxMessageChannel();
    FileSplitter splitter = new FileSplitter(true, true);
    splitter.setApplySequence(true);
    splitter.setOutputChannel(outputChannel);
    splitter.handleMessage(new GenericMessage<>(file));
    StepVerifier.create(outputChannel).assertNext(m -> {
        assertThat(m, hasHeaderKey(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE));
        assertThat(m, hasHeader(FileHeaders.MARKER, "START"));
        assertThat(m, hasPayload(instanceOf(FileSplitter.FileMarker.class)));
        FileMarker fileMarker = (FileSplitter.FileMarker) m.getPayload();
        assertEquals(FileMarker.Mark.START, fileMarker.getMark());
        assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
    }).expectNextCount(2).assertNext(m -> {
        assertThat(m, hasHeader(FileHeaders.MARKER, "END"));
        assertThat(m, hasPayload(instanceOf(FileSplitter.FileMarker.class)));
        FileMarker fileMarker = (FileSplitter.FileMarker) m.getPayload();
        assertEquals(FileMarker.Mark.END, fileMarker.getMark());
        assertEquals(file.getAbsolutePath(), fileMarker.getFilePath());
        assertEquals(2, fileMarker.getLineCount());
    }).then(() -> ((Subscriber<?>) TestUtils.getPropertyValue(outputChannel, "subscribers", List.class).get(0)).onComplete()).verifyComplete();
}
Also used : Subscriber(org.reactivestreams.Subscriber) FileMarker(org.springframework.integration.file.splitter.FileSplitter.FileMarker) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Example 7 with FluxMessageChannel

use of org.springframework.integration.channel.FluxMessageChannel in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerViaConsumerEndpointFactoryBean.

@Test
public void testReactiveStreamsConsumerViaConsumerEndpointFactoryBean() throws Exception {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(3);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    ConsumerEndpointFactoryBean endpointFactoryBean = new ConsumerEndpointFactoryBean();
    endpointFactoryBean.setBeanFactory(mock(ConfigurableBeanFactory.class));
    endpointFactoryBean.setInputChannel(testChannel);
    endpointFactoryBean.setHandler(messageHandler);
    endpointFactoryBean.setBeanName("reactiveConsumer");
    endpointFactoryBean.afterPropertiesSet();
    endpointFactoryBean.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    endpointFactoryBean.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    endpointFactoryBean.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result.size(), equalTo(3));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Test(org.junit.Test)

Example 8 with FluxMessageChannel

use of org.springframework.integration.channel.FluxMessageChannel in project spring-integration by spring-projects.

the class ReactiveStreamsConsumerTests method testReactiveStreamsConsumerFluxMessageChannel.

@Test
public void testReactiveStreamsConsumerFluxMessageChannel() throws InterruptedException {
    FluxMessageChannel testChannel = new FluxMessageChannel();
    List<Message<?>> result = new LinkedList<>();
    CountDownLatch stopLatch = new CountDownLatch(2);
    MessageHandler messageHandler = m -> {
        result.add(m);
        stopLatch.countDown();
    };
    MessageHandler testSubscriber = new MethodInvokingMessageHandler(messageHandler, (String) null);
    ReactiveStreamsConsumer reactiveConsumer = new ReactiveStreamsConsumer(testChannel, testSubscriber);
    reactiveConsumer.setBeanFactory(mock(BeanFactory.class));
    reactiveConsumer.afterPropertiesSet();
    reactiveConsumer.start();
    Message<?> testMessage = new GenericMessage<>("test");
    testChannel.send(testMessage);
    reactiveConsumer.stop();
    try {
        testChannel.send(testMessage);
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageDeliveryException.class));
        assertThat(e.getCause(), instanceOf(IllegalStateException.class));
        assertThat(e.getMessage(), containsString("doesn't have subscribers to accept messages"));
    }
    reactiveConsumer.start();
    Message<?> testMessage2 = new GenericMessage<>("test2");
    testChannel.send(testMessage2);
    assertTrue(stopLatch.await(10, TimeUnit.SECONDS));
    assertThat(result, Matchers.<Message<?>>contains(testMessage, testMessage2));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Assert.assertSame(org.junit.Assert.assertSame) Assert.assertThat(org.junit.Assert.assertThat) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert.fail(org.junit.Assert.fail) Message(org.springframework.messaging.Message) LinkedList(java.util.LinkedList) Subscriber(org.reactivestreams.Subscriber) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) BlockingQueue(java.util.concurrent.BlockingQueue) Test(org.junit.Test) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ConsumerEndpointFactoryBean(org.springframework.integration.config.ConsumerEndpointFactoryBean) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) BDDMockito.willAnswer(org.mockito.BDDMockito.willAnswer) Mockito.verify(org.mockito.Mockito.verify) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) Mockito(org.mockito.Mockito) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) Mockito.never(org.mockito.Mockito.never) List(java.util.List) MessageHandler(org.springframework.messaging.MessageHandler) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Subscription(org.reactivestreams.Subscription) BeanFactory(org.springframework.beans.factory.BeanFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) DirectChannel(org.springframework.integration.channel.DirectChannel) Matchers.containsString(org.hamcrest.Matchers.containsString) Mockito.mock(org.mockito.Mockito.mock) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) ReactiveStreamsConsumer(org.springframework.integration.endpoint.ReactiveStreamsConsumer) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MethodInvokingMessageHandler(org.springframework.integration.handler.MethodInvokingMessageHandler) MessageHandler(org.springframework.messaging.MessageHandler) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedList(java.util.LinkedList) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) GenericMessage(org.springframework.messaging.support.GenericMessage) BeanFactory(org.springframework.beans.factory.BeanFactory) ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) Test(org.junit.Test)

Example 9 with FluxMessageChannel

use of org.springframework.integration.channel.FluxMessageChannel in project spring-integration by spring-projects.

the class IntegrationFlowDefinition method toReactivePublisher.

/**
 * Represent an Integration Flow as a Reactive Streams {@link Publisher} bean.
 * @param <T> the expected {@code payload} type
 * @return the Reactive Streams {@link Publisher}
 */
@SuppressWarnings("unchecked")
public <T> Publisher<Message<T>> toReactivePublisher() {
    MessageChannel channelForPublisher = this.currentMessageChannel;
    Publisher<Message<T>> publisher;
    if (channelForPublisher instanceof Publisher) {
        publisher = (Publisher<Message<T>>) channelForPublisher;
    } else {
        if (channelForPublisher != null) {
            publisher = MessageChannelReactiveUtils.toPublisher(channelForPublisher);
        } else {
            MessageChannel reactiveChannel = new FluxMessageChannel();
            publisher = (Publisher<Message<T>>) reactiveChannel;
            channel(reactiveChannel);
        }
    }
    get();
    return new PublisherIntegrationFlow<>(this.integrationComponents, publisher);
}
Also used : FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) MessageChannel(org.springframework.messaging.MessageChannel) Message(org.springframework.messaging.Message) Publisher(org.reactivestreams.Publisher) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel)

Aggregations

FluxMessageChannel (org.springframework.integration.channel.FluxMessageChannel)9 Test (org.junit.Test)7 Subscriber (org.reactivestreams.Subscriber)7 GenericMessage (org.springframework.messaging.support.GenericMessage)5 Message (org.springframework.messaging.Message)4 List (java.util.List)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Matchers.instanceOf (org.hamcrest.Matchers.instanceOf)3 Assert.assertThat (org.junit.Assert.assertThat)3 QueueChannel (org.springframework.integration.channel.QueueChannel)3 LinkedList (java.util.LinkedList)2 BlockingQueue (java.util.concurrent.BlockingQueue)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 TimeUnit (java.util.concurrent.TimeUnit)2 Matchers (org.hamcrest.Matchers)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Assert.assertSame (org.junit.Assert.assertSame)2 Assert.assertTrue (org.junit.Assert.assertTrue)2 Assert.fail (org.junit.Assert.fail)2