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:com.example.common.MoneyJacksonAutoConfiguration.java

@Override
public Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
    Object result = null;//from   w  ww  .j  a  v a2 s .  c  o  m
    try {
        Object payload = message.getPayload();

        if (payload instanceof byte[]) {
            result = mapper.readValue((byte[]) payload, targetClass);
        } else if (payload instanceof String) {
            result = mapper.readValue((String) payload, targetClass);
        }
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return null;
    }
    return result;
}

From source file:biz.c24.io.spring.integration.config.FileSplitterTests.java

@Test
public void defaultBatchSize() throws Exception {
    Boolean result = (Boolean) transformer
            .doTransform(MessageBuilder.withPayload((resource.getFile())).build());
    Message<List<String>> message;
    int messageCount = 0;
    for (int i = 0; i < 10; i++) {
        message = (Message<List<String>>) feedChannel.receive();
        assertThat(message.getPayload().get(0), is(i + 1 + ""));
        messageCount++;/*from   ww  w . j  a v a2 s  .  c om*/
    }
    assertThat(messageCount, is(10));

}

From source file:nl.rav.comparision.integration.unitofwork.java.UnitOfWorkSpringTest.java

@Test
public void testSuccess() throws Exception {
    Message<?> reply = sendMessage();
    assertNotNull(reply);//from  w ww .j a  v  a2s  .c  o  m
    assertEquals("baz", reply.getPayload());

    assertNotNull(completed);
    assertEquals("Hello, world!", ((AdviceMessage) completed).getInputMessage().getPayload());
    assertEquals("foo", completed.getPayload());

    assertNull(failed);
}

From source file:com.create.batch.integration.FileMessageToJobRequest.java

@Transformer(inputChannel = "inboundFileChannel", outputChannel = "outboundJobRequestChannel")
public JobLaunchRequest toRequest(final Message<File> message) {
    log.debug("toRequest : {}", message);
    final Instant timestamp = clock.instant();
    final JobParameters jobParameters = new JobParametersBuilder()
            .addString(jobParameter, message.getPayload().getAbsolutePath())
            .addLong(TIMESTAMP_PARAMETER, timestamp.getEpochSecond()).toJobParameters();
    return new JobLaunchRequest(job, jobParameters);
}

From source file:nz.co.senanque.messaging.GenericEndpoint.java

public void unpackMessage(Message<?> message, Object context) {
    Object payload = message.getPayload();
    if (payload instanceof org.w3c.dom.Document) {
        Document document = new DOMBuilder().build((org.w3c.dom.Document) payload);
        if (log.isDebugEnabled()) {
            log.debug("document\n{}", getStringFromDoc((org.w3c.dom.Document) payload));
        }//from  w  ww  .  j av  a2 s  .  co m
        Element root = document.getRootElement();
        String errorValue = root.getAttributeValue("error");
        if (errorValue != null) {
            throw new WorkflowException(errorValue);
        }
        unpackRoot(root, context);
    } else {
        throw new WorkflowException(
                "Expected payload to be org.w3c.dom.Document, instead found a " + payload.getClass().getName());
    }
}

From source file:biz.c24.io.spring.integration.config.FileSplitterTests.java

@Test
public void splitOnEvens() throws Exception {
    transformer.setInitiator("^-?\\d*[02468]$");
    Boolean result = (Boolean) transformer
            .doTransform(MessageBuilder.withPayload((resource.getFile())).build());
    Message<List<String>> message;
    int messageCount = 0;
    for (int i = 0; i < 5; i++) {
        message = (Message<List<String>>) feedChannel.receive();
        if (i < 4) {
            assertThat(message.getPayload().get(0), is((i + 1) * 2 + "" + ((i + 1) * 2 + 1)));
        } else {//from   ww w  . j  ava 2s  .  c  om
            assertThat(message.getPayload().get(0), is("10"));
        }
        messageCount++;
    }
    assertThat(messageCount, is(5));
}

From source file:com.kinglcc.spring.jms.core.converter.Jackson2MessageAdapterConverter.java

@Override
public Object fromMessage(Message<?> message, Type targetType, Class<?> contextClass) {
    ObjectMapper objectMapper = getObjectMapper();
    JavaType javaType = objectMapper.getTypeFactory().constructType(targetType, contextClass);
    try {/*w  w  w  .  j ava2s  . com*/
        Object payload = message.getPayload();
        if (payload instanceof byte[]) {
            return objectMapper.readValue((byte[]) payload, javaType);
        } else {
            return objectMapper.readValue((String) payload, javaType);
        }
    } catch (IOException ex) {
        throw new MessageConversionException(message, "Could not read JSON: " + ex.getMessage(), ex);
    }
}

From source file:ru.asmsoft.p2p.heartbeat.Heartbeat.java

@ServiceActivator(inputChannel = "incoming-heartbeat")
public void handleIncomingPing(Message<PingPacket> message) {
    logger.trace("Received: {}", message);

    String nodeAddress = (String) message.getHeaders().get("ip_address");
    nodeRepository.registerNode(nodeAddress);

    PingPacket packet = message.getPayload();

    // If our DB is outdated
    if (messageRepository.getDbVersion() < packet.getDbVersion()) {
        selfUpdateService.startNodeUpdate(nodeAddress);
    }/*from  w  ww  . ja v  a 2 s  .  c  o  m*/

}

From source file:nz.co.senanque.messaging.ErrorEndpoint.java

public void unpackMessage(Message<?> message, Object context) {
    // this is always an error so extract the error message and throw an exception.
    final MessagingException messagingException = (MessagingException) message.getPayload();
    throw new WorkflowException(messagingException.getCause().getMessage());
}

From source file:biz.c24.io.spring.integration.config.ValidatingSelectorTests.java

@Test
public void testValid() {
    Employee employee = new Employee();
    employee.setSalutation("Mr");
    employee.setFirstName("Andy");
    employee.setLastName("Acheson");
    employee.setJobTitle("Software Developer");

    template.convertAndSend(employee);//from  ww w  .j  av a2 s .co  m

    @SuppressWarnings("unchecked")
    Message<Employee> result = (Message<Employee>) validChannel.receive(1);

    assertThat(result, is(not(nullValue())));
    assertThat(result.getPayload(), is(Employee.class));

    // Make sure there are no other messages floating around
    assertThat(validChannel.receive(1), is(nullValue()));
    assertThat(invalidChannel.receive(1), is(nullValue()));
}