private void pullTweets(Key key, Twitter twitter) { try { RateLimitStatus rateLimitStatus = twitter.getRateLimitStatus(); if (rateLimitStatus.getRemainingHits() == 0) return; double currentRate = (rateLimitStatus.getHourlyLimit() - rateLimitStatus.getRemainingHits()) / (3601 - rateLimitStatus.getSecondsUntilReset()); logger.info( "Current twitter refresh rate: {}/h, official refresh rate: {}", String.format("%1.1f", currentRate), rateLimitStatus.getHourlyLimit()); if (currentRate > rateLimitStatus.getHourlyLimit()) { logger.info("Skipped refreshing Twitter feeds to limit the refresh rate"); return; } ResponseList<Status> newFriendsTimeline = twitter.getHomeTimeline(); ResponseList<Status> newMentions = twitter.getMentions(); synchronized (this) { mergeStatuses(timeline.get(key), newFriendsTimeline); mergeStatuses(mentions.get(key), newMentions); } } catch (TwitterException e) { logger.warn("Twitter reported an error: " + e.getMessage(), e); } }
protected boolean handleReceivingRateLimitStatus(RateLimitStatus rateLimitStatus) { try { int secondsUntilReset = rateLimitStatus.getSecondsUntilReset(); int remainingHits = rateLimitStatus.getRemainingHits(); if (remainingHits == 0) { logger.debug("rate status limit service returned 0 for the remaining hits value"); return false; } if (secondsUntilReset == 0) { logger.debug( "rate status limit service returned 0 for the seconds until reset period value"); return false; } int secondsUntilWeCanPullAgain = secondsUntilReset / remainingHits; long msUntilWeCanPullAgain = secondsUntilWeCanPullAgain * 1000; logger.debug( "need to Thread.sleep() " + secondsUntilWeCanPullAgain + " seconds until the next timeline pull. Have " + remainingHits + " remaining pull this rate period. The period ends in " + secondsUntilReset); Thread.sleep(msUntilWeCanPullAgain); } catch (Throwable throwable) { logger.debug( "encountered an error when" + " trying to refresh the timeline: " + ExceptionUtils.getFullStackTrace(throwable)); } return true; }