转载

Queues.drain 解决按数量和指定时间弹出队列中的元素

当要做操作日志跟踪时,实时一条条日志数据保存到数据库很消耗数据库的性能, 于是想到把日志缓存起来,积累到一定数量或过了一定时间 (就算数量不足),批量插入到数据库。

maven 配置

		<dependency>
			<groupId>com.google.guava</groupId>
			<artifactId>guava</artifactId>
			<version>30.1-jre</version>
		</dependency>

java 代码

@Service
public class ActionLogServiceImpl extends BaseServiceImpl<ActionLog> implements ActionLogService {

    volatile BlockingQueue<ActionLog> queue = new LinkedBlockingQueue<ActionLog>();

    volatile boolean isRunning = true;

    
    @PostConstruct
    public void postConstruct() {
        Thread thread = new Thread(new Runnable() {

            @Override
            public void run() {
                while (isRunning) {
                    try {
                        List<ActionLog> list = new ArrayList<>(1005);
                        //阻塞积累超过1000条或5分钟,更新到数据库
                        Queues.drain(queue, list, 1000, 60 * 5, TimeUnit.SECONDS);
                        batchSave(list);
                    } catch (Throwable e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        thread.start();
    }

    @PreDestroy
    public void destroy() {
        isRunning = false;
        List<ActionLog> list = new ArrayList<>(1005);
        queue.drainTo(list);
        batchSave(list);
    }

    public void batchSave(List<ActionLog> list) {
         //保存到数据库
    }


    //在别处调用这个方法插入队列
    @Override
    public void addActionLog(ActionLog actionLog) {
        queue.add(actionLog);
    }
}
 
正文到此结束
Loading...