springboot异步方法调用

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));
    }


}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值