concept Twitter in category java

appears as: Twitter
Spring Integration in Action

This is an excerpt from Manning's book Spring Integration in Action.

In Spring Integration, several polling endpoints are provided: inbound channel adapters for files, JMS, email, and a generic method-invoking variant. The latter can be used to create custom channel adapters, such as for querying Twitter or a Really Simple Syndication (RSS) feed. Twitter and RSS adapters are both available as of Spring Integration 2.0, but before they were added, users could simply configure the generic method-invoking adapter to call their own implementations.

Likewise, Spring Integration will evolve to provide many more channel adapters in the social media realm. The relatively new Spring Social project provides support for a number of different social media technologies, and it’s easy for Spring Integration to build adapters on top of those underlying APIs. The supporting classes provided in the Spring Social project take the form of templates, strategies, and other common idioms of the Spring programming model. Twitter is one of the supported technologies, and as of version 2.1, Spring Integration relies completely on that library’s TwitterTemplate.

13.2.6. Sending and receiving direct messages via Twitter

We mentioned that Twitter, in general, supports a broad publish-subscribe style of messaging. Although it’s probably not as popular, Twitter also supports a point-to-point style via direct messages. The concept is simple. If you want to send a message to someone who follows you, and you want only that person to receive it, then use direct messages instead of updating your status.

When considering the classification of communication options, direct messaging with Twitter seems to sit between email and chat. Just as some people refer to Twitter public timeline posting as microblogging, the direct message functionality may be thought of as micromailing. In both cases, the essential characteristic of the Twitter alternative is its enforced brevity. From a cultural perspective, it’s interesting to see the wild success of a technology that seems to be defined by a limitation. Even though the content and size of messages may have more in common with those of an IM chat session, the request-and-reply interaction typically feels more like email. To some degree, those who frequently use direct messaging with Twitter embrace the asynchronous nature of communication. They accept the reality that even if other parties are currently available, they may prefer to respond when it’s more convenient for them. Using a phone call analogy, it’s the same reason voice mail is so commonly used even on a mobile phone that’s most likely in the presence of its owner most of the time. When you send a colleague a message like “Want to grab a beer after work?” you probably don’t need to know the answer immediately. Likewise, the receiver will likely perceive it as more polite that the sender acknowledges the question as a lower priority than one that would necessitate a synchronous phone call. It’s as if Twitter gives the sender the option to go straight to voice mail. Enough philosophizing. Let’s take a look at the Spring Integration channel adapters that support direct messages.

The outbound channel adapter for sending direct messages via Twitter looks similar to the status updating channel adapter. The key difference is that its element name is qualified with the dm- prefix. Because sending direct messages requires authentication, you also need to provide a reference to a fully configured TwitterTemplate instance. That configuration must include the OAuth credentials:

<int-twitter:dm-outbound-channel-adapter id="dmOutAdapter"
                                     twitter-template="twitterTemplate"
                                     channel="dmOutChannel"/>

There’s one more requirement when sending a direct message via Twitter. You must somehow specify the user to whom you’d like to send the message. As you’d likely expect by now, the target user information should be specified in a message header. There’s a constant defined to represent that header’s name: TwitterHeaders.DM_TARGET_USER_ID. Here’s an example of programmatically sending a Spring Integration message that ultimately triggers the sending of a Twitter direct message. This example assumes the configuration of the dmOutChannel, as shown in the earlier dm-outbound-channel-adapter example: