/
TwitterSourceConfiguration.java
64 lines (55 loc) · 2.2 KB
/
TwitterSourceConfiguration.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package io.switchbit.configuration;
import org.apache.camel.spi.IdempotentRepository;
import org.apache.camel.spring.processor.idempotent.SpringCacheIdempotentRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;
/**
* Twitter configuration taken in part from
* https://github.com/snicoll/spring-boot-starter-twitter4j
*/
@Configuration
@EnableConfigurationProperties(TwitterProperties.class)
public class TwitterSourceConfiguration {
private TwitterProperties properties;
public TwitterSourceConfiguration(TwitterProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public TwitterFactory twitterFactory() {
if (this.properties.getConsumerKey() == null
|| this.properties.getConsumerSecret() == null
|| this.properties.getAccessToken() == null
|| this.properties.getAccessTokenSecret() == null) {
throw new RuntimeException(
"Twitter properties not configured properly. Please check twitter.* properties settings in configuration file.");
}
ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
configurationBuilder.setDebugEnabled(false)
.setOAuthConsumerKey(properties.getConsumerKey())
.setOAuthConsumerSecret(properties.getConsumerSecret())
.setOAuthAccessToken(properties.getAccessToken())
.setOAuthAccessTokenSecret(properties.getAccessTokenSecret());
return new TwitterFactory(configurationBuilder.build());
}
@Bean
@ConditionalOnMissingBean
public Twitter twitter(TwitterFactory twitterFactory) {
return twitterFactory.getInstance();
}
/**
* {@link IdempotentRepository} backed by Redis. Used to prevent retweets being
* processed more than once
*/
@Bean
public IdempotentRepository springCacheIdempotentRepository(
CacheManager cacheManager) {
return new SpringCacheIdempotentRepository(cacheManager, "retweets");
}
}