当前位置: 首页>>代码示例>>Java>>正文


Java BasicClient.stop方法代码示例

本文整理汇总了Java中com.twitter.hbc.httpclient.BasicClient.stop方法的典型用法代码示例。如果您正苦于以下问题:Java BasicClient.stop方法的具体用法?Java BasicClient.stop怎么用?Java BasicClient.stop使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.twitter.hbc.httpclient.BasicClient的用法示例。


在下文中一共展示了BasicClient.stop方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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

示例2: 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();
}
 
开发者ID:neo4j-examples,项目名称:neo4j-twitter-stream,代码行数:37,代码来源:TwitterStreamProcessor.java

示例3: 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();
}
 
开发者ID:twitter,项目名称:hbc,代码行数:40,代码来源:Twitter4jSampleStreamExample.java

示例4: stream

import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public Stream<TweetHandler> stream() {
        String myKey = "sl2WbCf4UnIr08xvHVitHJ99r";
        String mySecret = "PE6yauvXjKLuvoQNXZAJo5C8N5U5piSFb3udwkoI76paK6KyqI";
        String myToken = "1098376471-p6iWfxCLtyMvMutTb010w1D1xZ3UyJhcC2kkBjN";
        String myAccess = "2o1uGcp4b2bFynOfu2cA1uz63n5aruV0RwNsUjRpjDBZS";

        out.println("Creating Twitter Stream");
        BlockingQueue<String> statusQueue = new LinkedBlockingQueue<>(1000);
        StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
        endpoint.trackTerms(Lists.newArrayList("twitterapi", this.topic));
        endpoint.stallWarnings(false);
        Authentication twitterAuth = new OAuth1(myKey, mySecret, myToken, myAccess);

        BasicClient twitterClient = new ClientBuilder()
                .name("Twitter client")
                .hosts(Constants.STREAM_HOST)
                .endpoint(endpoint)
                .authentication(twitterAuth)
                .processor(new StringDelimitedProcessor(statusQueue))
                .build();

        twitterClient.connect();

        List<TweetHandler> list = new ArrayList();
        List<String> twitterList = new ArrayList();

        statusQueue.drainTo(twitterList);
        for(int i=0; i<numberOfTweets; i++) {
            String message;
            try {
                message = statusQueue.take();
                list.add(new TweetHandler(message));
            } catch (InterruptedException ex) {
                ex.printStackTrace();
            }
        }

//        for (int msgRead = 0; msgRead < this.numberOfTweets; msgRead++) {
//            try {
//                if (twitterClient.isDone()) {
//                  //  out.println(twitterClient.getExitEvent().getMessage());
//                    break;
//                }
//
//                String msg = statusQueue.poll(10, TimeUnit.SECONDS);
//                if (msg == null) {
//                    out.println("Waited 10 seconds - no message received");
//                } else {
//                    list.add(new TweetHandler(msg));
//                    out.println("Added message: " + msg.length());
//                }
//            } catch (InterruptedException ex) {
//                ex.printStackTrace();
//            }
//        }
        twitterClient.stop();
        out.printf("%d messages processed!\n", twitterClient.getStatsTracker().getNumMessages());

        return list.stream();
    }
 
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:61,代码来源:TwitterStream.java

示例5: oauth

import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public static void oauth(String consumerKey, String consumerSecret, String token, String secret) {
	// 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.
	StatusesFilterEndpoint endpoint = new StatusesFilterEndpoint();
	endpoint.trackTerms(Lists.newArrayList("storm", "precipitation", "tornado", "blizzard", "weather"));
	endpoint.stallWarnings(false);


	Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
	//Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);

	// Create a new BasicClient. By default gzip is enabled.
	BasicClient client = new ClientBuilder()
	.name("sampleExampleClient")
	.hosts(Constants.STREAM_HOST)
	.endpoint(endpoint)
	.authentication(auth)
	.processor(new StringDelimitedProcessor(queue))
	.build();

	// Establish a connection
	client.connect();
	JSONObjectParser parser = new JSONObjectParser();

	// Do whatever needs to be done with messages
	for (int msgRead = 0; msgRead < 200000; msgRead++) {
		if (client.isDone()) {
			System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
			break;
		}

		String msg;
		JSONArray jsonArray = new JSONArray();
		try {
			msg = queue.poll(5, TimeUnit.SECONDS);
			if (msg == null) {
				System.out.println("Did not receive a message in 5 seconds");
			} else {
				// TODO: we create a jsonObject that only contains fields we care about. 
				// Right now we're just dumping the whole thing.
				//System.out.println(msg);
				//build a tweet from the message
				buildTweetFromMessage(msg);

			}
		} catch (Exception e) {
			//TODO: we need to be able to restart the connection if it happens to fail. 
			e.printStackTrace();
		}
	}




	client.stop();

	// Print some stats
	System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());

}
 
开发者ID:jmaupin82,项目名称:Twitter-Analyzer,代码行数:64,代码来源:Crawler.java

示例6: run

import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public static void run(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();
  endpoint.stallWarnings(false);

  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, secret);
  //Authentication auth = new com.twitter.hbc.httpclient.auth.BasicAuth(username, password);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .name("sampleExampleClient")
          .hosts(Constants.STREAM_HOST)
          .endpoint(endpoint)
          .authentication(auth)
          .processor(new StringDelimitedProcessor(queue))
          .build();

  // Establish a connection
  client.connect();

  // Do whatever needs to be done with messages
  for (int msgRead = 0; msgRead < 1000; msgRead++) {
    if (client.isDone()) {
      System.out.println("Client connection closed unexpectedly: " + client.getExitEvent().getMessage());
      break;
    }

    String msg = queue.poll(5, TimeUnit.SECONDS);
    if (msg == null) {
      System.out.println("Did not receive a message in 5 seconds");
    } else {
      System.out.println(msg);
    }
  }

  client.stop();

  // Print some stats
  System.out.printf("The client read %d messages!\n", client.getStatsTracker().getNumMessages());
}
 
开发者ID:twitter,项目名称:hbc,代码行数:45,代码来源:SampleStreamExample.java

示例7: run

import com.twitter.hbc.httpclient.BasicClient; //导入方法依赖的package包/类
public void run(String consumerKey, String consumerSecret, String token, String tokenSecret)
        throws InterruptedException, ControlStreamException, IOException {
  // 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.
  List<Long> followings = new ArrayList<Long>();
  followings.add(111111111L);
  followings.add(222222222L);

  SitestreamEndpoint endpoint = new SitestreamEndpoint(followings);
  Authentication auth = new OAuth1(consumerKey, consumerSecret, token, tokenSecret);

  // Create a new BasicClient. By default gzip is enabled.
  BasicClient client = new ClientBuilder()
          .hosts(Constants.SITESTREAM_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
  Twitter4jSitestreamClient t4jClient = new Twitter4jSitestreamClient(
          client, queue, Lists.newArrayList(listener), 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);

  // Create a sitestream controller to issue controlstream requests
  SitestreamController controller = new SitestreamController(auth);

  controller.getFriends(t4jClient.getStreamId(), 12345L);
  controller.addUser(t4jClient.getStreamId(), 987765L);

  client.stop();
}
 
开发者ID:twitter,项目名称:hbc,代码行数:49,代码来源:SitestreamExample.java


注:本文中的com.twitter.hbc.httpclient.BasicClient.stop方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。