By default, Application Real-Time Monitoring Service (ARMS) allows you to monitor asynchronous tasks that are created by using the @Async annotation in Spring. In addition, ARMS allows you to monitor custom asynchronous tasks by adding scan packages for asynchronous pass-through or using ARMS SDKs for manual pass-through. If an error such as a method timeout occurs when your asynchronous task is running, you can use a trace to check the upstream and downstream of the asynchronous task. This helps you troubleshoot potential errors at the earliest opportunity.
Prerequisites
Method 1: Use the @Async annotation in Spring
- Executors:
- org.springframework.scheduling.concurrent.ConcurrentTaskExecutor
- org.springframework.core.task.SimpleAsyncTaskExecutor
- org.springframework.scheduling.quartz.SimpleThreadPoolTaskExecutor
- org.springframework.core.task.support.TaskExecutorAdapter
- org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor
- org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler
- org.springframework.jca.work.WorkManagerTaskExecutor
- org.springframework.scheduling.commonj.WorkManagerTaskExecutor
- Tasks:
- org.springframework.aop.interceptor.AsyncExecutionInterceptor$1
- org.springframework.aop.interceptor.AsyncExecutionInterceptor$$Lambda$
Method 2: Add a scan package for asynchronous pass-through
You can add a scan package for asynchronous pass-through to the configurations of your application to monitor asynchronous tasks. After an Runnable object, a Callable object, or a Supplier object is created, the corresponding method in the scan package for asynchronous pass-through automatically captures the trace context of the current thread. Then, when threads are used in asynchronous mode, the method passes the captured trace context to the threads.
package com.alibaba.arms.brightroar.console.service;
@Service
public class NameService {
private ExecutorService es = Executors.newFixedThreadPool(5);
public void name() {
es.submit(new Runnable() {
@Override
public void run() {
System.out.println(System.currentTimeMillis()+ ": my name is john, " + Thread.currentThread().getId());
}
});
}
}
Method 3: Use an ARMS SDK to implement manual pass-through
If the preceding methods cannot meet your requirements in a complex monitoring scenario, you can use an ARMS SDK to implement manual pass-through. You can enhance the Runnable and Callable methods and executors to concatenate the traces in asynchronous threads to monitor asynchronous tasks.