Search in sources :

Example 36 with IntegrationMessageHeaderAccessor

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

the class TestAnnotatedEndpointWithCustomizedAggregator method aggregatingMethod.

@Aggregator(inputChannel = "inputChannel", outputChannel = "outputChannel", discardChannel = "discardChannel", sendPartialResultsOnExpiry = "true", sendTimeout = "98765432")
public Message<?> aggregatingMethod(List<Message<?>> messages) {
    List<Message<?>> sortableList = new ArrayList<>(messages);
    Collections.sort(sortableList, new MessageSequenceComparator());
    StringBuffer buffer = new StringBuffer();
    Object correlationId = null;
    for (Message<?> message : sortableList) {
        buffer.append(message.getPayload().toString());
        if (null == correlationId) {
            correlationId = new IntegrationMessageHeaderAccessor(message).getCorrelationId();
        }
    }
    Message<?> returnedMessage = new GenericMessage<>(buffer.toString());
    aggregatedMessages.put(correlationId, returnedMessage);
    return returnedMessage;
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageSequenceComparator(org.springframework.integration.aggregator.MessageSequenceComparator) ArrayList(java.util.ArrayList) Aggregator(org.springframework.integration.annotation.Aggregator)

Example 37 with IntegrationMessageHeaderAccessor

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

the class TestAggregatorBean method createSingleMessageFromGroup.

@Aggregator
public Message<?> createSingleMessageFromGroup(List<Message<?>> messages) {
    List<Message<?>> sortableList = new ArrayList<>(messages);
    Collections.sort(sortableList, new MessageSequenceComparator());
    StringBuffer buffer = new StringBuffer();
    Object correlationId = null;
    for (Message<?> message : sortableList) {
        buffer.append(message.getPayload().toString());
        if (null == correlationId) {
            correlationId = new IntegrationMessageHeaderAccessor(message).getCorrelationId();
        }
    }
    Message<?> returnedMessage = new GenericMessage<>(buffer.toString());
    if (correlationId != null) {
        this.aggregatedMessages.put(correlationId, returnedMessage);
    }
    return returnedMessage;
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageSequenceComparator(org.springframework.integration.aggregator.MessageSequenceComparator) ArrayList(java.util.ArrayList) Aggregator(org.springframework.integration.annotation.Aggregator)

Example 38 with IntegrationMessageHeaderAccessor

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

the class SplitterParserTests method splitterParserTestApplySequenceFalse.

@Test
public void splitterParserTestApplySequenceFalse() {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("splitterParserTests.xml", this.getClass());
    context.start();
    DirectChannel inputChannel = context.getBean("noSequenceInput", DirectChannel.class);
    PollableChannel output = (PollableChannel) context.getBean("output");
    inputChannel.send(MessageBuilder.withPayload(Collections.emptyList()).build());
    Message<?> message = output.receive(1000);
    assertThat(new IntegrationMessageHeaderAccessor(message).getSequenceNumber(), is(0));
    assertThat(new IntegrationMessageHeaderAccessor(message).getSequenceSize(), is(0));
    context.close();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) DirectChannel(org.springframework.integration.channel.DirectChannel) PollableChannel(org.springframework.messaging.PollableChannel) Test(org.junit.Test)

Example 39 with IntegrationMessageHeaderAccessor

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

the class DefaultSplitterTests method splitFlux.

@Test
public void splitFlux() {
    Message<?> message = new GenericMessage<>(Flux.generate(() -> 0, (state, sink) -> {
        if (state == 10) {
            sink.complete();
        } else {
            sink.next(state);
        }
        return ++state;
    }));
    QueueChannel outputChannel = new QueueChannel();
    DefaultMessageSplitter splitter = new DefaultMessageSplitter();
    splitter.setOutputChannel(outputChannel);
    splitter.handleMessage(message);
    for (int i = 0; i < 10; i++) {
        Message<?> reply = outputChannel.receive(0);
        assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply).getCorrelationId());
    }
    assertNull(outputChannel.receive(10));
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) FluxMessageChannel(org.springframework.integration.channel.FluxMessageChannel) TestUtils(org.springframework.integration.test.util.TestUtils) Assert.assertThat(org.junit.Assert.assertThat) MessageBuilder(org.springframework.integration.support.MessageBuilder) Flux(reactor.core.publisher.Flux) List(java.util.List) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) Assert.assertNull(org.junit.Assert.assertNull) Stream(java.util.stream.Stream) Matchers.nullValue(org.hamcrest.Matchers.nullValue) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Matchers.is(org.hamcrest.Matchers.is) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) DirectChannel(org.springframework.integration.channel.DirectChannel) Collections(java.util.Collections) Subscriber(org.reactivestreams.Subscriber) Assert.assertEquals(org.junit.Assert.assertEquals) GenericMessage(org.springframework.messaging.support.GenericMessage) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 40 with IntegrationMessageHeaderAccessor

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

the class HeaderFilterTests method testFilterWithinHandler.

@Test
public void testFilterWithinHandler() {
    UUID correlationId = UUID.randomUUID();
    QueueChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("test").setHeader("x", 1).setHeader("y", 2).setHeader("z", 3).setCorrelationId(correlationId).setReplyChannel(replyChannel).setErrorChannelName("testErrorChannel").build();
    HeaderFilter filter = new HeaderFilter("x", "z");
    MessageTransformingHandler handler = new MessageTransformingHandler(filter);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    handler.handleMessage(message);
    Message<?> result = replyChannel.receive(0);
    assertNotNull(result);
    assertNotNull(result.getHeaders().get("y"));
    assertNull(result.getHeaders().get("x"));
    assertNull(result.getHeaders().get("z"));
    assertEquals("testErrorChannel", result.getHeaders().getErrorChannel());
    assertEquals(replyChannel, result.getHeaders().getReplyChannel());
    assertEquals(correlationId, new IntegrationMessageHeaderAccessor(result).getCorrelationId());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) Matchers.containsString(org.hamcrest.Matchers.containsString) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

IntegrationMessageHeaderAccessor (org.springframework.integration.IntegrationMessageHeaderAccessor)68 Test (org.junit.Test)56 QueueChannel (org.springframework.integration.channel.QueueChannel)32 Message (org.springframework.messaging.Message)21 GenericMessage (org.springframework.messaging.support.GenericMessage)19 MessageChannel (org.springframework.messaging.MessageChannel)15 ArrayList (java.util.ArrayList)10 MessagingTemplate (org.springframework.integration.core.MessagingTemplate)9 DirectChannel (org.springframework.integration.channel.DirectChannel)8 ServiceActivatingHandler (org.springframework.integration.handler.ServiceActivatingHandler)6 List (java.util.List)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 MessageSequenceComparator (org.springframework.integration.aggregator.MessageSequenceComparator)4 Aggregator (org.springframework.integration.annotation.Aggregator)4 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 EventDrivenConsumer (org.springframework.integration.endpoint.EventDrivenConsumer)3 MessageGroup (org.springframework.integration.store.MessageGroup)3 SimpleMessageGroup (org.springframework.integration.store.SimpleMessageGroup)3