Search in sources :

Example 21 with IntegrationMessageHeaderAccessor

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

the class MethodInvokingSplitterTests method headerForMessageReturnValues.

@Test
public void headerForMessageReturnValues() throws Exception {
    GenericMessage<String> message = new GenericMessage<String>("foo.bar");
    MethodInvokingSplitter splitter = this.getSplitter("messageToMessageList");
    QueueChannel replyChannel = new QueueChannel();
    splitter.setOutputChannel(replyChannel);
    splitter.handleMessage(message);
    List<Message<?>> replies = replyChannel.clear();
    Message<?> reply1 = replies.get(0);
    assertNotNull(reply1);
    assertEquals(2, new IntegrationMessageHeaderAccessor(reply1).getSequenceSize());
    assertEquals(1, new IntegrationMessageHeaderAccessor(reply1).getSequenceNumber());
    assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply1).getCorrelationId());
    Message<?> reply2 = replies.get(1);
    assertNotNull(reply2);
    assertEquals(2, new IntegrationMessageHeaderAccessor(reply2).getSequenceSize());
    assertEquals(2, new IntegrationMessageHeaderAccessor(reply2).getSequenceNumber());
    assertEquals(message.getHeaders().getId(), new IntegrationMessageHeaderAccessor(reply2).getCorrelationId());
}
Also used : GenericMessage(org.springframework.messaging.support.GenericMessage) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) GenericMessage(org.springframework.messaging.support.GenericMessage) Test(org.junit.Test)

Example 22 with IntegrationMessageHeaderAccessor

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

the class JmsSendingMessageHandler method handleMessageInternal.

@Override
protected void handleMessageInternal(final Message<?> message) throws Exception {
    if (message == null) {
        throw new IllegalArgumentException("message must not be null");
    }
    Object destination = this.determineDestination(message);
    Object objectToSend = (this.extractPayload) ? message.getPayload() : message;
    MessagePostProcessor messagePostProcessor = new HeaderMappingMessagePostProcessor(message, this.headerMapper);
    try {
        DynamicJmsTemplateProperties.setPriority(new IntegrationMessageHeaderAccessor(message).getPriority());
        this.send(destination, objectToSend, messagePostProcessor);
    } finally {
        DynamicJmsTemplateProperties.clearPriority();
    }
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessagePostProcessor(org.springframework.jms.core.MessagePostProcessor)

Example 23 with IntegrationMessageHeaderAccessor

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

the class MessageBuilderTests method testSequenceNumberAsLong.

@Test
public void testSequenceNumberAsLong() {
    Message<String> message = MessageBuilder.withPayload("foo").setHeader(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER, Long.MAX_VALUE).build();
    @SuppressWarnings("unused") Integer sequenceNumber = new IntegrationMessageHeaderAccessor(message).getSequenceNumber();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Test(org.junit.Test)

Example 24 with IntegrationMessageHeaderAccessor

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

the class TcpMessageMapperTests method testToMessageSequenceNewWithCustomHeader.

@Test
public void testToMessageSequenceNewWithCustomHeader() throws Exception {
    TcpMessageMapper mapper = new TcpMessageMapper() {

        @Override
        protected Map<String, ?> supplyCustomHeaders(TcpConnection connection) {
            return Collections.singletonMap("foo", "bar");
        }
    };
    mapper.setApplySequence(true);
    Socket socket = SocketFactory.getDefault().createSocket();
    TcpConnection connection = new TcpConnectionSupport(socket, false, false, null, null) {

        @Override
        public void run() {
        }

        @Override
        public void send(Message<?> message) throws Exception {
        }

        @Override
        public boolean isOpen() {
            return false;
        }

        @Override
        public int getPort() {
            return 1234;
        }

        @Override
        public Object getPayload() throws Exception {
            return TEST_PAYLOAD.getBytes();
        }

        @Override
        public String getHostName() {
            return "MyHost";
        }

        @Override
        public String getHostAddress() {
            return "1.1.1.1";
        }

        @Override
        public String getConnectionId() {
            return "anId";
        }

        @Override
        public Object getDeserializerStateKey() {
            return null;
        }

        @Override
        public SSLSession getSslSession() {
            return null;
        }
    };
    Message<?> message = mapper.toMessage(connection);
    assertEquals(TEST_PAYLOAD, new String((byte[]) message.getPayload()));
    assertEquals("MyHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    IntegrationMessageHeaderAccessor headerAccessor = new IntegrationMessageHeaderAccessor(message);
    assertEquals(1, headerAccessor.getSequenceNumber());
    assertEquals(message.getHeaders().get(IpHeaders.CONNECTION_ID), headerAccessor.getCorrelationId());
    message = mapper.toMessage(connection);
    headerAccessor = new IntegrationMessageHeaderAccessor(message);
    assertEquals(TEST_PAYLOAD, new String((byte[]) message.getPayload()));
    assertEquals("MyHost", message.getHeaders().get(IpHeaders.HOSTNAME));
    assertEquals("1.1.1.1", message.getHeaders().get(IpHeaders.IP_ADDRESS));
    assertEquals(1234, message.getHeaders().get(IpHeaders.REMOTE_PORT));
    assertEquals(2, headerAccessor.getSequenceNumber());
    assertEquals(message.getHeaders().get(IpHeaders.CONNECTION_ID), headerAccessor.getCorrelationId());
    assertNotNull(message.getHeaders().get("foo"));
    assertEquals("bar", message.getHeaders().get("foo"));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) Matchers.containsString(org.hamcrest.Matchers.containsString) Socket(java.net.Socket) Test(org.junit.Test)

Example 25 with IntegrationMessageHeaderAccessor

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

the class StreamingInboundTests method testAllDataMaxFetch.

@SuppressWarnings("unchecked")
@Test
public void testAllDataMaxFetch() throws Exception {
    StringSessionFactory sessionFactory = new StringSessionFactory();
    Streamer streamer = new Streamer(new StringRemoteFileTemplate(sessionFactory), null);
    streamer.setBeanFactory(mock(BeanFactory.class));
    streamer.setRemoteDirectory("/foo");
    streamer.setMaxFetchSize(1);
    streamer.setFilter(new AcceptOnceFileListFilter<>());
    streamer.afterPropertiesSet();
    Message<byte[]> received = (Message<byte[]>) this.transformer.transform(streamer.receive());
    assertEquals("foo\nbar", new String(received.getPayload()));
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("foo", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    // close after list, transform
    verify(StaticMessageHeaderAccessor.getCloseableResource(received), times(2)).close();
    received = (Message<byte[]>) this.transformer.transform(streamer.receive());
    assertEquals("baz\nqux", new String(received.getPayload()));
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("bar", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    // close after list, transform
    verify(new IntegrationMessageHeaderAccessor(received).getCloseableResource(), times(4)).close();
    verify(sessionFactory.getSession(), times(2)).list("/foo");
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) BeanFactory(org.springframework.beans.factory.BeanFactory) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) 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