提交 c8cda227 编写于 作者: yubinCloud's avatar yubinCloud

10-6 增加定时任务,定时执行更新 ebook 信息的 SQL

上级 a33fe00c
package io.github.yubincloud.fairywiki.job;
import io.github.yubincloud.fairywiki.service.DocService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class DocJob {
private static final Logger LOG = LoggerFactory.getLogger(DocJob.class);
@Resource
private DocService docService;
/**
* 每30秒更新电子书信息
*/
@Scheduled(cron = "5/30 * * * * ?")
public void cron() {
System.out.println("Starting a doc job");
docService.updateEbookFooter();
}
}
package io.github.yubincloud.fairywiki.job;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.text.SimpleDateFormat;
import java.util.Date;
@Component
public class ScheduleJobDemo {
private static final Logger LOG = LoggerFactory.getLogger(ScheduleJobDemo.class);
/**
* 固定时间间隔,fixedRate单位毫秒
*/
@Scheduled(fixedRate = 1000)
public void simple() throws InterruptedException {
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
String dateString = formatter.format(new Date());
Thread.sleep(2000);
LOG.info("每隔5秒钟执行一次: {}", dateString);
}
/**
* 自定义cron表达式跑批
* 只有等上一次执行完成,下一次才会在下一个时间点执行,错过就错过
*/
@Scheduled(cron = "*/1 * * * * ?")
public void cron() throws InterruptedException {
SimpleDateFormat formatter = new SimpleDateFormat("mm:ss SSS");
String dateString = formatter.format(new Date());
Thread.sleep(1500);
LOG.info("每隔1秒钟执行一次: {}", dateString);
}
}
//package io.github.yubincloud.fairywiki.job;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.scheduling.annotation.Scheduled;
//import org.springframework.stereotype.Component;
//
//import java.text.SimpleDateFormat;
//import java.util.Date;
//
//
//@Component
//public class ScheduleJobDemo {
//
// private static final Logger LOG = LoggerFactory.getLogger(ScheduleJobDemo.class);
//
// /**
// * 固定时间间隔,fixedRate单位毫秒
// */
// @Scheduled(fixedRate = 1000)
// public void simple() throws InterruptedException {
// SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
// String dateString = formatter.format(new Date());
// Thread.sleep(2000);
// LOG.info("每隔5秒钟执行一次: {}", dateString);
// }
//
// /**
// * 自定义cron表达式跑批
// * 只有等上一次执行完成,下一次才会在下一个时间点执行,错过就错过
// */
// @Scheduled(cron = "*/1 * * * * ?")
// public void cron() throws InterruptedException {
// SimpleDateFormat formatter = new SimpleDateFormat("mm:ss SSS");
// String dateString = formatter.format(new Date());
// Thread.sleep(1500);
// LOG.info("每隔1秒钟执行一次: {}", dateString);
// }
//}
......@@ -15,4 +15,9 @@ public interface DocMapperCustom {
* @param docId 文档的 id
*/
void increaseVoteCount(@Param("id") Long docId);
/**
* 更新所有 Ebook 的阅读量、点赞量信息
*/
void updateEbookFooter();
}
......@@ -18,10 +18,10 @@ import io.github.yubincloud.fairywiki.utils.CopyUtil;
import io.github.yubincloud.fairywiki.utils.RedisUtil;
import io.github.yubincloud.fairywiki.utils.RequestContext;
import io.github.yubincloud.fairywiki.utils.SnowFlake;
import org.apache.commons.lang3.ObjectUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import javax.annotation.Resource;
import java.util.List;
......@@ -153,4 +153,11 @@ public class DocService {
private String constructIpKeyInRedis(Long docId, String ip) {
return "ODC_VOTE_" + docId + "_" + ip;
}
/**
* 更新所有 Ebook 的阅读量、点赞量信息
*/
public void updateEbookFooter() {
docMapperCustom.updateEbookFooter();
}
}
......@@ -14,4 +14,13 @@
WHERE id = #{id}
</update>
<update id="updateEbookFooter">
UPDATE ebook t1, (
SELECT ebook_id, count(1) doc_count, sum(view_count) view_count, sum(vote_count) vote_count
FROM doc
GROUP BY ebook_id) t2
SET t1.doc_count = t2.doc_count, t1.view_count = t2.view_count, t1.vote_count = t2.vote_count
WHERE t1.id = t2.ebook_id
</update>
</mapper>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册