Chapter 349. Twitter Components
Available as of Camel version 2.10
The camel-twitter consists of 4 components:
The Twitter components enable the most useful features of the Twitter API by encapsulating Twitter4J. It allows direct, polling, or event-driven consumption of timelines, users, trends, and direct messages. Also, it supports producing messages as status updates or direct messages.
Twitter now requires the use of OAuth for all client application authentication. In order to use camel-twitter with your account, you’ll need to create a new application within Twitter at https://dev.twitter.com/apps/new and grant the application access to your account. Finally, generate your access token and secret.
Maven users will need to add the following dependency to their pom.xml for this component:
<dependency> <groupId>org.apache.camel</groupId> <artifactId>camel-twitter</artifactId> <version>${camel-version}</version> </dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-twitter</artifactId>
<version>${camel-version}</version>
</dependency>
349.1. Consumer endpoints Copy linkLink copied to clipboard!
Rather than the endpoints returning a List through one single route exchange, camel-twitter creates one route exchange per returned object. As an example, if "timeline/home" results in five statuses, the route will be executed five times (one for each Status).
Endpoint | Context | Body Type | Notice |
---|---|---|---|
direct, polling | twitter4j.DirectMessage |
| |
direct, polling | twitter4j.Status |
| |
event, polling | twitter4j.Status |
| |
direct, polling | twitter4j.Status |
|
349.2. Producer endpoints Copy linkLink copied to clipboard!
Endpoint | Body Type | Notice |
---|---|---|
String | ||
List<twitter4j.Status> | ||
String | Only 'user' timelineType is supported for producer |
349.3. Message headers Copy linkLink copied to clipboard!
Name | Description |
---|---|
| This header is used by the search producer to change the search key words dynamically. |
|
Camel 2.11.0: This header can override the option of |
|
Camel 2.11.0 This header can override the option of |
|
Camel 2.11.0 This header can override the option of |
349.4. Message body Copy linkLink copied to clipboard!
All message bodies utilize objects provided by the Twitter4J API.
349.5. Use cases Copy linkLink copied to clipboard!
API Rate Limits: Twitter REST APIs encapsulated by Twitter4J are subjected to API Rate Limiting. You can find the per method limits in the API Rate Limits documentation. Note that endpoints/resources not listed in that page are default to 15 requests per allotted user per window.
349.5.1. To create a status update within your Twitter profile, send this producer a String body: Copy linkLink copied to clipboard!
from("direct:foo") .to("twitter-timeline://user?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]);
from("direct:foo")
.to("twitter-timeline://user?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]);
349.5.2. To poll, every 60 sec., all statuses on your home timeline: Copy linkLink copied to clipboard!
from("twitter-timeline://home?type=polling&delay=60&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]") .to("bean:blah");
from("twitter-timeline://home?type=polling&delay=60&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
.to("bean:blah");
349.5.3. To search for all statuses with the keyword 'camel' only once: Copy linkLink copied to clipboard!
from("twitter-search://foo?type=polling&keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]") .to("bean:blah");
from("twitter-search://foo?type=polling&keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]")
.to("bean:blah");
349.5.4. Searching using a producer with static keywords: Copy linkLink copied to clipboard!
from("direct:foo") .to("twitter-search://foo?keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
from("direct:foo")
.to("twitter-search://foo?keywords=camel&consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
349.5.5. Searching using a producer with dynamic keywords from header: Copy linkLink copied to clipboard!
In the bar
header we have the keywords we want to search, so we can assign this value to the CamelTwitterKeywords
header:
from("direct:foo") .setHeader("CamelTwitterKeywords", header("bar")) .to("twitter-search://foo?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
from("direct:foo")
.setHeader("CamelTwitterKeywords", header("bar"))
.to("twitter-search://foo?consumerKey=[s]&consumerSecret=[s]&accessToken=[s]&accessTokenSecret=[s]");
349.6. Example Copy linkLink copied to clipboard!
See also the Twitter Websocket Example.
349.7. See Also Copy linkLink copied to clipboard!
- Configuring Camel
- Component
- Endpoint
- Getting Started
- Twitter Websocket Example