每次收到点击请求,将文章id放进一个队列里,然后开启一个轮询服务,隔五秒去update到数据库中。每次查询到的文章点击量,有可能是未及时更新的(时效性要求不高)。
使用ConcurrentLinkedQueue 作为队列,因为它线程安全, 长度足够满足需要
public class CargroupService implements ICargroupService {
private final static Log log = LogFactory.getLog(CargroupService.class);
/** 文章访问队列 **/
public static ConcurrentLinkedQueue articleClickQueue = new ConcurrentLinkedQueue();
/** 定时更新点击量线程 **/
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
/** 更新线程是否启动 **/
private static volatile boolean isStartScheduled = false;
@Override
public void getArticleDetail(ArticleQuery query) {
// 利用线程安全的队列来保存被点击的文章,然后定时更新数据库
String queueStr = dbInfo.getId()+";"+ip;
articleClickQueue.offer(queueStr);
if (isStartS