polishing Twitter
This commit is contained in:
@@ -20,8 +20,10 @@ import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.integration.Message;
|
||||
@@ -52,6 +54,12 @@ import org.springframework.util.StringUtils;
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint implements MessageSource {
|
||||
|
||||
private volatile long lastPollForTweet;
|
||||
|
||||
private final CountDownLatch latch = new CountDownLatch(1);
|
||||
|
||||
private final TwitterPollingTask twitterPoller = new TwitterPollingTask();
|
||||
|
||||
private volatile MetadataStore metadataStore;
|
||||
|
||||
@@ -122,8 +130,23 @@ abstract class AbstractTwitterMessageSource<T> extends AbstractEndpoint implemen
|
||||
}
|
||||
}
|
||||
|
||||
public Message<?> receive() {
|
||||
public Message<?> receive() {
|
||||
Tweet tweet = this.tweets.poll();
|
||||
if (tweet == null){
|
||||
long currentTime = System.currentTimeMillis();
|
||||
long diff = currentTime - lastPollForTweet;
|
||||
lastPollForTweet = currentTime;
|
||||
if (diff < 15000){
|
||||
try {
|
||||
long waitTime = 15000-diff;
|
||||
this.latch.await(waitTime, TimeUnit.MILLISECONDS);
|
||||
} catch (Exception e) {
|
||||
//ignore
|
||||
}
|
||||
}
|
||||
twitterPoller.run();
|
||||
}
|
||||
tweet = this.tweets.poll();
|
||||
if (tweet != null) {
|
||||
this.lastProcessedId = tweet.getId();
|
||||
this.metadataStore.put(this.metadataKey, String.valueOf(this.lastProcessedId));
|
||||
|
||||
@@ -27,12 +27,12 @@
|
||||
<channel id="inbound_updates"/>
|
||||
<channel id="inbound_search"/>
|
||||
|
||||
<!-- <beans:bean id="twitterTemplate" class="org.springframework.integration.twitter.core.Twitter4jTemplate">-->
|
||||
<!-- <beans:constructor-arg value="${twitter.oauth.consumerKey}"/>-->
|
||||
<!-- <beans:constructor-arg value="${twitter.oauth.consumerSecret}"/>-->
|
||||
<!-- <beans:constructor-arg value="${twitter.oauth.accessToken}"/>-->
|
||||
<!-- <beans:constructor-arg value="${twitter.oauth.accessTokenSecret}"/>-->
|
||||
<!-- </beans:bean>-->
|
||||
<beans:bean id="twitterTemplate" class="org.springframework.integration.twitter.core.Twitter4jTemplate">
|
||||
<beans:constructor-arg value="${twitter.oauth.consumerKey}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.consumerSecret}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.accessToken}"/>
|
||||
<beans:constructor-arg value="${twitter.oauth.accessTokenSecret}"/>
|
||||
</beans:bean>
|
||||
|
||||
<!-- <twitter:mentions-inbound-channel-adapter twitter-template="twitterTemplate" channel="inbound_mentions">-->
|
||||
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
|
||||
@@ -44,15 +44,15 @@
|
||||
<!-- </twitter:dm-inbound-channel-adapter>-->
|
||||
<!-- <service-activator input-channel="inbound_dm" ref="twitterAnnouncer" method="dm"/>-->
|
||||
|
||||
<twitter:search-inbound-channel-adapter id="searchAdapter" channel="inbound_search" query="#springintegration">
|
||||
<poller fixed-rate="5000" max-messages-per-poll="5"/>
|
||||
</twitter:search-inbound-channel-adapter>
|
||||
<service-activator input-channel="inbound_search" ref="twitterAnnouncer" method="search"/>
|
||||
<!-- <twitter:search-inbound-channel-adapter id="searchAdapter" channel="inbound_search" query="#springintegration">-->
|
||||
<!-- <poller fixed-rate="5000" max-messages-per-poll="5"/>-->
|
||||
<!-- </twitter:search-inbound-channel-adapter>-->
|
||||
<!-- <service-activator input-channel="inbound_search" ref="twitterAnnouncer" method="search"/>-->
|
||||
|
||||
<!-- <twitter:inbound-channel-adapter id="twitterInbound" twitter-template="twitterTemplate" channel="inbound_updates">-->
|
||||
<!-- <poller fixed-rate="5000" max-messages-per-poll="3"/>-->
|
||||
<!-- </twitter:inbound-channel-adapter>-->
|
||||
<!-- <service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>-->
|
||||
<twitter:inbound-channel-adapter id="twitterInbound" twitter-template="twitterTemplate" channel="inbound_updates">
|
||||
<poller fixed-rate="1000" max-messages-per-poll="3"/>
|
||||
</twitter:inbound-channel-adapter>
|
||||
<service-activator input-channel="inbound_updates" ref="twitterAnnouncer" method="updates"/>
|
||||
|
||||
<beans:bean id="twitterAnnouncer" class="org.springframework.integration.twitter.ignored.TwitterAnnouncer"/>
|
||||
</beans:beans>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# oauth setup for prosibook twitter account
|
||||
twitter.oauth.consumerKey=
|
||||
twitter.oauth.consumerSecret=
|
||||
twitter.oauth.accessToken=
|
||||
twitter.oauth.accessTokenSecret=
|
||||
twitter.oauth.consumerKey=OU4CbkHKIWl1SI0VwiOgAQ
|
||||
twitter.oauth.consumerSecret=p6pPukUG6d0ebSXuLSI9iaq2MIpzxJoLZyj6ilRmO3o
|
||||
twitter.oauth.accessToken=61091649-vWVUNginOL069jjpm2lHEuxGXjW163kK3CPNZCdcc
|
||||
twitter.oauth.accessTokenSecret=dufKyVlcngRitGDFfqiTlJC5leh6lsxPaq8FbeBc
|
||||
Reference in New Issue
Block a user