springboot异步方法调用
启动类开启异步
启动类添加注解
@EnableAsync
开启异步调用
@SpringBootApplication
@EnableAsync
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application .class, args);
}
}
编写异步配置AsyncConfig
package com.tel.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
/**
* 多线程执行task任务配置
*
* @author Mr.sean
* @description
* @date Created in11:55 2020/5/12
*/
@Configuration
public class AsyncConfig {
private int corePoolSize = 5;//线程池的核心数量(不会被销毁的)
private int maxPoolSize = 20;//线程池维护线程的最大数量(核心线程(corePoolSize)+非核心线程(KeepAliveTime)其中非核心线程是有生命周期的)
private int queueCapacity = 10;//队列的容量
/**
* @return
*/
@Bean
public Executor taskExecutor() {
//ThreadPoolTaskExecutor是Spring中的线程池
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
//设置线程存活时间(单位秒)
executor.setKeepAliveSeconds(6000);
//线程名称的前缀
executor.setThreadNamePrefix("this-excutor-");
executor.initialize();
return executor;
}
}
编写异步方法
实现方法加注解
@Async
使其成为一个异步方法
package com.tel.service;
import com.tel.dao.model.TelGift;
import com.tel.dao.vo.TelUserVO;
import java.util.List;
public interface AsyncService {
/**
* 描述:使用 @Async 实现异步调用
* @return
*/
public void asyncMethod();
package com.tel.service.impl;
import com.tel.common.base.ExecuteResult;
import com.tel.common.cache.CacheByRedis;
import com.tel.common.contant.TelConstants;
import com.tel.common.utils.JPushUtil;
import com.tel.dao.mapper.TelRoomMapper;
import com.tel.dao.model.*;
import com.tel.dao.vo.TelUserVO;
import com.tel.service.AsyncService;
import com.tel.service.TelLoginUserService;
import com.tel.service.TelRoomService;
import com.tel.service.TelUserFollowService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class JPushGiftAsyncImpl implements AsyncService {
private static final String result = "SUCCUSS"; //返回结果
private static Logger log = LoggerFactory.getLogger(JPushGiftAsyncImpl.class);
@Override
@Async
public void asyncMethod() {
Long startTime = System.currentTimeMillis();
try {
//睡眠10s进行测试
Thread.sleep(1000*10);
} catch (InterruptedException e) {
e.printStackTrace();
}
//异步实现的业务
。。。
Long endTime = System.currentTimeMillis();
System.out.println("异步调用成功,异步用时:"+(endTime -startTime));
}
测试
package com.tel;
import static org.junit.Assert.assertTrue;
import com.tel.common.utils.MD5Util;
import com.tel.common.utils.UrlUtil;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Unit test for simple App.
*/
public class AppTest
{
@Autowired
private AsyncService asyncService;
public static void main(String[] args) {
Long startTime = System.currentTimeMillis();
asyncService.asyncMethod();
Long endTime = System.currentTimeMillis();
System.out.println("方法执行时间为:"+(endTime-startTime));
}
}