code cleanup
This commit is contained in:
parent
9f9ddea279
commit
746fe1774c
|
@ -9,7 +9,6 @@ import org.baeldung.persistence.service.RedditTokenService;
|
||||||
import org.baeldung.reddit.classifier.RedditClassifier;
|
import org.baeldung.reddit.classifier.RedditClassifier;
|
||||||
import org.baeldung.reddit.util.MyFeatures;
|
import org.baeldung.reddit.util.MyFeatures;
|
||||||
import org.baeldung.reddit.util.UserAgentInterceptor;
|
import org.baeldung.reddit.util.UserAgentInterceptor;
|
||||||
import org.baeldung.web.schedule.ScheduledTasks;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
|
@ -76,14 +75,12 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
|
public OAuth2RestTemplate schedulerRedditTemplate(OAuth2ProtectedResourceDetails reddit) {
|
||||||
final ScheduledTasks s = new ScheduledTasks();
|
|
||||||
final List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
|
final List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
|
||||||
list.add(new UserAgentInterceptor());
|
list.add(new UserAgentInterceptor());
|
||||||
final OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(reddit);
|
final OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(reddit);
|
||||||
restTemplate.setInterceptors(list);
|
restTemplate.setInterceptors(list);
|
||||||
s.setRedditRestTemplate(restTemplate);
|
return restTemplate;
|
||||||
return s;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -22,6 +22,7 @@ import org.baeldung.reddit.util.RedditApiConstants;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
@ -52,6 +53,7 @@ public class RedditController {
|
||||||
public static final String REMEMBER_ME_COOKIE = "CustomRememberMe";
|
public static final String REMEMBER_ME_COOKIE = "CustomRememberMe";
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
@Qualifier("redditRestTemplate")
|
||||||
private OAuth2RestTemplate redditRestTemplate;
|
private OAuth2RestTemplate redditRestTemplate;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
|
|
|
@ -12,6 +12,7 @@ import org.baeldung.reddit.util.RedditApiConstants;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
import org.springframework.scheduling.annotation.Scheduled;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
||||||
|
@ -19,31 +20,39 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
|
||||||
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
|
||||||
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
|
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
|
|
||||||
import com.fasterxml.jackson.databind.JsonNode;
|
import com.fasterxml.jackson.databind.JsonNode;
|
||||||
|
|
||||||
public class ScheduledTasks {
|
@Component
|
||||||
|
public class RedditScheduler {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("schedulerRedditTemplate")
|
||||||
private OAuth2RestTemplate redditRestTemplate;
|
private OAuth2RestTemplate redditRestTemplate;
|
||||||
private final Logger logger = LoggerFactory.getLogger(getClass());
|
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private PostRepository postReopsitory;
|
private PostRepository postReopsitory;
|
||||||
|
|
||||||
|
private final Logger logger = LoggerFactory.getLogger(getClass());
|
||||||
|
|
||||||
@Scheduled(fixedRate = 1 * 60 * 1000)
|
@Scheduled(fixedRate = 1 * 60 * 1000)
|
||||||
public void reportCurrentTime() {
|
public void schedulePosts() {
|
||||||
final List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false);
|
final List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false);
|
||||||
logger.info(posts.size() + " Posts in the queue.");
|
logger.info(posts.size() + " Posts in the queue.");
|
||||||
for (final Post post : posts) {
|
for (final Post post : posts) {
|
||||||
submitPost(post);
|
submitPost(post);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Scheduled(fixedRate = 3 * 60 * 1000)
|
||||||
|
public void checkAndReSubmitPosts() {
|
||||||
final List<Post> submitted = postReopsitory.findByRedditIDNotNullAndNoOfAttemptsGreaterThan(0);
|
final List<Post> submitted = postReopsitory.findByRedditIDNotNullAndNoOfAttemptsGreaterThan(0);
|
||||||
logger.info(submitted.size() + " Posts to check their score");
|
logger.info(submitted.size() + " Posts to check their score");
|
||||||
for (final Post post : submitted) {
|
for (final Post post : submitted) {
|
||||||
checkIfNeedResubmit(post);
|
checkAndReSubmit(post);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +122,15 @@ public class ScheduledTasks {
|
||||||
logger.info(node.toString());
|
logger.info(node.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void checkIfNeedResubmit(Post post) {
|
private void checkAndReSubmit(Post post) {
|
||||||
|
try {
|
||||||
|
checkAndReSubmitInternal(post);
|
||||||
|
} catch (final Exception e) {
|
||||||
|
logger.error("Error occurred while check post " + post.toString(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void checkAndReSubmitInternal(Post post) {
|
||||||
final long currentTime = new Date().getTime();
|
final long currentTime = new Date().getTime();
|
||||||
final long interval = currentTime - post.getSubmissionDate().getTime();
|
final long interval = currentTime - post.getSubmissionDate().getTime();
|
||||||
final long intervalInMinutes = TimeUnit.MINUTES.convert(interval, TimeUnit.MILLISECONDS);
|
final long intervalInMinutes = TimeUnit.MINUTES.convert(interval, TimeUnit.MILLISECONDS);
|
Loading…
Reference in New Issue