Example usage for org.springframework.integration.support MessageBuilder withPayload

List of usage examples for org.springframework.integration.support MessageBuilder withPayload

Introduction

In this page you can find the example usage for org.springframework.integration.support MessageBuilder withPayload.

Prototype

public static <T> MessageBuilder<T> withPayload(T payload) 

Source Link

Document

Create a builder for a new Message instance with the provided payload.

Usage

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

public void ping() throws IOException {
    pingPacket = MessageBuilder.withPayload(new PingPacket(messageRepository.getDbVersion())).build();
    logger.trace("Send: {}", pingPacket);
    outgoingHeartbeatChannel.send(pingPacket);
}

From source file:ru.asmsoft.p2p.transaction.IncomingTransactionManager.java

private Message prepareResponse(Message requestMessage, P2PPacket response) {

    Message responseMessage = MessageBuilder.withPayload(response)
            .setHeader("ip_address", requestMessage.getHeaders().get("ip_address"))
            .setHeader("ip_port", nodeConfiguration.getPort()).setHeader("uuid", response.getUuid()).build();

    return responseMessage;

}

From source file:org.opencredo.cloud.storage.si.transformer.internal.BlobToByteArrayTransformer.java

/**
 * @param message/*from ww  w . ja v  a2s  .  co m*/
 * @throws IOException
 */
public Message<byte[]> doTransform(Message<BlobDetails> message) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Transform blob to byte array: '{}'", String.valueOf(message.getPayload()));
    }
    BlobDetails payload = message.getPayload();

    MessageBuilder<byte[]> builder;
    InputStream input = getTemplate().receiveAsInputStream(payload.getContainerName(), payload.getName());
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    try {
        IOUtils.copy(input, output);
    } catch (IOException e) {
        throw new BlobTransformException("Failed to copy blob [" + payload + "] byte stream to byte array", e);
    }

    builder = (MessageBuilder<byte[]>) MessageBuilder.withPayload(output.toByteArray())//
            .copyHeaders(message.getHeaders());
    Message<byte[]> blobMessage = builder.build();

    return blobMessage;
}

From source file:siia.booking.integration.CancellationsTest.java

@Test
public void testRejection() {
    CancellationRequest cancellationRequest = new CancellationRequest();
    cancellationRequest.setReservationCode("SILVER123456");
    input.send(MessageBuilder.withPayload(cancellationRequest).build());
    Message<?> confirmedMessage = confirmed.receive(0);
    assertNull(confirmedMessage);//from  w  w w. jav a  2 s  . c om
    Message<?> rejectedMessage = rejected.receive(0);
    assertNotNull(rejectedMessage);
    assertEquals(CancellationRequest.class, rejectedMessage.getPayload().getClass());
    CancellationRequest request = (CancellationRequest) rejectedMessage.getPayload();
    assertEquals("SILVER123456", request.getReservationCode());
}

From source file:siia.booking.integration.FlightNotificationsSpelTest.java

@Test
public void shouldEnrichHeaderAndSendMessageForRelatedTrips() {
    final Flight flight = mock(Flight.class);
    final FlightNotification notification = new FlightNotification("Flight has been cancelled", flight);
    final Message<FlightNotification> testMessage = MessageBuilder.withPayload(notification).build();
    Trip trip1 = mock(Trip.class);
    Trip trip2 = mock(Trip.class);
    List<Trip> relatedTrips = asList(trip1, trip2);
    given(mockedTripRepository.findTripsRelatedTo(flight)).willReturn(relatedTrips);
    flightNotifications.send(testMessage);
    Message<TripNotification> tripNotificationMessage1 = (Message<TripNotification>) interceptedTripNotifications
            .receive(1000);/*from w  ww .jav a  2  s.c  o  m*/
    assertThat(tripNotificationMessage1, is(notNullValue()));
    assertThat((List<Trip>) tripNotificationMessage1.getHeaders().get("affectedTrips"), is(relatedTrips));
    assertThat(tripNotificationMessage1.getPayload().getTrip(), is(sameInstance(trip1)));
    Message<TripNotification> tripNotificationMessage2 = (Message<TripNotification>) interceptedTripNotifications
            .receive(1000);
    assertThat(tripNotificationMessage2.getPayload().getTrip(), is(sameInstance(trip2)));
}

