Search in sources :

Example 1 with IntegrationMessageHeaderAccessor

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

the class PartitionCapableBinderTests method testPartitionedModuleSpEL.

@Test
public void testPartitionedModuleSpEL() throws Exception {
    B binder = getBinder();
    CP consumerProperties = createConsumerProperties();
    consumerProperties.setConcurrency(2);
    consumerProperties.setInstanceIndex(0);
    consumerProperties.setInstanceCount(3);
    consumerProperties.setPartitioned(true);
    QueueChannel input0 = new QueueChannel();
    input0.setBeanName("test.input0S");
    Binding<MessageChannel> input0Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input0, consumerProperties);
    consumerProperties.setInstanceIndex(1);
    QueueChannel input1 = new QueueChannel();
    input1.setBeanName("test.input1S");
    Binding<MessageChannel> input1Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input1, consumerProperties);
    consumerProperties.setInstanceIndex(2);
    QueueChannel input2 = new QueueChannel();
    input2.setBeanName("test.input2S");
    Binding<MessageChannel> input2Binding = binder.bindConsumer(String.format("part%s0", getDestinationNameDelimiter()), "testPartitionedModuleSpEL", input2, consumerProperties);
    PP producerProperties = createProducerProperties();
    producerProperties.setPartitionKeyExpression(spelExpressionParser.parseExpression("payload"));
    producerProperties.setPartitionSelectorExpression(spelExpressionParser.parseExpression("hashCode()"));
    producerProperties.setPartitionCount(3);
    DirectChannel output = createBindableChannel("output", createProducerBindingProperties(producerProperties));
    output.setBeanName("test.output");
    Binding<MessageChannel> outputBinding = binder.bindProducer(String.format("part%s0", getDestinationNameDelimiter()), output, producerProperties);
    try {
        Object endpoint = extractEndpoint(outputBinding);
        checkRkExpressionForPartitionedModuleSpEL(endpoint);
    } catch (UnsupportedOperationException ignored) {
    }
    Message<String> message2 = MessageBuilder.withPayload("2").setHeader(IntegrationMessageHeaderAccessor.CORRELATION_ID, "foo").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, 42).setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_SIZE, 43).build();
    output.send(message2);
    output.send(MessageBuilder.withPayload("1").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    output.send(MessageBuilder.withPayload("0").setHeader(MessageHeaders.CONTENT_TYPE, MimeTypeUtils.TEXT_PLAIN).build());
    Message<?> receive0 = receive(input0);
    assertThat(receive0).isNotNull();
    Message<?> receive1 = receive(input1);
    assertThat(receive1).isNotNull();
    Message<?> receive2 = receive(input2);
    assertThat(receive2).isNotNull();
    Condition<Message<?>> correlationHeadersForPayload2 = new Condition<Message<?>>() {

        @Override
        public boolean matches(Message<?> value) {
            IntegrationMessageHeaderAccessor accessor = new IntegrationMessageHeaderAccessor(value);
            return "foo".equals(accessor.getCorrelationId()) && 42 == accessor.getSequenceNumber() && 43 == accessor.getSequenceSize();
        }
    };
    if (usesExplicitRouting()) {
        assertThat(receive0.getPayload()).isEqualTo("0".getBytes());
        assertThat(receive1.getPayload()).isEqualTo("1".getBytes());
        assertThat(receive2.getPayload()).isEqualTo("2".getBytes());
        assertThat(receive2).has(correlationHeadersForPayload2);
    } else {
        List<Message<?>> receivedMessages = Arrays.asList(receive0, receive1, receive2);
        assertThat(receivedMessages).extracting("payload").containsExactlyInAnyOrder("0".getBytes(), "1".getBytes(), "2".getBytes());
        Condition<Message<?>> payloadIs2 = new Condition<Message<?>>() {

            @Override
            public boolean matches(Message<?> value) {
                return value.getPayload().equals("2".getBytes());
            }
        };
        assertThat(receivedMessages).filteredOn(payloadIs2).areExactly(1, correlationHeadersForPayload2);
    }
    input0Binding.unbind();
    input1Binding.unbind();
    input2Binding.unbind();
    outputBinding.unbind();
}
Also used : Condition(org.assertj.core.api.Condition) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) DirectChannel(org.springframework.integration.channel.DirectChannel) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageChannel(org.springframework.messaging.MessageChannel) Test(org.junit.Test)

