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.twitter.ignored.TwitterAnnouncer.java

public void search(Message<?> search) {
    MessageHistory history = MessageHistory.read(search);
    Tweet tweet = (Tweet) search.getPayload();
    logger.info("A search item was received " + tweet.getCreatedAt() + " with text " + tweet.getText());
}

From source file:org.springframework.integration.twitter.inbound.DirectMessageReceivingMessageSourceTests.java

@SuppressWarnings("unchecked")
@Test//www  .  ja  v  a  2  s  .  c o m
@Ignore
public void demoReceiveDm() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"),
            prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"),
            prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    DirectMessageReceivingMessageSource tSource = new DirectMessageReceivingMessageSource(template, "foo");
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<DirectMessage> message = (Message<DirectMessage>) tSource.receive();
        if (message != null) {
            DirectMessage tweet = message.getPayload();
            logger.info(
                    tweet.getSender().getScreenName() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}

From source file:org.springframework.integration.twitter.inbound.SearchReceivingMessageSourceTests.java

@SuppressWarnings("unchecked")
@Test//from w  w  w  . jav  a2 s.c om
@Ignore
public void demoReceiveSearchResults() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"),
            prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"),
            prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    SearchReceivingMessageSource tSource = new SearchReceivingMessageSource(template, "foo");
    tSource.setQuery(SEARCH_QUERY);
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<Tweet> message = (Message<Tweet>) tSource.receive();
        if (message != null) {
            Tweet tweet = message.getPayload();
            logger.info(tweet.getFromUser() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}

From source file:org.springframework.integration.twitter.inbound.TimelineReceivingMessageSourceTests.java

@SuppressWarnings("unchecked")
@Test//from  www.  j a v  a 2s  .c o  m
@Ignore
public void demoReceiveTimeline() throws Exception {
    PropertiesFactoryBean pf = new PropertiesFactoryBean();
    pf.setLocation(new ClassPathResource("sample.properties"));
    pf.afterPropertiesSet();
    Properties prop = pf.getObject();
    TwitterTemplate template = new TwitterTemplate(prop.getProperty("z_oleg.oauth.consumerKey"),
            prop.getProperty("z_oleg.oauth.consumerSecret"), prop.getProperty("z_oleg.oauth.accessToken"),
            prop.getProperty("z_oleg.oauth.accessTokenSecret"));
    TimelineReceivingMessageSource tSource = new TimelineReceivingMessageSource(template, "foo");
    tSource.afterPropertiesSet();
    for (int i = 0; i < 50; i++) {
        Message<Tweet> message = (Message<Tweet>) tSource.receive();
        if (message != null) {
            Tweet tweet = message.getPayload();
            logger.info(tweet.getFromUser() + " - " + tweet.getText() + " - " + tweet.getCreatedAt());
        }
    }
}

From source file:org.springframework.integration.vertx.DemoService.java

public List<Message<?>> getNext() {
    List<Message<?>> messages = new ArrayList<>();
    for (Entry<String, AtomicInteger> entry : clients.entrySet()) {
        Message<String> message = MessageBuilder
                .withPayload(Integer.toString(entry.getValue().incrementAndGet()))
                .setHeader(IpHeaders.CONNECTION_ID, entry.getKey()).build();
        messages.add(message);/*from  w w w . j a  v a  2s  .c  o  m*/
        logger.info("Sending " + message.getPayload() + " to connection " + entry.getKey());
    }
    return messages;
}

From source file:org.springframework.integration.x.bus.MessageBusSupport.java

protected final Message<?> serializePayloadIfNecessary(Message<?> message, MediaType to) {
    Object originalPayload = message.getPayload();
    Object originalContentType = message.getHeaders().get(MessageHeaders.CONTENT_TYPE);
    Object contentType = originalContentType;
    if (to.equals(ALL)) {
        return message;
    } else if (to.equals(APPLICATION_OCTET_STREAM)) {
        contentType = resolveContentType(originalPayload);
        Object payload = serializePayloadIfNecessary(originalPayload);
        MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(payload)
                .copyHeaders(message.getHeaders()).setHeader(MessageHeaders.CONTENT_TYPE, contentType);
        if (originalContentType != null) {
            messageBuilder.setHeader(ORIGINAL_CONTENT_TYPE_HEADER, originalContentType);
        }//from ww  w . j  av  a  2s  . c om
        return messageBuilder.build();
    } else {
        throw new IllegalArgumentException("'to' can only be 'ALL' or 'APPLICATION_OCTET_STREAM'");
    }
}

From source file:org.springframework.integration.x.bus.MessageBusSupport.java

protected final Message<?> deserializePayloadIfNecessary(Message<?> message) {
    Message<?> messageToSend = message;
    Object originalPayload = message.getPayload();
    MimeType contentType = contentTypeResolver.resolve(message.getHeaders());
    Object payload = deserializePayload(originalPayload, contentType);
    if (payload != null) {
        MessageBuilder<Object> transformed = MessageBuilder.withPayload(payload)
                .copyHeaders(message.getHeaders());
        Object originalContentType = message.getHeaders().get(ORIGINAL_CONTENT_TYPE_HEADER);
        transformed.setHeader(MessageHeaders.CONTENT_TYPE, originalContentType);
        transformed.setHeader(ORIGINAL_CONTENT_TYPE_HEADER, null);
        messageToSend = transformed.build();
    }/*from   w  w w . j a va 2s. c  o  m*/
    return messageToSend;
}

From source file:org.springframework.integration.x.rollover.file.RolloverFileMessageHandler.java

@Override
protected void handleMessageInternal(Message<?> message) throws Exception {
    Object payload = message.getPayload();

    if (payload instanceof String) {
        try {// w  w  w  . jav  a  2  s  . c  o m
            String s = (String) payload;
            if (!binary) {
                s += "\n";
            }
            IOUtils.write(s, outputStream);

            // rollover file after write completed.
            outputStream.rollover();
        } catch (IOException e) {
            logger.error("Failed to write payload to rollover output stream", e);
        }
    } else if (payload instanceof byte[]) {
        try {
            IOUtils.write(((byte[]) payload), outputStream);
        } catch (IOException e) {
            logger.error("Failed to write payload to rollover output stream", e);
        }
    } else {
        throw new MessagingException(message, "Only String and byte[] message payload are supported");
    }

    if (flushRate > 0 && ((messageCounter.addAndGet(1) % flushRate) == 0)) {
        outputStream.flush();
    }
}

From source file:org.springframework.integration.zip.splitter.UnZipResultSplitter.java

@Override
@SuppressWarnings("unchecked")
protected Object splitMessage(Message<?> message) {
    Assert.state(message.getPayload() instanceof Map,
            "The UnZipResultSplitter supports only Map<String, Object> payload");
    Map<String, Object> unzippedEntries = (Map<String, Object>) message.getPayload();
    MessageHeaders headers = message.getHeaders();

    List<MessageBuilder<Object>> messageBuilders = new ArrayList<MessageBuilder<Object>>(
            unzippedEntries.size());//from w ww . j av a2  s  . c  o  m

    for (Map.Entry<String, Object> entry : unzippedEntries.entrySet()) {
        String path = FilenameUtils.getPath(entry.getKey());
        String filename = FilenameUtils.getName(entry.getKey());
        MessageBuilder<Object> messageBuilder = MessageBuilder.withPayload(entry.getValue())
                .setHeader(FileHeaders.FILENAME, filename).setHeader(ZipHeaders.ZIP_ENTRY_PATH, path)
                .copyHeadersIfAbsent(headers);
        messageBuilders.add(messageBuilder);
    }

    return messageBuilders;
}

From source file:org.springframework.integration.zip.transformer.AbstractZipTransformer.java

/**
 * @param message the message and its payload must not be null.
 *//*from   w  w  w . ja va2 s .c  o m*/
@Override
protected Object doTransform(Message<?> message) throws Exception {
    Assert.notNull(message, "message must not be null");
    final Object payload = message.getPayload();
    Assert.notNull(payload, "payload must not be null");

    return doZipTransform(message);
}