本文整理汇总了Java中com.twitter.hbc.httpclient.BasicClient类的典型用法代码示例。如果您正苦于以下问题:Java BasicClient类的具体用法?Java BasicClient怎么用?Java BasicClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
BasicClient类属于com.twitter.hbc.httpclient包,在下文中一共展示了BasicClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: configureStreamClient
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
private static BasicClient configureStreamClient(BlockingQueue<String> msgQueue, String twitterKeys, List<Long> userIds, List<String> terms) {
Hosts hosts = new HttpHosts(Constants.STREAM_HOST);
StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint()
.followings(userIds)
.trackTerms(terms);
endpoint.stallWarnings(false);
String[] keys = twitterKeys.split(":");
Authentication auth = new OAuth1(keys[0], keys[1], keys[2], keys[3]);
ClientBuilder builder = new ClientBuilder()
.name("Neo4j-Twitter-Stream")
.hosts(hosts)
.authentication(auth)
.endpoint(endpoint)
.processor(new StringDelimitedProcessor(msgQueue));
return builder.build();
}
示例2: testInitialize_withRequiredInput
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Test case for {@link TwitterStreamSource#initialize(java.util.Properties)} being provided
* properties showing all required input
*/
@Test
public void testInitialize_withRequiredInput() throws Exception {
Properties props = new Properties();
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_KEY, "ckey");
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_SECRET, "csecret");
props.put(TwitterStreamSource.CFG_TWITTER_PROFILES, "");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_KEY, "tkey");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_SECRET, "tsecret");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_LANGUAGES, "");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_SEARCH_TERMS, "");
TwitterStreamSource source = new TwitterStreamSource();
source.setId("test-id");
source.setTwitterClient(Mockito.mock(BasicClient.class));
source.initialize(props);
}
示例3: setup
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
@Before
public void setup() throws IOException {
mockClient = mock(BasicClient.class);
queue = new LinkedBlockingQueue<String>();
executor = mock(ExecutorService.class);
t4jClient = spy(new BaseTwitter4jClient(mockClient, queue, executor));
status = reader.readFile("status.json");
user = reader.readFile("user.json");
statusDeletionNotice = reader.readFile("status-deletion.json");
limit = reader.readFile("limit.json");
scrubGeo = reader.readFile("scrub-geo.json");
friendsList = reader.readFile("friends-list.json");
disconnectMessage = reader.readFile("disconnect-message.json");
controlMessage = reader.readFile("control-message.json");
directMessage = reader.readFile("direct-message.json");
directMessageDelete = reader.readFile("direct-message-delete.json");
}
示例4: streamTwitter
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
public static void streamTwitter(String consumerKey, String consumerSecret, String accessToken, String accessSecret) throws InterruptedException {
BlockingQueue<String> statusQueue = new LinkedBlockingQueue<String>(10000);
StatusesSampleEndpoint ending = new StatusesSampleEndpoint();
ending.stallWarnings(false);
Authentication twitterAuth = new OAuth1(consumerKey, consumerSecret, accessToken, accessSecret);
BasicClient twitterClient = new ClientBuilder()
.name("Twitter client")
.hosts(Constants.STREAM_HOST)
.endpoint(ending)
.authentication(twitterAuth)
.processor(new StringDelimitedProcessor(statusQueue))
.build();
twitterClient.connect();
for (int msgRead = 0; msgRead < 1000; msgRead++) {
if (twitterClient.isDone()) {
System.out.println(twitterClient.getExitEvent().getMessage());
break;
}
String msg = statusQueue.poll(10, TimeUnit.SECONDS);
if (msg == null) {
System.out.println("Waited 10 seconds - no message received");
} else {
System.out.println(msg);
}
}
twitterClient.stop();
System.out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:40,代码来源:SampleStreamExample.java
示例5: main
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
public static void main(String... args) throws InterruptedException, MalformedURLException, URISyntaxException {
int maxReads = 1000000;
BlockingQueue<String> msgQueue = new LinkedBlockingQueue<String>(10000);
List<Long> userIds = asList();
String searchTerms = getenv("TWITTER_TERMS") != null ? getenv("TWITTER_TERMS") : "happy";
List<String> terms = asList(searchTerms.split(","));
BasicClient client = configureStreamClient(msgQueue, getenv("TWITTER_KEYS"), userIds, terms);
TwitterNeo4jWriter writer = new TwitterNeo4jWriter(getenv("NEO4J_URL"));
writer.init();
int numProcessingThreads = Math.max(1,Runtime.getRuntime().availableProcessors() - 1);
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
client.connect();
List<String> buffer = new ArrayList<>(BATCH);
for (int msgRead = 0; msgRead < maxReads; msgRead++) {
if (client.isDone()) {
System.err.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
break;
}
String msg = msgQueue.poll(5, TimeUnit.SECONDS);
if (msg == null) System.out.println("Did not receive a message in 5 seconds");
else buffer.add(msg);
if (buffer.size() < BATCH) continue;
List<String> tweets = buffer;
service.submit(() -> writer.insert(tweets,3));
buffer = new ArrayList<>(BATCH);
}
client.stop();
writer.close();
}
示例6: setup
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
@Before
public void setup() {
this.client = mock(BasicClient.class);
this.cb = mock(ClientBuilder.class);
when(cb.name("Twitter Api Reader")).thenReturn(cb);
when(cb.hosts(Constants.STREAM_HOST)).thenReturn(cb);
when(cb.endpoint(any(StreamingEndpoint.class))).thenReturn(cb);
when(cb.authentication(any(Authentication.class))).thenReturn(cb);
when(cb.processor(any(HosebirdMessageProcessor.class))).thenReturn(cb);
when(cb.retries(10)).thenReturn(cb);
when(cb.build()).thenReturn(client);
this.logger = mock(Logger.class);
this.clientReadAndSendPredicate = new ReadAndSendPredicate() {
@Override
public boolean process() {
return !client.isDone();
}
};
this.config = new TwitterApiReaderConfig();
this.config.hosebird = new HosebirdConfig();
this.config.twitterapi = new TwitterApiConfig();
this.config.twitterapi.consumerKey = "aaaaa";
this.config.twitterapi.consumerSecret = "bbbbb";
this.config.twitterapi.accessToken = "ccccc";
this.config.twitterapi.accessSecret = "ddddd";
this.config.hosebird.retries = 10;
this.config.hosebird.bufferSize = 10000;
this.config.kafka = new KafkaConfig();
this.config.kafka.topic = "Data";
this.config.metrics = new MetricsConfig();
this.config.metrics.host = "G_HOST";
this.config.metrics.port = 1111;
this.config.metrics.prefix = "G_PREFIX";
this.config.metrics.reportingTime = 2;
}
示例7: logClientExitReason
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Logs the reason that the Hosebird client said it was done
* by examining the exit event.
* @param client the Hosebird client
*/
@VisibleForTesting
@SuppressWarnings("checkstyle:designforextension")
protected void logClientExitReason(final Client client) {
BasicClient bc = (BasicClient) client;
Event e = bc.getExitEvent();
log.error(
"Hosebird client stopped: {} {}",
new Object[]{
e.getEventType().name(),
e.getMessage()});
}
示例8: setup
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
@Before
public void setup() {
this.client = mock(BasicClient.class);
this.cb = mock(ClientBuilder.class);
when(cb.name("Gnip Reader")).thenReturn(cb);
when(cb.hosts(Constants.ENTERPRISE_STREAM_HOST)).thenReturn(cb);
when(cb.endpoint(any(StreamingEndpoint.class))).thenReturn(cb);
when(cb.authentication(any(Authentication.class))).thenReturn(cb);
when(cb.processor(any(HosebirdMessageProcessor.class))).thenReturn(cb);
when(cb.retries(10)).thenReturn(cb);
when(cb.build()).thenReturn(client);
this.logger = mock(Logger.class);
this.clientReadAndSendPredicate = new ReadAndSendPredicate() {
@Override
public boolean process() {
return !client.isDone();
}
};
this.config = new ConcreteHosebirdConfig();
this.config.hosebird = new HosebirdConfig();
this.config.hosebird.retries = 10;
this.config.hosebird.bufferSize = 10000;
this.config.kafka = new KafkaConfig();
this.config.kafka.topic = "Data";
this.config.metrics = new MetricsConfig();
this.config.metrics.host = "G_HOST";
this.config.metrics.port = 1111;
this.config.metrics.prefix = "G_PREFIX";
this.config.metrics.reportingTime = 2;
}
示例9: setup
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
@Before
public void setup() {
this.client = mock(BasicClient.class);
this.cb = mock(ClientBuilder.class);
when(cb.name("Gnip Reader")).thenReturn(cb);
when(cb.hosts(Constants.ENTERPRISE_STREAM_HOST_v2)).thenReturn(cb);
when(cb.endpoint(any(StreamingEndpoint.class))).thenReturn(cb);
when(cb.authentication(any(Authentication.class))).thenReturn(cb);
when(cb.processor(any(HosebirdMessageProcessor.class))).thenReturn(cb);
when(cb.retries(10)).thenReturn(cb);
when(cb.build()).thenReturn(client);
this.logger = mock(Logger.class);
this.clientReadAndSendPredicate = new ReadAndSendPredicate() {
@Override
public boolean process() {
return !client.isDone();
}
};
this.config = new GnipReaderConfig();
this.config.gnip = new GnipConfig();
this.config.hosebird = new HosebirdConfig();
this.config.gnip.account = "ACCOUNT";
this.config.gnip.product = "PRODUCT";
this.config.gnip.label = "LABEL";
this.config.gnip.username = "USERNAME";
this.config.gnip.password = "PASSWORD";
this.config.hosebird.retries = 10;
this.config.hosebird.bufferSize = 10000;
this.config.kafka = new KafkaConfig();
this.config.kafka.topic = "Data";
this.config.metrics = new MetricsConfig();
this.config.metrics.host = "G_HOST";
this.config.metrics.port = 1111;
this.config.metrics.prefix = "G_PREFIX";
this.config.metrics.reportingTime = 2;
}
示例10: testRun_withTwitterClientWhichIsDone
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Test case for {@link TwitterStreamSource#run()} with a
* twitter client showing true when calling isDone
*/
@Test
public void testRun_withTwitterClientWhichIsDone() throws Exception {
BasicClient twitterClientMock = Mockito.mock(BasicClient.class);
Mockito.when(twitterClientMock.isDone()).thenReturn(true);
Properties props = new Properties();
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_KEY, "consumer-key");
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_SECRET, "consumer-secret");
props.put(TwitterStreamSource.CFG_TWITTER_PROFILES, "1");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_KEY, "token-key");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_SECRET, "token-secret");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_LANGUAGES, "en");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_SEARCH_TERMS, "soccer");
TwitterStreamSource consumer = new TwitterStreamSource();
consumer.setId("test-id");
consumer.initialize(props);
consumer.setTwitterClient(twitterClientMock);
consumer.getStreamMessageQueue().offer("test-message");
Assert.assertFalse("Value must be true", consumer.getStreamMessageQueue().isEmpty());
executor.submit(consumer);
Thread.sleep(100);
consumer.run();
Assert.assertFalse("Value must be true", consumer.getStreamMessageQueue().isEmpty());
Assert.assertEquals("Values must be equal", 0, consumer.getMessageCount());
}
示例11: testRun_withIsRunningSetToFalse
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Test case for {@link TwitterStreamSource#run()} where isRunning shows false
*/
@Test
public void testRun_withIsRunningSetToFalse() throws Exception {
BasicClient twitterClientMock = Mockito.mock(BasicClient.class);
IncomingMessageCallback callback = Mockito.mock(IncomingMessageCallback.class);
Mockito.when(twitterClientMock.isDone()).thenReturn(false);
Properties props = new Properties();
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_KEY, "consumer-key");
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_SECRET, "consumer-secret");
props.put(TwitterStreamSource.CFG_TWITTER_PROFILES, "1");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_KEY, "token-key");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_SECRET, "token-secret");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_LANGUAGES, "en");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_SEARCH_TERMS, "soccer");
TwitterStreamSource consumer = new TwitterStreamSource();
consumer.setId("test-id");
consumer.setTwitterClient(twitterClientMock);
consumer.initialize(props);
consumer.setIncomingMessageCallback(callback);
consumer.setRunning(false);
String msgContent = "{\"content\":\"test-message\", \"timestamp_ms\":\""+System.currentTimeMillis()+"\"}";
consumer.getStreamMessageQueue().offer(msgContent);
Assert.assertFalse("Value must be false", consumer.getStreamMessageQueue().isEmpty());
executor.submit(consumer);
Thread.sleep(100);
consumer.shutdown();
Assert.assertEquals("Must show one element as none were consumed", 1, consumer.getStreamMessageQueue().size());
Assert.assertEquals("Values must be equal", 0, consumer.getMessageCount());
}
示例12: testRun_withAtLeastOneIteration
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Test case for {@link TwitterStreamSource#run()} with at least one iteration
*/
@Test
public void testRun_withAtLeastOneIteration() throws Exception {
IncomingMessageCallback callback = Mockito.mock(IncomingMessageCallback.class);
BasicClient twitterClientMock = Mockito.mock(BasicClient.class);
Mockito.when(twitterClientMock.isDone()).thenReturn(false);
Properties props = new Properties();
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_KEY, "consumer-key");
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_SECRET, "consumer-secret");
props.put(TwitterStreamSource.CFG_TWITTER_PROFILES, "1");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_KEY, "token-key");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_SECRET, "token-secret");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_LANGUAGES, "en");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_SEARCH_TERMS, "soccer");
TwitterStreamSource consumer = new TwitterStreamSource();
consumer.setId("test-id");
consumer.setTwitterClient(twitterClientMock);
consumer.setIncomingMessageCallback(callback);
consumer.initialize(props);
consumer.getStreamMessageQueue().offer("{\"content\":\"test-message\", \"timestamp_ms\":\""+System.currentTimeMillis()+"\"}");
Assert.assertFalse("Value must be false", consumer.getStreamMessageQueue().isEmpty());
executor.submit(consumer);
Thread.sleep(100);
consumer.shutdown();
Assert.assertTrue("Value must be true", consumer.getStreamMessageQueue().isEmpty());
Assert.assertEquals("Values must be equal", 1, consumer.getMessageCount());
}
示例13: testRun_withOneValidOneInvalidMessage
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
/**
* Test case for {@link TwitterStreamSource#run()} with one valid and one invalid message. Must show no errors
* as the source does not care about the message format
*/
@Test
public void testRun_withOneValidOneInvalidMessage() throws Exception {
IncomingMessageCallback callback = Mockito.mock(IncomingMessageCallback.class);
BasicClient twitterClientMock = Mockito.mock(BasicClient.class);
Mockito.when(twitterClientMock.isDone()).thenReturn(false);
Properties props = new Properties();
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_KEY, "consumer-key");
props.put(TwitterStreamSource.CFG_TWITTER_CONSUMER_SECRET, "consumer-secret");
props.put(TwitterStreamSource.CFG_TWITTER_PROFILES, "1");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_KEY, "token-key");
props.put(TwitterStreamSource.CFG_TWITTER_TOKEN_SECRET, "token-secret");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_LANGUAGES, "en");
props.put(TwitterStreamSource.CFG_TWITTER_TWEET_SEARCH_TERMS, "soccer");
TwitterStreamSource consumer = new TwitterStreamSource();
consumer.setTwitterClient(twitterClientMock);
consumer.setId("testRun_withOneValidOneInvalidMessage");
consumer.initialize(props);
consumer.setIncomingMessageCallback(callback);
consumer.getStreamMessageQueue().offer("{\"content\":\"test-message\", \"timestamp_ms\":\""+System.currentTimeMillis()+"\"}");
consumer.getStreamMessageQueue().offer("noJsonContent");
Assert.assertEquals("Values must be equal", 2, consumer.getStreamMessageQueue().size());
executor.submit(consumer);
Thread.sleep(200);
consumer.shutdown();
Assert.assertTrue("Value must be true", consumer.getStreamMessageQueue().isEmpty());
Assert.assertEquals("Values must be equal", 2, consumer.getMessageCount());
}
示例14: build
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
public BasicClient build() {
HttpParams params = new BasicHttpParams();
if (proxyHost != null) {
HttpHost proxy = new HttpHost(proxyHost, proxyPort);
params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
}
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setUserAgent(params, USER_AGENT);
HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);
HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
return new BasicClient(name, hosts, endpoint, auth, enableGZip, processor, reconnectionManager,
rateTracker, executorService, eventQueue, params, schemeRegistry);
}
示例15: oauth
import com.twitter.hbc.httpclient.BasicClient; //导入依赖的package包/类
public void oauth(String consumerKey, String consumerSecret, String token, String secret) throws InterruptedException {
// Create an appropriately sized blocking queue
BlockingQueue<String> queue = new LinkedBlockingQueue<String>(10000);
// Define our endpoint: By default, delimited=length is set (we need this for our processor)
// and stall warnings are on.
StatusesSampleEndpoint endpoint = new StatusesSampleEndpoint();
Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
// Authentication auth = new BasicAuth(username, password);
// Create a new BasicClient. By default gzip is enabled.
BasicClient client = new ClientBuilder()
.hosts(Constants.STREAM_HOST)
.endpoint(endpoint)
.authentication(auth)
.processor(new StringDelimitedProcessor(queue))
.build();
// Create an executor service which will spawn threads to do the actual work of parsing the incoming messages and
// calling the listeners on each message
int numProcessingThreads = 4;
ExecutorService service = Executors.newFixedThreadPool(numProcessingThreads);
// Wrap our BasicClient with the twitter4j client
Twitter4jStatusClient t4jClient = new Twitter4jStatusClient(
client, queue, Lists.newArrayList(listener1, listener2), service);
// Establish a connection
t4jClient.connect();
for (int threads = 0; threads < numProcessingThreads; threads++) {
// This must be called once per processing thread
t4jClient.process();
}
Thread.sleep(5000);
client.stop();
}