Example usage for org.springframework.messaging Message getPayload

List of usage examples for org.springframework.messaging Message getPayload

Introduction

In this page you can find the example usage for org.springframework.messaging Message getPayload.

Prototype

T getPayload();

Source Link

Document

Return the message payload.

Usage

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testRecipientListRouter() {

    Message<String> fooMessage = MessageBuilder.withPayload("fooPayload").setHeader("recipient", true).build();
    Message<String> barMessage = MessageBuilder.withPayload("barPayload").setHeader("recipient", true).build();
    Message<String> badMessage = new GenericMessage<>("badPayload");

    this.recipientListInput.send(fooMessage);
    Message<?> result1a = this.fooChannel.receive(2000);
    assertNotNull(result1a);//ww w.j  a  va2s. c o m
    assertEquals("foo", result1a.getPayload());
    Message<?> result1b = this.barChannel.receive(2000);
    assertNotNull(result1b);
    assertEquals("foo", result1b.getPayload());

    this.recipientListInput.send(barMessage);
    assertNull(this.fooChannel.receive(0));
    Message<?> result2b = this.barChannel.receive(2000);
    assertNotNull(result2b);
    assertEquals("bar", result2b.getPayload());

    this.recipientListInput.send(badMessage);
    assertNull(this.fooChannel.receive(0));
    assertNull(this.barChannel.receive(0));
    Message<?> result3c = this.defaultOutputChannel.receive(2000);
    assertNotNull(result3c);
    assertEquals("bad", result3c.getPayload());

}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testPriority() throws InterruptedException {
    Message<String> message = MessageBuilder.withPayload("1").setPriority(1).build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("-1").setPriority(-1).build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("3").setPriority(3).build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("0").setPriority(0).build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("2").setPriority(2).build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("none").build();
    this.priorityChannel.send(message);

    message = MessageBuilder.withPayload("31").setPriority(3).build();
    this.priorityChannel.send(message);

    this.controlBus.send("@priorityChannelBridge.start()");

    Message<?> receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);/*from  w  w  w. j av a  2 s .c  om*/
    assertEquals("3", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("31", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("2", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("1", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("0", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("-1", receive.getPayload());

    receive = this.priorityReplyChannel.receive(2000);
    assertNotNull(receive);
    assertEquals("none", receive.getPayload());

    this.controlBus.send("@priorityChannelBridge.stop()");
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testMessageProducerFlow() throws Exception {
    FileOutputStream file = new FileOutputStream(new java.io.File(tmpDir, "TailTest"));
    for (int i = 0; i < 50; i++) {
        file.write((i + "\n").getBytes());
    }// w  ww.  jav a2  s  . c o  m

    for (int i = 0; i < 50; i++) {
        Message<?> message = this.tailChannel.receive(5000);
        assertNotNull(message);
        assertEquals("hello " + i, message.getPayload());
    }
    assertNull(this.tailChannel.receive(1));

    this.controlBus.send("@tailer.stop()");
    file.close();
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testGatewayFlow() throws Exception {
    PollableChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("foo").setReplyChannel(replyChannel).build();

    this.gatewayInput.send(message);

    Message<?> receive = replyChannel.receive(2000);
    assertNotNull(receive);/*from   ww w  . j a va  2 s . c o m*/
    assertEquals("FOO", receive.getPayload());
    assertNull(this.gatewayError.receive(1));

    message = MessageBuilder.withPayload("bar").setReplyChannel(replyChannel).build();

    this.gatewayInput.send(message);

    receive = replyChannel.receive(1);
    assertNull(receive);

    receive = this.gatewayError.receive(2000);
    assertNotNull(receive);
    assertThat(receive, instanceOf(ErrorMessage.class));
    assertThat(receive.getPayload(), instanceOf(MessageRejectedException.class));
    assertThat(((Exception) receive.getPayload()).getMessage(), containsString("' rejected Message"));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testAmqpOutboundFlow() throws Exception {
    this.amqpOutboundInput
            .send(MessageBuilder.withPayload("hello through the amqp").setHeader("routingKey", "foo").build());
    Message<?> receive = null;
    int i = 0;/* w w  w.  j  a  v  a2s  .  c om*/
    do {
        receive = this.amqpReplyChannel.receive();
        if (receive != null) {
            break;
        }
        Thread.sleep(100);
        i++;
    } while (i < 10);

    assertNotNull(receive);
    assertEquals("HELLO THROUGH THE AMQP", receive.getPayload());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testJmsOutboundInboundFlow() {
    this.jmsOutboundInboundChannel.send(MessageBuilder.withPayload("hello THROUGH the JMS")
            .setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "jmsInbound").build());

    Message<?> receive = this.jmsOutboundInboundReplyChannel.receive(5000);

    assertNotNull(receive);//from   w w  w. ja va 2s . c  om
    assertEquals("HELLO THROUGH THE JMS", receive.getPayload());

    this.jmsOutboundInboundChannel.send(MessageBuilder.withPayload("hello THROUGH the JMS")
            .setHeader(SimpMessageHeaderAccessor.DESTINATION_HEADER, "jmsMessageDriver").build());

    receive = this.jmsOutboundInboundReplyChannel.receive(5000);

    assertNotNull(receive);
    assertEquals("hello through the jms", receive.getPayload());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testJmsPipelineFlow() {
    PollableChannel replyChannel = new QueueChannel();
    Message<String> message = MessageBuilder.withPayload("hello through the jms pipeline")
            .setReplyChannel(replyChannel).setHeader("destination", "jmsPipelineTest").build();
    this.jmsOutboundGatewayChannel.send(message);

    Message<?> receive = replyChannel.receive(5000);

    assertNotNull(receive);/*  w w w.jav a  2 s .co m*/
    assertEquals("HELLO THROUGH THE JMS PIPELINE", receive.getPayload());
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testFileReadingFlow() throws Exception {
    List<Integer> evens = new ArrayList<>(25);
    for (int i = 0; i < 50; i++) {
        boolean even = i % 2 == 0;
        String extension = even ? ".sitest" : ".foofile";
        if (even) {
            evens.add(i);/*w ww. j av a 2  s.  c  om*/
        }
        FileOutputStream file = new FileOutputStream(new java.io.File(tmpDir, i + extension));
        file.write(("" + i).getBytes());
        file.flush();
        file.close();
    }

    Message<?> message = fileReadingResultChannel.receive(10000);
    assertNotNull(message);
    Object payload = message.getPayload();
    assertThat(payload, instanceOf(List.class));
    @SuppressWarnings("unchecked")
    List<String> result = (List<String>) payload;
    assertEquals(25, result.size());
    result.forEach(s -> assertTrue(evens.contains(Integer.parseInt(s))));
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testFileWritingFlow() throws Exception {
    String payload = "Spring Integration";
    this.fileWritingInput.send(new GenericMessage<>(payload));
    Message<?> receive = this.fileWritingResultChannel.receive(1000);
    assertNotNull(receive);/*from w  w w  .  ja va2  s.  com*/
    assertThat(receive.getPayload(), instanceOf(java.io.File.class));
    java.io.File resultFile = (java.io.File) receive.getPayload();
    assertThat(resultFile.getAbsolutePath(),
            endsWith(TestUtils.applySystemFileSeparator("fileWritingFlow/foo.sitest")));
    String fileContent = StreamUtils.copyToString(new FileInputStream(resultFile), Charset.defaultCharset());
    assertEquals(payload, fileContent);
}

From source file:org.springframework.integration.dsl.test.IntegrationFlowTests.java

@Test
public void testFtpInboundFlow() {
    Message<?> message = this.ftpInboundResultChannel.receive(1000);
    assertNotNull(message);/*  ww  w.  ja v a2s.  c o m*/
    Object payload = message.getPayload();
    assertThat(payload, instanceOf(java.io.File.class));
    java.io.File file = (java.io.File) payload;
    assertThat(file.getName(), isOneOf("FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a"));
    assertThat(file.getAbsolutePath(), containsString("ftpTest"));

    message = this.ftpInboundResultChannel.receive(1000);
    assertNotNull(message);
    file = (java.io.File) message.getPayload();
    assertThat(file.getName(), isOneOf("FTPSOURCE1.TXT.a", "FTPSOURCE2.TXT.a"));
    assertThat(file.getAbsolutePath(), containsString("ftpTest"));

    this.controlBus.send("@ftpInboundAdapter.stop()");
}