Twitter Search
Since Camel 2.10
Both producer and consumer are supported
The Twitter Search component consumes search results.
Usage
The credentials of the Twitter application are component options, for example in application.properties:
camel.component.twitter-search.consumer-key = {{env:TWITTER_CONSUMER_KEY}}
camel.component.twitter-search.consumer-secret = {{env:TWITTER_CONSUMER_SECRET}}
camel.component.twitter-search.access-token = {{env:TWITTER_ACCESS_TOKEN}}
camel.component.twitter-search.access-token-secret = {{env:TWITTER_ACCESS_TOKEN_SECRET}} The following route polls the search every minute, and logs the text of each tweet:
- route:
from:
uri: twitter-search:apachecamel
parameters:
delay: 60000
lang: en
steps:
- log:
message: "${body.text}" A producer searches once for each message, and the body is the list of tweets. The CamelTwitterKeywords header overrides the keywords of the endpoint:
- route:
from:
uri: direct:search
steps:
- setHeader:
name: CamelTwitterKeywords
expression:
simple:
expression: "${body}"
- to:
uri: twitter-search:camel
- log:
message: "Found ${body.size()} tweets" Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Twitter Search component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
Used for enabling or disabling all consumer based health checks from this component. | true | boolean | |
Used for enabling or disabling all producer based health checks from this component. Notice: Camel has by default disabled all producer based health-checks. You can turn on producer checks globally by setting camel.health.producersEnabled=true. | true | boolean | |
The host name of an HTTP proxy to call the Twitter API through, for all endpoints of the component. When set, httpProxyPort must be set too. An endpoint can override it. | String | ||
The password of httpProxyUser on the HTTP proxy, for all endpoints of the component. An endpoint can override it. | String | ||
The port of the HTTP proxy, required when httpProxyHost is set, for all endpoints of the component. An endpoint can override it. | int | ||
The user name to authenticate on the HTTP proxy with, together with httpProxyPassword, for all endpoints of the component. An endpoint can override it. | String | ||
The OAuth access token of the Twitter account that the endpoints act as, for all endpoints of the component. An endpoint can override it. | String | ||
The OAuth access token secret that goes with accessToken, for all endpoints of the component. An endpoint can override it. | String | ||
The OAuth consumer key (API key) of the Twitter app, for all endpoints of the component. An endpoint can override it. The four consumerKey, consumerSecret, accessToken and accessTokenSecret options are required, here or on the endpoint. | String | ||
The OAuth consumer secret (API key secret) of the Twitter app, for all endpoints of the component. An endpoint can override it. | String |
Endpoint Options
The Twitter Search endpoint is configured using URI syntax:
twitter-search:keywords
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
If the polling consumer did not poll any files, you can enable this option to send an empty message (no body) instead. | false | boolean | |
How the consumer reads. With polling, each poll of the scheduler (see delay) reads the tweets or direct messages (sinceId and filterOld say which ones). With direct, the consumer also reads once when it starts, without a since id, and the scheduler then polls as well, without starting after sinceId. The producer does not use it. Enum values:
| polling | EndpointType | |
Allows for bridging the consumer to the Camel routing Error Handler, which mean any exceptions (if possible) occurred while the Camel consumer is trying to pickup incoming messages, or the likes, will now be processed as a message and handled by the routing Error Handler. Important: This is only possible if the 3rd party component allows Camel to be alerted if an exception was thrown. Some components handle this internally only, and therefore bridgeErrorHandler is not possible. In other situations we may improve the Camel component to hook into the 3rd party component and make this possible for future releases. By default the consumer will use the org.apache.camel.spi.ExceptionHandler to deal with exceptions, that will be logged at WARN or ERROR level and ignored. | false | boolean | |
The unit of radius in a geographic search: km for kilometers or mi for miles. Set it together with radius: the twitter-search consumer does not apply the default km, and the poll fails when it is not set. Enum values:
| km | String | |
To let the consumer use a custom ExceptionHandler. Notice if the option bridgeErrorHandler is enabled then this option is not in use. By default the consumer will deal with exceptions, that will be logged at WARN or ERROR level and ignored. | ExceptionHandler | ||
Sets the exchange pattern when the consumer creates an exchange. Enum values:
| ExchangePattern | ||
Whether every Twitter API call of the endpoint asks for the extended mode of tweets (tweet_mode=extended), so that getText() of a tweet returns its full text, also beyond 140 characters, instead of a truncated text. | true | boolean | |
The latitude, in decimal degrees, of the center of the area of a geographic search. A twitter-search consumer sends the geocode of the search only when latitude, longitude and radius are all set. The twitter-search producer and the other schemes do not use it. | Double | ||
Not used: no twitter-directmessage, twitter-search or twitter-timeline endpoint reads this option. | String | ||
The longitude, in decimal degrees, of the center of the area of a geographic search. A twitter-search consumer sends the geocode of the search only when latitude, longitude and radius are all set. The twitter-search producer and the other schemes do not use it. | Double | ||
A pluggable org.apache.camel.PollingConsumerPollingStrategy allowing you to provide your custom implementation to control error handling usually occurred during the poll operation before an Exchange have been created and being routed in Camel. | PollingConsumerPollStrategy | ||
The radius, in the unit of distanceMetric, of the area of a geographic search around latitude and longitude. A twitter-search consumer sends the geocode of the search only when latitude, longitude and radius are all set. The twitter-search producer and the other schemes do not use it. | Double | ||
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The number of results to request per call (the count of the Twitter API): the tweets per page of a twitter-search search, the tweets per poll of a twitter-timeline polling consumer, and the direct messages per request of a twitter-directmessage consumer. It must be 1 or more. The CamelTwitterCount header overrides it for the twitter-search producer. | 5 | Integer | |
Whether to skip what an earlier poll already returned, by keeping the highest id seen (in memory only, starting from sinceId). A twitter-search consumer sends that id as the since id of the search, so each poll returns newer tweets only; without it each poll repeats the whole search. A twitter-directmessage consumer drops the direct messages up to that id. It has no effect on a twitter-timeline consumer, which always polls with the since id of the newest tweet seen, nor on the twitter-search producer, which always sends the sinceId option. | true | boolean | |
Restricts the search to tweets in this language, given as an ISO 639-1 code such as en (the lang of the Twitter search). Only twitter-search uses it; the CamelTwitterSearchLanguage header overrides it for the producer. | String | ||
For twitter-search, the number of pages of results to read per poll or per producer call: each further page is one more search request, and the reading stops early when there is no next page. A twitter-timeline polling consumer sends it as the page of the timeline request, so it selects which page to read, not how many. The CamelTwitterNumberOfPages header overrides it for the twitter-search producer. twitter-directmessage does not use it. | 1 | Integer | |
The id to start after: only tweets or direct messages with a greater id are read (the since_id of the Twitter API), for example to resume after a restart. A polling consumer starts from it and then moves on to the newest id it receives: twitter-search sends it as the since id when filterOld is on, twitter-timeline always sends it (it must be 1 or more), and twitter-directmessage drops the direct messages up to it when filterOld is on and it is above 1. The twitter-search producer sends it with every search unless the CamelTwitterSinceId header is set. A direct consumer does not use it. | 1 | long | |
Not used: no twitter-directmessage, twitter-search or twitter-timeline endpoint reads this option. | String | ||
The host name of an HTTP proxy to call the Twitter API through. When set, httpProxyPort must be set too. It overrides the httpProxyHost of the component. | String | ||
The password of httpProxyUser on the HTTP proxy. It overrides the httpProxyPassword of the component. | String | ||
The port of the HTTP proxy, required when httpProxyHost is set. It overrides the httpProxyPort of the component. | Integer | ||
The user name to authenticate on the HTTP proxy with, together with httpProxyPassword. It overrides the httpProxyUser of the component. | String | ||
The number of subsequent error polls (failed due some error) that should happen before the backoffMultipler should kick-in. | int | ||
The number of subsequent idle polls that should happen before the backoffMultipler should kick-in. | int | ||
To let the scheduled polling consumer backoff if there has been a number of subsequent idles/errors in a row. The multiplier is then the number of polls that will be skipped before the next actual attempt is happening again. When this option is in use then backoffIdleThreshold and/or backoffErrorThreshold must also be configured. | int | ||
Milliseconds between two polls of the consumer, or in the unit of the timeUnit option. The Twitter endpoints default to 30 seconds. | 30000 | long | |
If greedy is enabled, then the ScheduledPollConsumer will run immediately again, if the previous run polled 1 or more messages. | false | boolean | |
How long a poll may run before the health check of the consumer reports DOWN. The health check is otherwise based on the polls that are done, so it stays UP while a poll hangs, for example on a read without timeout from a connection that went silent. A poll includes routing the messages it polled, so set this above the longest poll you expect. The poll is not interrupted. The value is in millis (not affected by timeUnit). The default 0 turns this off. | 0 | long | |
Milliseconds before the first poll starts. | 1000 | long | |
Specifies a maximum limit of number of fires. So if you set it to 1, the scheduler will only fire once. If you set it to 5, it will only fire five times. A value of zero or negative means fire forever. | 0 | long | |
The consumer logs a start/complete log line when it polls. This option allows you to configure the logging level for that. Enum values:
| TRACE | LoggingLevel | |
Allows for configuring a custom/shared thread pool to use for the consumer. By default each consumer has its own single threaded thread pool. | ScheduledExecutorService | ||
To use a cron scheduler from either camel-spring or camel-quartz component. Use value spring or quartz for built in scheduler. | none | Object | |
To configure additional properties when using a custom scheduler or any of the Quartz, Spring based scheduler. This is a multi-value option with prefix: scheduler. | Map | ||
Whether the scheduler should be auto started. | true | boolean | |
Time unit for initialDelay and delay options. Enum values:
| MILLISECONDS | TimeUnit | |
Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details. | true | boolean | |
The OAuth access token of the Twitter account that the endpoint acts as. It overrides the accessToken of the component. The four consumerKey, consumerSecret, accessToken and accessTokenSecret options are required. | String | ||
The OAuth access token secret that goes with accessToken. It overrides the accessTokenSecret of the component. | String | ||
The OAuth consumer key (API key) of the Twitter app. It overrides the consumerKey of the component. The four consumerKey, consumerSecret, accessToken and accessTokenSecret options are required. | String | ||
The OAuth consumer secret (API key secret) of the Twitter app. It overrides the consumerSecret of the component. | String | ||
Whether a consumer sorts the tweets of each poll by id, so the oldest is routed first. It sorts the tweets of twitter-search and twitter-timeline consumers; the direct messages of twitter-directmessage keep the order of the Twitter API, newest first. The twitter-search producer does not sort its list. | true | boolean |
Message Headers
The Twitter Search component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
| Constant: | The search query that the producer uses instead of the keywords of the endpoint. | String | |
CamelTwitterSearchLanguage (common) Constant: | The language, as an ISO 639-1 code such as en, that the producer restricts the search to, instead of the lang option. | String | |
| Constant: | The number of tweets per page of results that the producer requests, instead of the count option. | Integer | |
CamelTwitterNumberOfPages (common) Constant: | The number of pages of results that the producer reads, instead of the numberOfPages option. | Integer | |
| Constant: | The since id of the search of the producer, instead of the sinceId option: only tweets with a greater id are returned. | Long | |
| Constant: | The max id of the search of the producer: only tweets with an id up to this one are returned, for example to page back through older tweets. | Long | |
CamelTwitterEventType (common) Constant: | What the consumer received: STATUS for a tweet of twitter-search or twitter-timeline, DIRECT_MESSAGE for a direct message of twitter-directmessage. | String |
Message Body
The message body at each side of the Twitter Search component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Consumer |
| One tweet found by the search per exchange, with its text in getText(). |
Producer input |
| The body is not read: the search uses the keywords of the endpoint, or of the CamelTwitterKeywords header. |
Producer output |
| The tweets found, an empty List when there are none. |
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.