Twitter 发布了一个 Java 客户端库,并包含以下使用它的示例代码:
// Connect to the filter endpoint, tracking the term "twitterapi"
Hosts host = new BasicHost(Constants.STREAM_HOST);
StreamingEndpoint endpoint = new StatusesFilterEndpoint();
endpoint.trackTerms(Lists.newArrayList("twitterapi"));
// Drop in the oauth credentials for your app, available on dev.twitter.com
Authentication auth = new OAuth1("consumerKey", "consumerSecret",
"token", "tokenSecret");
// Initialize a queue to collect messages from the stream
BlockingQueue<String> messages = new LinkedBlockingQueue<String>(100000);
// Build a client and read messages until the connection closes.
ClientBuilder builder = new ClientBuilder()
.name("FooBarBaz-StreamingClient")
.hosts(host)
.authentication(auth)
.endpoint(endpoint)
.processor(new StringDelimitedProcessor(messages));
Client client = builder.build;
client.connect();
while (!client.isDone()) {
String message = messages.take();
// Do something with message
}
发布公告: https ://dev.twitter.com/blog/the-hosebird-client-streaming-library
尖括号是什么意思?