SpringBoot多线程实现发送短信验证码

package com.zzb.thread.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {
    @Bean("asyncServiceExecutor")
    public ThreadPoolTaskExecutor asyncServiceExecutor() {//TaskExecutor不带返回值  ThreadPoolTaskExecutor带返回值
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor ();
        // 设置核心线程数
        // 指定了线程池中的线程数量,它的数量决定了添加的任务是开辟新的线程去执行,还是放到workQueue任务队列中去
        executor.setCorePoolSize(10);
        // 设置最大线程数
        // 指定了线程池中的最大线程数量,这个参数会根据你使用的workQueue任务队列的类型,决定线程池会开辟的最大线程数量
        executor.setMaxPoolSize(10);
        // 设置队列容量
        // new LinkedBlockingQueue<Runnable>();
        executor.setQueueCapacity(32);
        // 设置线程活跃时间(秒)
        // 当线程池中空闲线程数量超过corePoolSize时,多余的线程会在多长时间内被销毁
        executor.setKeepAliveSeconds(300);
        // 设置默认线程名称
        executor.setThreadNamePrefix("async-thread-");
        // 设置拒绝策略rejection-policy:当pool已经达到max size的时候,如何处理新任务 CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
        //new ThreadPoolExecutor.AbortPolicy());//银行满了,还有人进来,不处理这个人,抛出异常
        //new ThreadPoolExecutor.CallerRunsPolicy());//哪来的去哪里!
        //new ThreadPoolExecutor.DiscardPolicy());//银行满了,还有人进来,不处理这个人,不抛出异常
        //new ThreadPoolExecutor.DiscardOldestPolicy());//队列满了,尝试去和最早的竞争,也不会抛出异常!
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 等待所有任务结束后再关闭线程池
        // executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.initialize();
        return executor;
    }
}
package com.zzb.thread.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class ThreadService {
    @Async("asyncServiceExecutor")
    public void sendSms(String phone, int code) {
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(Thread.currentThread().getName() + "发送短信成功:" + code + ",手机号:" + phone);
    }
}
package com.zzb.thread.service;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class SmsService {
    @Autowired
    private ThreadService threadService;

    public String sendSms(String phone) {
        /**
         * 1. 调用短信平台 发送短信  如果发送成功,将验证码存入redis,redis要有过期时间
         * 2. 发送成功,返回成功
         */
        //短信验证码 要生成
        int code = RandomUtils.nextInt(100000, 999999);
        log.info("短信验证码:" + code);
        //放入线程池执行,不影响当前的业务,立马返回
        threadService.sendSms(phone, code);

        return "success";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值