Refactor post sync scheduler

pull/137/head
johnniang 2019-03-02 17:26:39 +08:00
parent 79eeba7fbd
commit b69ec0313e
3 changed files with 14 additions and 4 deletions

View File

@ -8,6 +8,7 @@ import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing; import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.scheduling.annotation.EnableScheduling;
/** /**
* <pre> * <pre>
@ -21,6 +22,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication @SpringBootApplication
@EnableCaching @EnableCaching
@EnableJpaAuditing @EnableJpaAuditing
@EnableScheduling
@EnableJpaRepositories(basePackages = "cc.ryanc.halo.repository", repositoryBaseClass = BaseRepositoryImpl.class) @EnableJpaRepositories(basePackages = "cc.ryanc.halo.repository", repositoryBaseClass = BaseRepositoryImpl.class)
public class Application { public class Application {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -50,8 +50,8 @@ public class StartedListener implements ApplicationListener<ApplicationStartedEv
this.loadThemes(); this.loadThemes();
this.loadOwo(); this.loadOwo();
//启动定时任务 //启动定时任务
CronUtil.start(); // CronUtil.start();
log.info("The scheduled task starts successfully!"); // log.info("The scheduled task starts successfully!");
} }
@PreDestroy @PreDestroy

View File

@ -2,8 +2,9 @@ package cc.ryanc.halo.task;
import cc.ryanc.halo.model.domain.Post; import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.service.PostService; import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import static cc.ryanc.halo.model.dto.HaloConst.POSTS_VIEWS; import static cc.ryanc.halo.model.dto.HaloConst.POSTS_VIEWS;
@ -12,13 +13,20 @@ import static cc.ryanc.halo.model.dto.HaloConst.POSTS_VIEWS;
* @date : 2018/12/5 * @date : 2018/12/5
*/ */
@Slf4j @Slf4j
@Component
public class PostSyncTask { public class PostSyncTask {
private final PostService postService;
public PostSyncTask(PostService postService) {
this.postService = postService;
}
/** /**
* *
*/ */
@Scheduled(cron = "0 0 * * * *")
public void postSync() { public void postSync() {
final PostService postService = SpringUtil.getBean(PostService.class);
int count = 0; int count = 0;
for (Long key : POSTS_VIEWS.keySet()) { for (Long key : POSTS_VIEWS.keySet()) {
Post post = postService.getByIdOfNullable(key); Post post = postService.getByIdOfNullable(key);