Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
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;
Expand Down Expand Up @@ -76,14 +75,12 @@ public void addViewControllers(final ViewControllerRegistry registry) {
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -52,6 +53,7 @@ public class RedditController {
public static final String REMEMBER_ME_COOKIE = "CustomRememberMe";

@Autowired
@Qualifier("redditRestTemplate")
private OAuth2RestTemplate redditRestTemplate;

@Autowired
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,47 @@
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;
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);
}
}

Expand Down Expand Up @@ -113,7 +122,15 @@ private void deletePost(String redditId) {
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);
Expand Down