|
@@ -8,10 +8,14 @@ import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
import com.aliyuncs.exceptions.ClientException;
|
|
|
import com.aliyuncs.profile.DefaultProfile;
|
|
|
import com.aliyuncs.profile.IClientProfile;
|
|
|
+import org.apache.commons.lang3.concurrent.BasicThreadFactory;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
import java.util.*;
|
|
|
+import java.util.concurrent.ScheduledExecutorService;
|
|
|
+import java.util.concurrent.ScheduledThreadPoolExecutor;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
/**
|
|
|
* 阿里发送短信验证码工具类
|
|
@@ -92,7 +96,7 @@ public class AliSMSUtil {
|
|
|
map.put("msg","验证类型不存在");
|
|
|
return map;
|
|
|
}
|
|
|
- String code = GetCode(phone);
|
|
|
+ String code = getCode(phone);
|
|
|
String params = "{\"code\":\""+code+"\"}";
|
|
|
map = sendSms(phone,params,SIGN_NAME,templateCode);
|
|
|
return map;
|
|
@@ -184,14 +188,14 @@ public class AliSMSUtil {
|
|
|
* @param phone
|
|
|
* @return
|
|
|
*/
|
|
|
- private String GetCode(String phone){
|
|
|
- String key = GetNum();
|
|
|
+ private String getCode(String phone){
|
|
|
+ String key = getNum();
|
|
|
CodeMap.put(phone,key);
|
|
|
CodeMapTime.put(phone,System.currentTimeMillis());
|
|
|
return key;
|
|
|
}
|
|
|
|
|
|
- private String GetNum(){
|
|
|
+ private String getNum(){
|
|
|
int number = 899999;
|
|
|
Random random = new Random();
|
|
|
int num =random.nextInt(number)+100000;
|
|
@@ -208,8 +212,12 @@ public class AliSMSUtil {
|
|
|
}
|
|
|
|
|
|
static{
|
|
|
- Timer tt=new Timer();//定时类
|
|
|
- tt.schedule(new TimerTask(){//创建一个定时任务
|
|
|
+ // 用Executor代替new timer
|
|
|
+ // 1是核心线程数量,后面参数是一个线程工厂,采用了建造者模式创建
|
|
|
+ // 可以通过线程工厂给每个创建出来的线程设置符合业务的名字。
|
|
|
+ ScheduledExecutorService executorService = new ScheduledThreadPoolExecutor(1,
|
|
|
+ new BasicThreadFactory.Builder().namingPattern("aliSMS-schedule-pool-%d").daemon(true).build());
|
|
|
+ executorService.scheduleAtFixedRate(new Runnable() {
|
|
|
@Override
|
|
|
public void run() {
|
|
|
try {
|
|
@@ -234,7 +242,7 @@ public class AliSMSUtil {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- }, START,INTERVAL);//从0秒开始,每隔10秒执行一次
|
|
|
+ },START,INTERVAL, TimeUnit.MILLISECONDS);
|
|
|
}
|
|
|
|
|
|
}
|