pom引用
<!--阿里大魚-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.5</version>
</dependency>
<!--阿里大魚-->
異步線程發送短信
package com.sxapp.message.handler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.sxapp.message.constant.SmsTemplateCodeEnum;
import com.sxapp.message.constant.SmsType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Dirk
* @Description
* @Date Create at 2019-07-09 14:04
*/
public class SendSmsHandler implements Runnable {
private static final Logger log = LoggerFactory.getLogger(SendSmsHandler.class);
private String phone;
private String templateParam;
private SmsType type;
public SendSmsHandler(String phone, String templateParam, SmsType type) {
this.phone = phone;
this.templateParam = templateParam;
this.type = type;
}
/**
* 阿里大魚發送短信
*
* @param phone 手機號
* @param templateParam 短信模板變量
* @param type 驗證碼類型
*/
private void sendSms(String phone, String templateParam, SmsType type) {
String regionId = "";
String accessKeyId = "";
String accessSecret = "";
String signName = "";
DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessSecret);
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
request.putQueryParameter("PhoneNumbers", phone);
request.putQueryParameter("SignName", signName);
request.putQueryParameter("TemplateCode", SmsTemplateCodeEnum.getTemplateCode(type));
request.putQueryParameter("TemplateParam", templateParam);
CommonResponse response = null;
try {
response = client.getCommonResponse(request);
} catch (ClientException e) {
e.printStackTrace();
return;
}
if (response == null) {
log.error("send sms failed! phone is {}, type is {}", phone, type);
return;
}
JSONObject jsonObject = JSONObject.parseObject(response.getData());
String statusCode = jsonObject.getString("Code");
// 發送成功的狀態碼
String ok = "OK";
if (!ok.equals(statusCode)) {
log.error("send sms failed! phone is {}, response is {}", phone, response.getData());
return;
}
log.info("send sms success. phone is {}, response is {}", phone, response.getData());
}
@Override
public void run() {
sendSms(phone, templateParam, type);
}
public static class TemplateParamBuild {
private String code;
private String name;
private String phone;
public TemplateParamBuild code(String code) {
this.code = code;
return this;
}
public TemplateParamBuild name(String name) {
this.name = name;
return this;
}
public TemplateParamBuild phone(String newPhone) {
this.phone = newPhone;
return this;
}
public String getCode() {
return code;
}
public String getName() {
return name;
}
public String getPhone() {
return phone;
}
public String build() {
return JSON.toJSONString(this);
}
}
}
調用
public void sendSms(SmsRequest smsRequest) {
// 異步發送短信
SendSmsHandler handler = new SendSmsHandler(smsRequest.getPhone(),
new SendSmsHandler.TemplateParamBuild()
.code(smsRequest.getCode())
.name(smsRequest.getName())
.phone(smsRequest.getPhone())
.build(),
smsRequest.getSmsType());
threadPoolTaskExecutor.execute(handler);
}
SmsType
是對外暴露枚舉,SmsTemplateCodeEnum
是對應的帶有TemplateCode
屬性的枚舉