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:biz.c24.io.spring.integration.transformer.IoMarshallingTransformerIUTests.java

@Test
public void canMarshalTextToString() throws Exception {

    C24MarshallingTransformer ioMarshallingTransformer = new C24MarshallingTransformer();
    ioMarshallingTransformer.setOutputType(OutputType.STRING);
    ioMarshallingTransformer.setSinkFactory(new TextualSinkFactory());

    Message message = MessageBuilder.withPayload(loadObject()).build();

    Message<?> outputMessage = ioMarshallingTransformer.transform(message);

    assertThat((String) outputMessage.getPayload(), is(loadCsvString()));

}

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

/**
 * Get the book cover for a book with given isbn.
 * @param request/*from  w w  w. j ava  2 s.  c o m*/
 * @return
 */
public Message<GetBookAbstractResponseMessage> getBookAbstract(Message<GetBookAbstractRequestMessage> request) {
    GetBookAbstractResponseMessage response = new GetBookAbstractResponseMessage();

    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.BookingHeaderTests.java

@Test
public void bookExample() {
    BookFlightCommand testBookFlightCommand = new BookFlightCommand("SFO", "ORD");
    Message<?> testMessage = MessageBuilder.withPayload(testBookFlightCommand).setCorrelationId("ABC").build();
    inputChannel.send(testMessage);/* w ww. j  a  v  a2  s. c  o  m*/
    Message<?> reply = outputChannel.receive();
    assertThat(reply, hasHeaderKey("command"));
    assertThat(reply, hasHeader("command", notNullValue()));
    assertThat(reply, hasHeader("command", is(BookFlightCommand.class)));
    assertThat(reply, hasHeader("command", testBookFlightCommand));
    assertThat(reply, hasCorrelationId("ABC"));
    Map<String, Object> map = new HashMap<String, Object>();
    map.put("command", testBookFlightCommand);
    map.put(MessageHeaders.CORRELATION_ID, "ABC");
    assertThat(reply, hasAllHeaders(map));
}

From source file:org.springintegration.service.impl.DefaultTwitterService.java

/** {@inheritDoc} */
@Override//  w w w .  j  av a  2  s .  co  m
public void startTwitterAdapter() {

    Message<String> operation = MessageBuilder.withPayload("@twitter.start()").build();

    this.controlBusChannel.send(operation);

    if (this.dummyTwitter != null) {
        this.controlBusChannel.send(MessageBuilder.withPayload("@dummyTwitter.start()").build());
    }
}

From source file:org.opencredo.couchdb.inbound.CouchDbChangesPollingMessageSource.java

private Message<URI> prepareMessage(ChangedDocument changedDocument) {
    Message<URI> message = null;
    if (changedDocument != null) {
        message = MessageBuilder.withPayload(changedDocument.getUri()).build();
    }/* w w w .j  a v a  2 s  . com*/
    return message;
}

From source file:ru.asmsoft.p2p.fsm.NodeLifeCycle.java

@OnTransitionEnd(target = "UPDATING_REMOTE")
public void sendUpdatePacket() {

    // Build updating packet
    MessagePacket packet = new MessagePacket(messageRepository.getDbVersion() + 1, incomingBuffer.copyBuffer());

    // Register changeset
    messageRepository.registerChangeset(messageRepository.getDbVersion() + 1, incomingBuffer.getBuffer());

    // Clear the pending buffer
    incomingBuffer.clear();//from   w ww  .  j a  va  2  s.c o m

    // Send the update packet to broadcast
    outgoingBroadcastChannel.send(MessageBuilder.withPayload(packet).build());
}

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

@Test
public void endToEndLegQuoteTest() throws Exception {
    this.quoteRequestsChannel.send(MessageBuilder.withPayload(exampleLegQuote).build(), 1000);

    Message asXml = this.carQuotesChannel.receive(5000);
    assertNotNull(asXml);//  www.j  a  va  2 s .  com
    System.out.println(xmlDocToString((Document) asXml.getPayload()));

    asXml = this.flightQuotesChannel.receive(5000);
    System.out.println(asXml);
    System.out.println(xmlDocToString((Document) asXml.getPayload()));

    asXml = this.hotelQuotesChannel.receive(5000);
    System.out.println(asXml);
    System.out.println(xmlDocToString((Document) asXml.getPayload()));

}

From source file:org.springframework.cloud.stream.config.MessageChannelConfigurerTests.java

@Test
public void testMessageConverterConfigurer() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    MessageHandler messageHandler = new MessageHandler() {
        @Override//  w  w  w .  j  av  a 2s . c o  m
        public void handleMessage(Message<?> message) throws MessagingException {
            assertThat(message.getPayload()).isInstanceOf(Tuple.class);
            assertThat(((Tuple) message.getPayload()).getFieldNames().get(0)).isEqualTo("message");
            assertThat(((Tuple) message.getPayload()).getValue(0)).isEqualTo("Hi");
            latch.countDown();
        }
    };
    testSink.input().subscribe(messageHandler);
    testSink.input().send(MessageBuilder.withPayload("{\"message\":\"Hi\"}").build());
    assertThat(latch.await(10, TimeUnit.SECONDS)).isTrue();
    testSink.input().unsubscribe(messageHandler);
}

From source file:com.joshlong.activiti.coordinator.CoordinatorGatewayClient.java

public Message<?> processMessage(Message<?> message) throws MessagingException {
    try {/*from   w  w  w.  j av a  2  s  .com*/
        MessageHeaders headers = message.getHeaders();

        String procName = (String) headers.get(CoordinatorConstants.PROCESS_NAME);

        String stateName = (String) headers.get(CoordinatorConstants.STATE_NAME);

        ActivitiStateHandlerRegistration registration = registry.findRegistrationForProcessAndState(procName,
                stateName);

        Object bean = applicationContext.getBean(registration.getBeanName());

        Method method = registration.getHandlerMethod();
        //         int size = method.getParameterTypes().length;
        ArrayList<Object> argsList = new ArrayList<Object>();

        Map<Integer, String> processVariablesMap = registration.getProcessVariablesExpected();

        // already has which indexes get which process variables
        Map<Integer, Object> variables = new HashMap<Integer, Object>();

        for (Integer i : processVariablesMap.keySet())
            variables.put(i, headers.get(processVariablesMap.get(i)));

        if (registration.requiresProcessId()) {
            variables.put(registration.getProcessIdIndex(), headers.get(CoordinatorConstants.PROC_ID));
        }

        //         System.out.println(variables.toString());

        List<Integer> indices = new ArrayList<Integer>(variables.keySet());
        Collections.sort(indices);

        argsList.clear();

        for (Integer idx : indices)
            argsList.add(variables.get(idx));

        Object[] args = argsList.toArray(new Object[argsList.size()]);
        Object result = (args.length == 0) ? method.invoke(bean) : method.invoke(bean, args);

        MessageBuilder builder = MessageBuilder.withPayload(message.getPayload())
                .copyHeaders(message.getHeaders());

        if (result instanceof Map) {
            // then we update the BPM
            // by including them in the reply message
            builder.copyHeadersIfAbsent((Map) result);
        }

        return builder.build();
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }
}

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

/**
 * List all books in this registry.// w ww.  ja  v a2s  .c  om
 * @return
 */
public Message<ListBooksResponseMessage> listBooks() {
    ListBooksResponseMessage response = new ListBooksResponseMessage();
    Books books = new Books();
    books.getBooks().addAll(bookStore.values());
    response.setBooks(books);

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