List of usage examples for twitter4j RateLimitStatusListener RateLimitStatusListener
RateLimitStatusListener
From source file:com.appdynamics.alerts.twitter.TwitterAlert.java
License:Apache License
public static void main(String[] args) { twitter = TwitterFactory.getSingleton(); twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); twitter.addRateLimitStatusListener(new RateLimitStatusListener() { @Override/*from w w w. j a va 2 s.com*/ public void onRateLimitStatus(RateLimitStatusEvent rateLimitStatusEvent) { RateLimitStatus status = rateLimitStatusEvent.getRateLimitStatus(); if (status.getRemaining() <= 0) { logger.warn("Rate limit reached"); storeRateLimitStatus(status); } } @Override public void onRateLimitReached(RateLimitStatusEvent rateLimitStatusEvent) { //never called by API } }); if (args.length == 1 && args[0].equals("auth")) { try { getAuthorization(); System.out.println("Authenticated for user @" + twitter.getScreenName()); } catch (TwitterException e) { if (e.getStatusCode() == 401) { logger.error("Missing or incorrect authentication credentials"); } else { logger.error("Authentication failed", e); } } } else { try { parseEventParams(args); AccessToken accessToken = readAccessToken(); twitter.setOAuthAccessToken(accessToken); postUpdate(); } catch (IOException e) { logger.error("Access token unreadable,unable to authenticate. Please rerun authentication script"); } catch (ClassNotFoundException e) { logger.error("Access token unreadable,unable to authenticate. Please rerun authentication script"); } catch (Exception e) { logger.error("Failed to parse arguments"); } } }
From source file:org.n52.twitter.dao.TwitterAbstractDAO.java
License:Open Source License
public TwitterAbstractDAO(AccessToken accessToken, String oauthConsumerKey, String oauthConsumerSecret) { LOGGER.debug("Using the following default values: Tweets to harvest: '{}', Tweets per request: '{}'", MAX_TWEETS_TO_HARVEST, TWEETS_TO_LOAD_PER_API_REQUEST); TwitterFactory factory = new TwitterFactory(); twitter = factory.getInstance();//w ww. j a v a 2 s . c o m twitter.setOAuthConsumer(oauthConsumerKey, oauthConsumerSecret); twitter.setOAuthAccessToken(accessToken); twitter.addRateLimitStatusListener(new RateLimitStatusListener() { @Override public void onRateLimitStatus(RateLimitStatusEvent event) { LOGGER.debug("Remaining requests for the current period: {}", event.getRateLimitStatus().getRemaining()); if (event.getRateLimitStatus().getRemaining() <= 1) { try { LOGGER.debug("Going to sleep for {}s because rate limit is nearly reached.", (event.getRateLimitStatus().getSecondsUntilReset() + 5000)); Thread.sleep(event.getRateLimitStatus().getSecondsUntilReset() * 1000l + 5000); } catch (InterruptedException e) { LOGGER.error("Sleep interrupted", e); } } } @Override public void onRateLimitReached(RateLimitStatusEvent event) { } }); }