网页浏览量统计(队列实现)

这是一个demo,传达的只是思想,实际情况请兄弟们自己研究。

用一个线程安全的队列ConcurrentLinkedQueue 来保存请求个数,定时任务五秒执行一次,设置sum(sum = 数据库值+Queue.size()),然后会清除ConcurrentLinkedQueue 的内容,五秒一循环。队列可以保存任何信息,按需求而定

@Component
public class QueueDemo {
    private ConcurrentLinkedQueue concurrentLinkedQueue = new ConcurrentLinkedQueue();

    public ConcurrentLinkedQueue getConcurrentLinkedQueue() {
        return concurrentLinkedQueue;
    }

    public void setConcurrentLinkedQueue(ConcurrentLinkedQueue concurrentLinkedQueue) {
        this.concurrentLinkedQueue = concurrentLinkedQueue;
    }
}

包括定时任务 Schedled 

@Service
public class TestService {

    static volatile int i = 0;

    @Resource
    StudentMapper studentMapper;

    @Autowired
    QueueDemo queue;
    public void test1() throws InterruptedException {
        queue.getConcurrentLinkedQueue().add(System.currentTimeMillis());
        System.out.println("queue添加成功");
        new Thread(()->{
            try {
                TimeUnit.SECONDS.sleep(5);
                System.out.println("睡眠"+System.currentTimeMillis()+"  "+i);

            } catch (InterruptedException e) {
            }
        }).start();
    }
    @Scheduled(cron = "*/5 * * * * *")
    public void updateSum(){

        //把它当成在数据库读取网页信息的方法
        Student studen = studentMapper.select("num");
        System.out.println(studen);
        studen.setSum(
                queue.getConcurrentLinkedQueue().size()+studen.getSum());
        studentMapper.update(studen);
        queue.getConcurrentLinkedQueue().clear();
    }
}

使用Jmeter测试

猜你喜欢

转载自blog.csdn.net/qq_41835813/article/details/108953704