Search in sources :

Example 46 with IntegrationMessageHeaderAccessor

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

the class ResequencerTests method testBasicResequencing.

@Test
public void testBasicResequencing() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage("123", "ABC", 3, 3, replyChannel);
    Message<?> message2 = createMessage("456", "ABC", 3, 1, replyChannel);
    Message<?> message3 = createMessage("789", "ABC", 3, 2, replyChannel);
    this.resequencer.handleMessage(message1);
    this.resequencer.handleMessage(message3);
    this.resequencer.handleMessage(message2);
    Message<?> reply1 = replyChannel.receive(0);
    Message<?> reply2 = replyChannel.receive(0);
    Message<?> reply3 = replyChannel.receive(0);
    assertNotNull(reply1);
    assertThat(new IntegrationMessageHeaderAccessor(reply1).getSequenceNumber(), is(1));
    assertNotNull(reply2);
    assertThat(new IntegrationMessageHeaderAccessor(reply2).getSequenceNumber(), is(2));
    assertNotNull(reply3);
    assertThat(new IntegrationMessageHeaderAccessor(reply3).getSequenceNumber(), is(3));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 47 with IntegrationMessageHeaderAccessor

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

the class ResequencerTests method testResequencingWithCompleteSequenceRelease.

@Test
public void testResequencingWithCompleteSequenceRelease() throws InterruptedException {
    QueueChannel replyChannel = new QueueChannel();
    Message<?> message1 = createMessage("123", "ABC", 4, 2, replyChannel);
    Message<?> message2 = createMessage("456", "ABC", 4, 1, replyChannel);
    Message<?> message3 = createMessage("789", "ABC", 4, 4, replyChannel);
    Message<?> message4 = createMessage("XYZ", "ABC", 4, 3, replyChannel);
    this.resequencer.handleMessage(message1);
    this.resequencer.handleMessage(message2);
    this.resequencer.handleMessage(message3);
    Message<?> reply1 = replyChannel.receive(0);
    Message<?> reply2 = replyChannel.receive(0);
    Message<?> reply3 = replyChannel.receive(0);
    // no messages should have been received yet
    assertNull(reply1);
    assertNull(reply2);
    assertNull(reply3);
    // after sending the last message, the whole sequence should have been sent
    this.resequencer.handleMessage(message4);
    reply1 = replyChannel.receive(0);
    reply2 = replyChannel.receive(0);
    reply3 = replyChannel.receive(0);
    Message<?> reply4 = replyChannel.receive(0);
    assertNotNull(reply1);
    assertEquals(1, new IntegrationMessageHeaderAccessor(reply1).getSequenceNumber());
    assertNotNull(reply2);
    assertEquals(2, new IntegrationMessageHeaderAccessor(reply2).getSequenceNumber());
    assertNotNull(reply3);
    assertEquals(3, new IntegrationMessageHeaderAccessor(reply3).getSequenceNumber());
    assertNotNull(reply4);
    assertEquals(4, new IntegrationMessageHeaderAccessor(reply4).getSequenceNumber());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 48 with IntegrationMessageHeaderAccessor

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

the class ResequencerIntegrationTests method validateUnboundedResequencerLight.

@Test
public void validateUnboundedResequencerLight() {
    MessageChannel inputChannel = context.getBean("resequencerLightInput", MessageChannel.class);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    EventDrivenConsumer edc = context.getBean("resequencerLight", EventDrivenConsumer.class);
    ResequencingMessageHandler handler = TestUtils.getPropertyValue(edc, "handler", ResequencingMessageHandler.class);
    MessageGroupStore store = TestUtils.getPropertyValue(handler, "messageStore", MessageGroupStore.class);
    Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
    Message<?> message2 = MessageBuilder.withPayload("2").setCorrelationId("A").setSequenceNumber(2).setCorrelationId("A").setSequenceNumber(2).setHeader("foo", "foo").build();
    Message<?> message3 = MessageBuilder.withPayload("3").setCorrelationId("A").setSequenceNumber(3).build();
    Message<?> message4 = MessageBuilder.withPayload("4").setCorrelationId("A").setSequenceNumber(4).setHeader("foo", "foo").build();
    Message<?> message5 = MessageBuilder.withPayload("5").setCorrelationId("A").setSequenceNumber(5).build();
    Message<?> message6 = MessageBuilder.withPayload("6").setCorrelationId("A").setSequenceNumber(6).build();
    inputChannel.send(message3);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message1);
    message1 = outputChannel.receive(0);
    assertNotNull(message1);
    assertEquals(1, new IntegrationMessageHeaderAccessor(message1).getSequenceNumber());
    assertFalse(message1.getHeaders().containsKey("foo"));
    inputChannel.send(message2);
    message2 = outputChannel.receive(0);
    message3 = outputChannel.receive(0);
    assertNotNull(message2);
    assertNotNull(message3);
    assertEquals(2, new IntegrationMessageHeaderAccessor(message2).getSequenceNumber());
    assertTrue(message2.getHeaders().containsKey("foo"));
    assertEquals(3, new IntegrationMessageHeaderAccessor(message3).getSequenceNumber());
    assertFalse(message3.getHeaders().containsKey("foo"));
    inputChannel.send(message5);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message6);
    assertNull(outputChannel.receive(0));
    inputChannel.send(message4);
    message4 = outputChannel.receive(0);
    message5 = outputChannel.receive(0);
    message6 = outputChannel.receive(0);
    assertNotNull(message4);
    assertNotNull(message5);
    assertNotNull(message6);
    assertEquals(4, new IntegrationMessageHeaderAccessor(message4).getSequenceNumber());
    assertTrue(message4.getHeaders().containsKey("foo"));
    assertEquals(5, new IntegrationMessageHeaderAccessor(message5).getSequenceNumber());
    assertFalse(message5.getHeaders().containsKey("foo"));
    assertEquals(6, new IntegrationMessageHeaderAccessor(message6).getSequenceNumber());
    assertFalse(message6.getHeaders().containsKey("foo"));
    assertEquals(0, store.getMessageGroup("A").getMessages().size());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) EventDrivenConsumer(org.springframework.integration.endpoint.EventDrivenConsumer) MessageGroupStore(org.springframework.integration.store.MessageGroupStore) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) ResequencingMessageHandler(org.springframework.integration.aggregator.ResequencingMessageHandler) Test(org.junit.Test)