Example 2 with IntegrationMessageHeaderAccessor

use of org.springframework.integration.IntegrationMessageHeaderAccessor in project tutorials by eugenp.

the class ActivatorImpl method handleMessage.

@Override
public void handleMessage(Message<File> input) {
    File filePayload = input.getPayload();
    IntegrationMessageHeaderAccessor accessor = new IntegrationMessageHeaderAccessor(input);
    Logger.getAnonymousLogger().info("The file size " + filePayload.length());
    Logger.getAnonymousLogger().info("The time of the message " + accessor.getTimestamp());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) File(java.io.File)

Example 3 with IntegrationMessageHeaderAccessor

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

the class HeaderAnnotationTransformerTests method headerAnnotationWithPrefixedHeader.

// INT-1082
@Test
public void headerAnnotationWithPrefixedHeader() {
    Object target = new TestTransformer();
    MethodInvokingTransformer transformer = new MethodInvokingTransformer(target, "appendCorrelationId");
    MessageTransformingHandler handler = new MessageTransformingHandler(transformer);
    handler.setBeanFactory(mock(BeanFactory.class));
    handler.afterPropertiesSet();
    QueueChannel outputChannel = new QueueChannel();
    handler.setOutputChannel(outputChannel);
    handler.handleMessage(MessageBuilder.withPayload("test").setCorrelationId("abc").build());
    Message<?> result = outputChannel.receive(0);
    assertNotNull(result);
    assertEquals("testabc", result.getPayload());
    assertEquals("abc", new IntegrationMessageHeaderAccessor(result).getCorrelationId());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageTransformingHandler(org.springframework.integration.transformer.MessageTransformingHandler) QueueChannel(org.springframework.integration.channel.QueueChannel) BeanFactory(org.springframework.beans.factory.BeanFactory) MethodInvokingTransformer(org.springframework.integration.transformer.MethodInvokingTransformer) Test(org.junit.Test)

Example 4 with IntegrationMessageHeaderAccessor

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

the class MessageSequenceComparator method compare.

@Override
public int compare(Message<?> o1, Message<?> o2) {
    int sequenceNumber1 = new IntegrationMessageHeaderAccessor(o1).getSequenceNumber();
    int sequenceNumber2 = new IntegrationMessageHeaderAccessor(o2).getSequenceNumber();
    return Integer.compare(sequenceNumber1, sequenceNumber2);
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor)

Example 5 with IntegrationMessageHeaderAccessor

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

the class SequenceSizeReleaseStrategy method canRelease.

@Override
public boolean canRelease(MessageGroup messageGroup) {
    boolean canRelease = false;
    int size = messageGroup.size();
    if (this.releasePartialSequences && size > 0) {
        if (logger.isTraceEnabled()) {
            logger.trace("Considering partial release of group [" + messageGroup + "]");
        }
        Collection<Message<?>> messages = messageGroup.getMessages();
        Message<?> minMessage = Collections.min(messages, this.comparator);
        int nextSequenceNumber = new IntegrationMessageHeaderAccessor(minMessage).getSequenceNumber();
        int lastReleasedMessageSequence = messageGroup.getLastReleasedMessageSequenceNumber();
        if (nextSequenceNumber - lastReleasedMessageSequence == 1) {
            canRelease = true;
        }
    } else {
        if (size == 0) {
            canRelease = true;
        } else {
            int sequenceSize = messageGroup.getSequenceSize();
            // If there is no sequence then it must be incomplete....
            if (sequenceSize == size) {
                canRelease = true;
            }
        }
    }
    return canRelease;
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message)

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