code cleanup

This commit is contained in:
DOHA 2015-05-03 14:54:49 +02:00
parent 9f9ddea279
commit 746fe1774c
3 changed files with 26 additions and 10 deletions

View File

@ -9,7 +9,6 @@ import org.baeldung.persistence.service.RedditTokenService;
import org.baeldung.reddit.classifier.RedditClassifier;
import org.baeldung.reddit.util.MyFeatures;
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.Value;
import org.springframework.context.annotation.Bean;
@ -76,14 +75,12 @@ public class WebConfig extends WebMvcConfigurerAdapter {
}
@Bean
public ScheduledTasks scheduledTasks(OAuth2ProtectedResourceDetails reddit) {
final ScheduledTasks s = new ScheduledTasks();
public OAuth2RestTemplate schedulerRedditTemplate(OAuth2ProtectedResourceDetails reddit) {
final List<ClientHttpRequestInterceptor> list = new ArrayList<ClientHttpRequestInterceptor>();
list.add(new UserAgentInterceptor());
final OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(reddit);
restTemplate.setInterceptors(list);
s.setRedditRestTemplate(restTemplate);
return s;
return restTemplate;
}
@Bean

View File

@ -22,6 +22,7 @@ import org.baeldung.reddit.util.RedditApiConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
@ -52,6 +53,7 @@ public class RedditController {
public static final String REMEMBER_ME_COOKIE = "CustomRememberMe";
@Autowired
@Qualifier("redditRestTemplate")
private OAuth2RestTemplate redditRestTemplate;
@Autowired

View File

@ -12,6 +12,7 @@ import org.baeldung.reddit.util.RedditApiConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
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.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import com.fasterxml.jackson.databind.JsonNode;
public class ScheduledTasks {
@Component
public class RedditScheduler {
@Autowired
@Qualifier("schedulerRedditTemplate")
private OAuth2RestTemplate redditRestTemplate;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
private PostRepository postReopsitory;
private final Logger logger = LoggerFactory.getLogger(getClass());
@Scheduled(fixedRate = 1 * 60 * 1000)
public void reportCurrentTime() {
public void schedulePosts() {
final List<Post> posts = postReopsitory.findBySubmissionDateBeforeAndIsSent(new Date(), false);
logger.info(posts.size() + " Posts in the queue.");
for (final Post post : posts) {
submitPost(post);
}
}
@Scheduled(fixedRate = 3 * 60 * 1000)
public void checkAndReSubmitPosts() {
final List<Post> submitted = postReopsitory.findByRedditIDNotNullAndNoOfAttemptsGreaterThan(0);
logger.info(submitted.size() + " Posts to check their score");
for (final Post post : submitted) {
checkIfNeedResubmit(post);
checkAndReSubmit(post);
}
}
@ -113,7 +122,15 @@ public class ScheduledTasks {
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 interval = currentTime - post.getSubmissionDate().getTime();
final long intervalInMinutes = TimeUnit.MINUTES.convert(interval, TimeUnit.MILLISECONDS);