Example 49 with IntegrationMessageHeaderAccessor

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

the class ResequencerIntegrationTests method testResequencerRefServiceActivator.

@Test
public void testResequencerRefServiceActivator() {
    MessageChannel inputChannel = context.getBean("inputChannel", MessageChannel.class);
    QueueChannel outputChannel = context.getBean("outputChannel", QueueChannel.class);
    Message<?> message1 = MessageBuilder.withPayload("1").setCorrelationId("A").setSequenceNumber(1).build();
    inputChannel.send(message1);
    message1 = outputChannel.receive(0);
    assertNotNull(message1);
    assertEquals(1, new IntegrationMessageHeaderAccessor(message1).getSequenceNumber());
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageChannel(org.springframework.messaging.MessageChannel) QueueChannel(org.springframework.integration.channel.QueueChannel) Test(org.junit.Test)

Example 50 with IntegrationMessageHeaderAccessor

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

the class MessageHistory method write.

@SuppressWarnings("unchecked")
public static <T> Message<T> write(Message<T> message, NamedComponent component, MessageBuilderFactory messageBuilderFactory) {
    Assert.notNull(message, "Message must not be null");
    Assert.notNull(component, "Component must not be null");
    Properties metadata = extractMetadata(component);
    if (!metadata.isEmpty()) {
        MessageHistory previousHistory = message.getHeaders().get(HEADER_NAME, MessageHistory.class);
        List<Properties> components = (previousHistory != null) ? new ArrayList<Properties>(previousHistory) : new ArrayList<Properties>();
        components.add(metadata);
        MessageHistory history = new MessageHistory(components);
        if (message instanceof MutableMessage) {
            message.getHeaders().put(HEADER_NAME, history);
        } else if (message instanceof ErrorMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            Throwable payload = ((ErrorMessage) message).getPayload();
            ErrorMessage errorMessage = new ErrorMessage(payload, headerAccessor.toMessageHeaders());
            message = (Message<T>) errorMessage;
        } else if (message instanceof AdviceMessage) {
            IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
            headerAccessor.setHeader(HEADER_NAME, history);
            message = new AdviceMessage<T>(message.getPayload(), headerAccessor.toMessageHeaders(), ((AdviceMessage<?>) message).getInputMessage());
        } else {
            if (!(message instanceof GenericMessage) && (messageBuilderFactory instanceof DefaultMessageBuilderFactory || messageBuilderFactory instanceof MutableMessageBuilderFactory)) {
                if (logger.isWarnEnabled()) {
                    logger.warn("MessageHistory rebuilds the message and produces the result of the [" + messageBuilderFactory + "], not an instance of the provided type [" + message.getClass() + "]. Consider to supply a custom MessageBuilderFactory " + "to retain custom messages during MessageHistory tracking.");
                }
            }
            message = messageBuilderFactory.fromMessage(message).setHeader(HEADER_NAME, history).build();
        }
    }
    return message;
}
Also used : AdviceMessage(org.springframework.integration.message.AdviceMessage) ErrorMessage(org.springframework.messaging.support.ErrorMessage) MutableMessage(org.springframework.integration.support.MutableMessage) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) AdviceMessage(org.springframework.integration.message.AdviceMessage) Properties(java.util.Properties) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) GenericMessage(org.springframework.messaging.support.GenericMessage) MutableMessage(org.springframework.integration.support.MutableMessage) MutableMessageBuilderFactory(org.springframework.integration.support.MutableMessageBuilderFactory) DefaultMessageBuilderFactory(org.springframework.integration.support.DefaultMessageBuilderFactory) ErrorMessage(org.springframework.messaging.support.ErrorMessage)

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