INT-1553 second round of refatcoting to align with Spring Social, removed direct dependency on RateLimit

This commit is contained in:
Oleg Zhurakousky
2010-11-08 15:19:14 -05:00
parent 8c33858806
commit 49fc149e63
8 changed files with 72 additions and 34 deletions

View File

@@ -13,7 +13,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.config;
import org.springframework.integration.config.xml.AbstractIntegrationNamespaceHandler;

View File

@@ -1,17 +1,17 @@
/*
* Copyright 2010 the original author or authors.
* Copyright 2002-2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;

View File

@@ -1,5 +1,17 @@
/**
*
/*
* Copyright 2002-2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;
@@ -59,7 +71,7 @@ public class Twitter4jTemplate implements TwitterOperations{
throw new TwitterOperationException("Failed to obtain profile id ", e);
}
}
@Override
//@Override
public RateLimitStatus getRateLimitStatus() {
try {
return twitter.getRateLimitStatus();
@@ -167,6 +179,10 @@ public class Twitter4jTemplate implements TwitterOperations{
}
}
public Twitter getUnderlyingTwitter(){
return this.twitter;
}
private List<Tweet> buildTweetsFromTwitterResponses(List<?> responses){
List<Tweet> tweets = new LinkedList<Tweet>();
if (responses != null){

View File

@@ -1,15 +1,24 @@
/**
*
/*
* Copyright 2002-2010 the original author or authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.integration.twitter.core;
import java.util.List;
import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.Status;
import twitter4j.StatusUpdate;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
@@ -20,8 +29,6 @@ public interface TwitterOperations {
String getProfileId();
RateLimitStatus getRateLimitStatus();
List<Tweet> getDirectMessages();
List<Tweet> getDirectMessages(Paging paging);
@@ -39,4 +46,6 @@ public interface TwitterOperations {
void sendDirectMessage(int userId, String text);
void updateStatus(Tweet status);
Twitter getUnderlyingTwitter();
}

View File

@@ -34,6 +34,7 @@ import org.springframework.integration.store.MetadataStore;
import org.springframework.integration.store.SimpleMetadataStore;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.integration.twitter.core.Tweet;
import org.springframework.integration.twitter.core.Twitter4jTemplate;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -148,7 +149,10 @@ public abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint
protected void doStart(){
Assert.notNull(this.twitter, "'twitter' instance can't be null");
historyWritingPostProcessor.setTrackableComponent(this);
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(this.twitter);
// temporarily injecting Twitter into a trigger so it can deal with Rate Limits. will be changed
// once we switch to Spring Social
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(this.twitter.getUnderlyingTwitter());
//
Runnable apiCallback = this.getApiCallback();
twitterUpdatePollingTask = this.getTaskScheduler().schedule(apiCallback, trigger);
}

View File

@@ -19,12 +19,14 @@ import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.SchedulingException;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.util.Assert;
import twitter4j.RateLimitStatus;
import twitter4j.Twitter;
import twitter4j.TwitterException;
/**
* @author Oleg Zhurakousky
@@ -32,9 +34,9 @@ import twitter4j.RateLimitStatus;
*/
class RateLimitStatusTrigger implements Trigger {
protected final Log logger = LogFactory.getLog(getClass());
private TwitterOperations twitter;
private Twitter twitter;
public RateLimitStatusTrigger(TwitterOperations twitter){
public RateLimitStatusTrigger(Twitter twitter){
Assert.notNull(twitter, "'twitter' must not be null");
this.twitter = twitter;
}
@@ -46,7 +48,7 @@ class RateLimitStatusTrigger implements Trigger {
if (triggerContext.lastCompletionTime() == null){
return new Date(System.currentTimeMillis());
}
// try {
try {
RateLimitStatus rateLimitStatus = twitter.getRateLimitStatus();
int secondsUntilReset = rateLimitStatus.getSecondsUntilReset();
int remainingHits = rateLimitStatus.getRemainingHits();
@@ -67,8 +69,8 @@ class RateLimitStatusTrigger implements Trigger {
" remaining pull this rate period. The period ends in " +
secondsUntilReset);
return new Date(System.currentTimeMillis() + msUntilWeCanPullAgain);
// } catch (TwitterException e) {
// throw new SchedulingException("Failed to schedule the next Twitter update", e);
// }
} catch (TwitterException e) {
throw new SchedulingException("Failed to schedule the next Twitter update", e);
}
}
}

View File

@@ -38,6 +38,7 @@ import twitter4j.DirectMessage;
import twitter4j.Paging;
import twitter4j.RateLimitStatus;
import twitter4j.ResponseList;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
@@ -61,13 +62,15 @@ public class DirectMessageReceivingMessageSourceTests {
when(secondMessage.getCreatedAt()).thenReturn(new Date(2222222222L));
when(secondMessage.getId()).thenReturn((long) 2000);
when(twitter.getProfileId()).thenReturn("kermit");
Twitter tw = mock(Twitter.class);
when(twitter.getUnderlyingTwitter()).thenReturn(tw);
}
@Test
public void testSuccessfullInitialization() throws Exception{
DirectMessageReceivingMessageSource source = new DirectMessageReceivingMessageSource(twitter);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.afterPropertiesSet();
@@ -105,7 +108,9 @@ public class DirectMessageReceivingMessageSourceTests {
@SuppressWarnings("unchecked")
private void setUpMockScenarioForMessagePolling() throws Exception{
RateLimitStatus rateLimitStatus = mock(RateLimitStatus.class);
when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatus);
Twitter tw = mock(Twitter.class);
when(twitter.getUnderlyingTwitter()).thenReturn(tw);
when(tw.getRateLimitStatus()).thenReturn(rateLimitStatus);
when(rateLimitStatus.getSecondsUntilReset()).thenReturn(2464);
when(rateLimitStatus.getRemainingHits()).thenReturn(250);

View File

@@ -26,6 +26,7 @@ import org.springframework.integration.twitter.core.TwitterOperations;
import org.springframework.scheduling.TriggerContext;
import twitter4j.RateLimitStatus;
import twitter4j.Twitter;
/**
* @author Oleg Zhurakousky
@@ -36,7 +37,9 @@ public class RateLimitStatusTriggerTests {
@Test
public void testTriggerImediateAndSubsequentExecutionTime() throws Exception{
TwitterOperations twitter = mock(TwitterOperations.class);
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter);
Twitter tw = mock(Twitter.class);
when(twitter.getUnderlyingTwitter()).thenReturn(tw);
RateLimitStatusTrigger trigger = new RateLimitStatusTrigger(twitter.getUnderlyingTwitter());
TriggerContext context = mock(TriggerContext.class);
Date currentDate = new Date(System.currentTimeMillis());
Date nextDate = trigger.nextExecutionTime(context);
@@ -45,7 +48,7 @@ public class RateLimitStatusTriggerTests {
RateLimitStatus rateLimitStatis = mock(RateLimitStatus.class);
when(twitter.getRateLimitStatus()).thenReturn(rateLimitStatis);
when(tw.getRateLimitStatus()).thenReturn(rateLimitStatis);
when(rateLimitStatis.getRemainingHits()).thenReturn(2000);
when(rateLimitStatis.getSecondsUntilReset()).thenReturn(4000);