本文主要是介绍阿里大鱼异步发送短信,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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());}@Overridepublic 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
属性的枚举
这篇关于阿里大鱼异步发送短信的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!