Java Reference
In-Depth Information
channel.subscribe(new MessageHandler() {
public void handleMessage(Message<?> message) {
Tweet tweet = (Tweet) message.getPayload();
logger.debug(String.format("Received %s at %s ", tweet
.toString(), new Date().toString()));
Here, you've done very little except manually subscribe (which is what components do behind
the scenes when you configure it in XML, although it's quite succinct) to messages that come onto the
channel (from the MessageSource ) and print them out.
An Outbound Twitter Example
You've seen how to consume Twitter status updates on the bus. Now, let's figure out how to send
messages from the bus to Twitter. You will build an outbound Twitter adapter. This component will
accept status updates (messages of type Tweet ) coming in a channel and update the configured account
with the new status.
In the last example, you built a class that implemented MessageSource, and explained that you
could optionally configure a regular POJO and simply instruct Spring Integration to use a particular
method in lieu of relying on the interface-enforced receive method of the MessageSource interface. The
same is true in the opposite direction. You can implement MessageHandler<T> , whose API is similarly
public interface MessageHandler {
void handleMessage(Message<?> message)
throws MessageRejectedException,
This time, you won't implement the interface, but it's useful to know it's there. The code for the
outbound adapter is much simpler than the code for the inbound adapter because you have no quirks
to contend with, and the mapping from Spring Integration to Twitter is sane: one message to one status
package com.apress.springenterpriserecipes.springintegration.twitter;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.integration.core.Message;
import org.springframework.integration.message.MessageBuilder;
import org.springframework.util.Assert;
import twitter4j.Twitter;
import twitter4j.TwitterException;
public class TwitterMessageProducer implements InitializingBean {
Search WWH ::

Custom Search