Also let business operation records affect interface performance?

Posted by richmlpdx on Sun, 07 Nov 2021 19:36:11 +0100

1, Foreword

In other words, operation record is a necessary component of each business system! I believe that the small partners working on the Internet must have recorded demerit and operation records in business development! In fact, operation records are a relatively simple thing. There should be interfaces involved in modification. Although they are simple, they are also important. They are useless at ordinary times, but they may be an important source of information at critical times!

So the partners should pay attention to the operation records in the development!

As a siege lion with craftsman spirit, we must take care of the details 😎

Although operation records are necessary and coupled in our business logic, we absolutely do not allow operation records to affect our interface performance.
It's all coupled in the business logic. Why doesn't it affect the interface response time?
Asynchronous recording!!!

Therefore, the focus of this article is to take you to realize asynchronous recording of operation records. If you are not familiar with operation records, you can privately trust me.

2, Train of thought

When it comes to asynchrony, you must have ideas. Next, let's give you a picture. Your first reaction may be - this is MQ. Yes, this idea is similar to the implementation of MQ. It can be said to be the basic version of MQ.

Although you can understand the idea at the first glance, for the sake of my hard work in drawing, listen to my idea of breaking the text version. Ha ha, by the way, I'll give you a wave of actual combat code 😎

/**
 * Execute operation logging thread
 */
@Slf4j
public class OperationLogHandler implements Runnable{

    // Operation storage queue++++++
    private static ConcurrentLinkedQueue<OperationRecord> holderList = new ConcurrentLinkedQueue();

    private OperationRecordService recordService = SpringBeanUtil.getBeanByClass(OperationRecordService.class);

    @Override
    public void run() {
        for (;;){
            Iterator<OperationRecord> iterator = holderList.iterator();
            // Sleep when empty
            while (iterator.hasNext()){
                OperationRecord record = iterator.next();
                if (record == null) {
                    iterator.remove();
                    continue;
                }
                // Perform stock drop operation
                boolean saveRet = recordService.save(record);
                log.info("The result of asynchronously saving user operation log is:{}",saveRet);
                iterator.remove();
            }
        }
    }

    /**
     * Add record to queue (specific business logic call)
     * @param operationRecords
     */
    public static void addOpLogs(List<OperationRecord> operationRecords){
        if (CollectionUtil.isEmpty(operationRecords)){
            return;
        }
        boolean addRet = holderList.addAll(operationRecords);
        log.info("Add the operation log to the linked list. The result is:{}",addRet);
    }
}

The reason for using ConcurrentLinkedQueue as a thread safe queue is to prevent thread safety problems caused by concurrent business operations. Since there is only one thread working on the consumer, there is no thread safety problem.

The next step is to start the thread when the service starts

@Component
public class SyncOperationLogListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        run();
    }

    public void run(){
    	// Start using thread pool mode
        ThreadPoolUtil.getThreadPoolExecutor()
                .execute(new OperationLogHandler());
        System.out.println("=== operationLog handler start success ===");
    }
}

Here we will finish the design idea and implementation. I hope you can help.

3, Summary

I still have to emphasize the importance of operation records and logging, which can help us check the problem at the critical time and quickly locate the location of the problem.
Although the code we write is dense at a close look, standing far away, there are only two words on the full screen, self-confidence! Therefore, as a siege lion with craftsman spirit, you must take care of the details 😎

Come on, worker! awesome 😎

I'm rose. Thank you for watching. Your praise is the biggest driving force for Rose's output. See you in the next article!

Note: if there are any mistakes and suggestions in this blog, you are welcome to leave a message. If you are not familiar with the operation records, you are welcome to send a private letter to me!

Topics: Java