package cn.ithers.blog.schedule;
import cn.ithers.blog.service.IEsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
/**
* @Author: zhanghuan
* @date: 2020/11/1 17:14
* @description:
*/
public class EsImportAllTask {
@Autowired
private IEsArticleService esArticleService;
/**
* 定义一个按时间执行的定时任务,在每天16:00执行一次
* 把数据库中的所有文章按时导入es
*/
@Scheduled(cron = "0 0 16 * * ?")
public void importArticlesByTime() {
esArticleService.importAll();
}
}
package cn.ithers.blog.listener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
* @Author: zhanghuan
* @date: 2020/10/26 10:17
* @description:
*/
@Slf4j
@Configuration
@Order(Ordered.HIGHEST_PRECEDENCE)
public class StartedListener implements ApplicationListener<ApplicationStartedEvent> {
@Value("${blog-url}")
private String url;
@Value("${server.port}")
private String port;
@Override
public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
this.printStartInfo();
}
private void printStartInfo() {
log.info("Blog started at {}", url + ":" + port);
log.info("Blog has started successfully!");
}
}
