@async注解的使用

异步调用

1.使用:

springboot中的启动类中需要添加注解@EnableAsync来开启异步调用,在需要异步执行的方法上添加@Async("taskExecutor")注解进行标注。

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Async {

    /**
     * A qualifier value for the specified asynchronous operation(s).
     * 

May be used to determine the target executor to be used when executing this * method, matching the qualifier value (or the bean name) of a specific * {@link java.util.concurrent.Executor Executor} or * {@link org.springframework.core.task.TaskExecutor TaskExecutor} * bean definition. *

When specified on a class level {@code @Async} annotation, indicates that the * given executor should be used for all methods within the class. Method level use * of {@code Async#value} always overrides any value set at the class level. * @since 3.1.2 */ String value() default ""; }

一般会添加一个线程池的配置,不影响主线程,异步方法交给单独的线程完成

@Configuration
public class AsyncConfig {

    private static final int MAX_POOL_SIZE = 50;

    private static final int CORE_POOL_SIZE = 20;

    @Bean("taskExecutor")
    public AsyncTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
        taskExecutor.setMaxPoolSize(MAX_POOL_SIZE);
        taskExecutor.setCorePoolSize(CORE_POOL_SIZE);
        taskExecutor.setThreadNamePrefix("async-task-thread-pool");
        taskExecutor.initialize();
        return taskExecutor;
    }
}

需要执行异步调用的方法示例(带有返回值的Future,需要用到AsyncResult)

@Service
public class DeviceProcessServiceImpl implements DeviceProcessService {

    @Autowired
    private DeviceRpcService deviceRpcService;

    @Async("taskExecutor")
    @Override
    public Future>> queryDeviceProcessAbilities(List bindDevices) {
        if (CollectionUtils.isEmpty(bindDevices)) {
            return new AsyncResult<>(Maps.newHashMap());
        }
        List deviceIds = bindDevices.stream().map(BindDeviceDO::getDeviceId).collect(Collectors.toList());

        List devices = deviceRpcService.getDeviceProcessAbility(deviceIds);
        Map> deviceAbilityMap = Maps.newHashMap();
        ...
        return new AsyncResult<>(deviceAbilityMap);
    }
}

对加了@async注解方法有返回值的调用

private ProcessAbilityData asyncCollectProcessAbilities(List bindDevices,
                                                            List bindStaffs, String dccId) {
        // 返回值
        Future>> deviceProcessFutureResult = deviceProcessService
            .queryDeviceProcessAbilities(bindDevices);
        Future>> staffAbilityFutureResult = staffProcessService
            .queryStaffProcessAbilities(bindStaffs, dccId);
        Map> deviceAbilityMap;
        Map> staffAbilityMap;
        try {
            deviceAbilityMap = deviceProcessFutureResult.get();
            staffAbilityMap = staffAbilityFutureResult.get();
        } catch (InterruptedException | ExecutionException e) {
            ...
        }
        return new ProcessAbilityData(deviceAbilityMap, staffAbilityMap);
    }

 

 

你可能感兴趣的:(@async注解的使用)