Search in sources :

Example 26 with IntegrationMessageHeaderAccessor

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

the class StreamingInboundTests method testLineByLine.

@SuppressWarnings("unchecked")
@Test
public void testLineByLine() throws Exception {
    Streamer streamer = new Streamer(new StringRemoteFileTemplate(new StringSessionFactory()), null);
    streamer.setBeanFactory(mock(BeanFactory.class));
    streamer.setRemoteDirectory("/foo");
    streamer.afterPropertiesSet();
    QueueChannel out = new QueueChannel();
    FileSplitter splitter = new FileSplitter();
    splitter.setBeanFactory(mock(BeanFactory.class));
    splitter.setOutputChannel(out);
    splitter.afterPropertiesSet();
    Message<InputStream> receivedStream = streamer.receive();
    splitter.handleMessage(receivedStream);
    Message<byte[]> received = (Message<byte[]>) out.receive(0);
    assertEquals("foo", received.getPayload());
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("foo", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    received = (Message<byte[]>) out.receive(0);
    assertEquals("bar", received.getPayload());
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("foo", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    assertNull(out.receive(0));
    // close by list, splitter
    verify(new IntegrationMessageHeaderAccessor(receivedStream).getCloseableResource(), times(3)).close();
    receivedStream = streamer.receive();
    splitter.handleMessage(receivedStream);
    received = (Message<byte[]>) out.receive(0);
    assertEquals("baz", received.getPayload());
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("bar", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    received = (Message<byte[]>) out.receive(0);
    assertEquals("qux", received.getPayload());
    assertEquals("/foo", received.getHeaders().get(FileHeaders.REMOTE_DIRECTORY));
    assertEquals("bar", received.getHeaders().get(FileHeaders.REMOTE_FILE));
    assertNull(out.receive(0));
    // close by splitter
    verify(new IntegrationMessageHeaderAccessor(receivedStream).getCloseableResource(), times(5)).close();
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) FileSplitter(org.springframework.integration.file.splitter.FileSplitter) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(org.springframework.messaging.Message) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Example 27 with IntegrationMessageHeaderAccessor

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

the class MySqlJdbcMessageStoreTests method testSameMessageToMultipleGroups.

@Test
@Transactional
@Rollback(false)
@Repeat(20)
public void testSameMessageToMultipleGroups() throws Exception {
    final String group1Id = "group1";
    final String group2Id = "group2";
    final Message<String> message = MessageBuilder.withPayload("foo").build();
    final MessageBuilder<String> builder1 = MessageBuilder.fromMessage(message);
    final MessageBuilder<String> builder2 = MessageBuilder.fromMessage(message);
    builder1.setSequenceNumber(1);
    builder2.setSequenceNumber(2);
    final Message<?> message1 = builder1.build();
    final Message<?> message2 = builder2.build();
    messageStore.addMessageToGroup(group1Id, message1);
    messageStore.addMessageToGroup(group2Id, message2);
    final Message<?> messageFromGroup1 = messageStore.pollMessageFromGroup(group1Id);
    final Message<?> messageFromGroup2 = messageStore.pollMessageFromGroup(group2Id);
    assertNotNull(messageFromGroup1);
    assertNotNull(messageFromGroup2);
    LOG.info("messageFromGroup1: " + messageFromGroup1.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromGroup1).getSequenceNumber());
    LOG.info("messageFromGroup2: " + messageFromGroup2.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromGroup2).getSequenceNumber());
    assertEquals(Integer.valueOf(1), messageFromGroup1.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
    assertEquals(Integer.valueOf(2), messageFromGroup2.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 28 with IntegrationMessageHeaderAccessor

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

the class MySqlJdbcMessageStoreTests method testSameMessageAndGroupToMultipleRegions.

@Test
@Transactional
@Rollback(false)
@Repeat(20)
public void testSameMessageAndGroupToMultipleRegions() throws Exception {
    final String groupId = "myGroup";
    final String region1 = "region1";
    final String region2 = "region2";
    final JdbcMessageStore messageStore1 = new JdbcMessageStore(dataSource);
    messageStore1.setRegion(region1);
    final JdbcMessageStore messageStore2 = new JdbcMessageStore(dataSource);
    messageStore1.setRegion(region2);
    final Message<String> message = MessageBuilder.withPayload("foo").build();
    final MessageBuilder<String> builder1 = MessageBuilder.fromMessage(message);
    final MessageBuilder<String> builder2 = MessageBuilder.fromMessage(message);
    builder1.setSequenceNumber(1);
    builder2.setSequenceNumber(2);
    final Message<?> message1 = builder1.build();
    final Message<?> message2 = builder2.build();
    messageStore1.addMessageToGroup(groupId, message1);
    messageStore2.addMessageToGroup(groupId, message2);
    final Message<?> messageFromRegion1 = messageStore1.pollMessageFromGroup(groupId);
    final Message<?> messageFromRegion2 = messageStore2.pollMessageFromGroup(groupId);
    assertNotNull(messageFromRegion1);
    assertNotNull(messageFromRegion2);
    LOG.info("messageFromRegion1: " + messageFromRegion1.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromRegion1).getSequenceNumber());
    LOG.info("messageFromRegion2: " + messageFromRegion2.getHeaders().getId() + "; Sequence #: " + new IntegrationMessageHeaderAccessor(messageFromRegion2).getSequenceNumber());
    assertEquals(Integer.valueOf(1), messageFromRegion1.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
    assertEquals(Integer.valueOf(2), messageFromRegion2.getHeaders().get(IntegrationMessageHeaderAccessor.SEQUENCE_NUMBER));
}
Also used : IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) JdbcMessageStore(org.springframework.integration.jdbc.store.JdbcMessageStore) Test(org.junit.Test) Repeat(org.springframework.test.annotation.Repeat) Rollback(org.springframework.test.annotation.Rollback) Transactional(org.springframework.transaction.annotation.Transactional)

Example 29 with IntegrationMessageHeaderAccessor

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

the class JmsOutboundGateway method sendAndReceiveWithContainer.

private Object sendAndReceiveWithContainer(Message<?> requestMessage) throws JMSException {
    // NOSONAR - closed in ConnectionFactoryUtils.
    Connection connection = this.createConnection();
    Session session = null;
    Destination replyTo = this.replyContainer.getReplyDestination();
    try {
        session = this.createSession(connection);
        // convert to JMS Message
        Object objectToSend = requestMessage;
        if (this.extractRequestPayload) {
            objectToSend = requestMessage.getPayload();
        }
        javax.jms.Message jmsRequest = this.messageConverter.toMessage(objectToSend, session);
        // map headers
        this.headerMapper.fromHeaders(requestMessage.getHeaders(), jmsRequest);
        jmsRequest.setJMSReplyTo(replyTo);
        connection.start();
        if (logger.isDebugEnabled()) {
            logger.debug("ReplyTo: " + replyTo);
        }
        Integer priority = new IntegrationMessageHeaderAccessor(requestMessage).getPriority();
        if (priority == null) {
            priority = this.priority;
        }
        Destination requestDestination = this.determineRequestDestination(requestMessage, session);
        Object reply = null;
        if (this.correlationKey == null) {
            /*
				 * Remove any existing correlation id that was mapped from the inbound message
				 * (it will be restored in the reply by normal ARPMH header processing).
				 */
            jmsRequest.setJMSCorrelationID(null);
            reply = doSendAndReceiveAsyncDefaultCorrelation(requestDestination, jmsRequest, session, priority);
        } else {
            reply = doSendAndReceiveAsync(requestDestination, jmsRequest, session, priority);
        }
        /*
			 * Remove the gateway's internal correlation Id to avoid conflicts with an upstream
			 * gateway.
			 */
        if (reply instanceof javax.jms.Message) {
            ((javax.jms.Message) reply).setJMSCorrelationID(null);
        }
        return reply;
    } finally {
        JmsUtils.closeSession(session);
        ConnectionFactoryUtils.releaseConnection(connection, this.connectionFactory, true);
    }
}
Also used : Destination(javax.jms.Destination) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) Message(org.springframework.messaging.Message) Connection(javax.jms.Connection) Session(javax.jms.Session)

Example 30 with IntegrationMessageHeaderAccessor

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

the class HeaderEnricherOverwriteTests method priorityExplicitOverwriteTrue.

@Test
public void priorityExplicitOverwriteTrue() {
    MessageChannel channel = context.getBean("priorityExplicitOverwriteTrueInput", MessageChannel.class);
    MessagingTemplate template = new MessagingTemplate();
    template.setDefaultDestination(channel);
    Message<?> result = template.sendAndReceive(new GenericMessage<String>("test"));
    assertNotNull(result);
    assertEquals(new Integer(42), new IntegrationMessageHeaderAccessor(result).getPriority());
}
Also used : MessagingTemplate(org.springframework.integration.core.MessagingTemplate) IntegrationMessageHeaderAccessor(org.springframework.integration.IntegrationMessageHeaderAccessor) MessageChannel(org.springframework.messaging.MessageChannel) 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