From source file:com.consol.citrus.samples.bookstore.BookStore.java

/**
 * Get the book details for a book with given isbn.
 * @param request// w  w  w  .j av  a2  s .  c om
 * @return
 */
public Message<GetBookDetailsResponseMessage> getBookDetails(Message<GetBookDetailsRequestMessage> request) {
    GetBookDetailsResponseMessage response = new GetBookDetailsResponseMessage();

    Book book = bookStore.get(request.getPayload().getIsbn());

    if (book == null) {
        throw new UnknownBookException(request, request.getPayload().getIsbn());
    } else {
        response.setBook(book);
    }

    return MessageBuilder.withPayload(response).build();
}

From source file:siia.booking.integration.CancellationsWithExceptionTest.java

@Test(expected = MessageRejectedException.class)
public void testRejection() {
    CancellationRequest cancellationRequest = new CancellationRequest();
    cancellationRequest.setReservationCode("SILVER123456");
    input.send(MessageBuilder.withPayload(cancellationRequest).build());
}

From source file:siia.booking.integration.CancellationsWithNotificationTest.java

@Test
public void testCancellationNotification() {
    CancellationRequest cancellationRequest = new CancellationRequest();
    cancellationRequest.setReservationCode("BRONZE123");
    input.send(MessageBuilder.withPayload(cancellationRequest).build());
    Message<?> confirmedMessage = confirmed.receive(0);
    assertNull(confirmedMessage);/* ww w. ja v  a2 s  .  co  m*/
    assertEquals(1, mailSender.getCount());
    assertEquals("BRONZE123 has been rejected", mailSender.getLastMessageText());
}

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

@Override
public void startNodeUpdate(String nodeAddress) {

    UpdateMePacket updateMePacket = new UpdateMePacket();

    Message outgoingMessage = MessageBuilder.withPayload(updateMePacket).setHeader("ip_address", nodeAddress)
            .setHeader("ip_port", nodeConfiguration.getPort()).setHeader("uuid", updateMePacket.getUuid())
            .build();//from  w w  w  .ja  v  a 2  s. c  om

    // Send UpdateMePacket
    outgoingChannel.send(outgoingMessage);

    outgoingUpdateMeChannel.send(outgoingMessage);

    // Switch state machine
    stateMachine.sendEvent(NodeEvents.UpdateMeRequestSent);

}

From source file:com.excelsiorsoft.transformer.TransformationHandler.java

/**
 * Actual Spring Integration transformation handler.
 *
 * @param inputMessage Spring Integration input message
 * @return New Spring Integration message with updated headers
 *//*  w  ww .jav a2  s .com*/
@Transformer
public Message<byte[]> handleFile(final Message<File> inputMessage) {

    final File inputFile = inputMessage.getPayload();
    final String filename = inputFile.getName();

    final String inputAsString;

    try {
        inputAsString = FileUtils.readFileToString(inputFile);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    ByteArrayOutputStream bout = new ByteArrayOutputStream();

    Workbook wb = new HSSFWorkbook();
    Sheet sheet = wb.createSheet("Sample Sheet");

    // Create a row and put some cells in it. Rows are 0 based.
    Row row = sheet.createRow((short) 0);
    // Create a cell and put a value in it.
    Cell cell = row.createCell(0);

    cell.setCellValue(inputAsString);

    try {
        wb.write(bout);
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }

    final Message<byte[]> message = MessageBuilder.withPayload(bout.toByteArray())
            .setHeader(FileHeaders.FILENAME, filename + ".xls").setHeader(FileHeaders.ORIGINAL_FILE, inputFile)
            .setHeader("file_size", inputFile.length()).build();

    return message;
}