Java Reference
In-Depth Information
public void setUserId(String userId) {
this.userId = userId;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Twitter getTwitter() {
return twitter;
}
public void setTwitter(Twitter twitter) {
this.twitter = twitter;
}
}
The bulk of the class is in the accessors and mutators for configuration. The class takes a
userId
and
password
, or can be configured with an instance of Twitter4J's
Twitter
class (which itself has
userId
and
password
properties). The
read()
method is the crux of the implementation. It attempts to take items
off of the
Queue
, which it then returns. If it finds there's nothing to return_which it will when it either is
first run or has exhausted all the cache items_it will attempt a query on the API. The API surfaces a
Paging
object, which works something like
Criteria
in Hibernate. You can configure how many results
to return using the
count
property. The most interesting option is called the
sinceId
, which lets you
search for all records occurring after the
Status
having the
ID
equal to the value given as the
sinceId
. It's
a built-in duplication-prevention mechanism.
This means that while you are caching
Status
updates in the
MessageSource
, you can bound the
growth because you don't have to forever store every previous message and test each new one for
equality. This implementation does note the last ID that was processed and returned from the
read()
method. This value is then used in any subsequent queries to preclude it, and any
Status
es before it,
from appearing in the search results.
Note that there is no support in this implementation for durably storing the ID of the last read status
between runtimes. That is, if you kill the Java process running Spring Integration, this
MessageSource
will
simply start again with the last 100 messages from the instant it makes the query, regardless of whether
they've been read or not in a previous or concurrent process. Care should be taken to either guard that
state in something durable and transactional like a database, or at the very least to implement duplicate
detection further on down the line.
A quick test of the component might look like the following:
public static void main(String[] args) throws Throwable {
ClassPathXmlApplicationContext classPathXmlApplicationContext =
new ClassPathXmlApplicationContext( "solution031.xml");
classPathXmlApplicationContext.start();
DirectChannel channel = (DirectChannel) classPathXmlApplicationContext
.getBean("inboundTweets");
Search WWH ::
Custom Search