From 746fe1774c3226797274312dca7cdf1919fa09a3 Mon Sep 17 00:00:00 2001 From: DOHA Date: Sun, 3 May 2015 14:54:49 +0200 Subject: [PATCH] code cleanup --- .../java/org/baeldung/config/WebConfig.java | 7 ++--- .../org/baeldung/web/RedditController.java | 2 ++ ...heduledTasks.java => RedditScheduler.java} | 27 +++++++++++++++---- 3 files changed, 26 insertions(+), 10 deletions(-) rename spring-security-oauth/src/main/java/org/baeldung/web/schedule/{ScheduledTasks.java => RedditScheduler.java} (90%) diff --git a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java index d506815424..c8ba494663 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java +++ b/spring-security-oauth/src/main/java/org/baeldung/config/WebConfig.java @@ -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 list = new ArrayList(); list.add(new UserAgentInterceptor()); final OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(reddit); restTemplate.setInterceptors(list); - s.setRedditRestTemplate(restTemplate); - return s; + return restTemplate; } @Bean diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java index 26e546e9a9..672749a99e 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/RedditController.java @@ -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 diff --git a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/RedditScheduler.java similarity index 90% rename from spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java rename to spring-security-oauth/src/main/java/org/baeldung/web/schedule/RedditScheduler.java index ee7bcc36ac..11c1d36ccd 100644 --- a/spring-security-oauth/src/main/java/org/baeldung/web/schedule/ScheduledTasks.java +++ b/spring-security-oauth/src/main/java/org/baeldung/web/schedule/RedditScheduler.java @@ -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 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 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);