现在需要构建一个通用的短信发送服务(独立于品优购的单独工程),接收 activeMQ 的消息(MAP类型) 消息包括手机号(mobile)、短信模板号(template_code)、签名(sign_name)、参数字符串(param )。
代码实现(1)创建工程 pyg-sms(JAR 工程),POM 文件引入依赖
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-activemq
org.springframework.boot
spring-boot-devtools
com.aliyun
aliyun-java-sdk-core
3.2.8
com.aliyun
aliyun-java-sdk-dysmsapi
1.0.0-SNAPSHOT
com.pinyougou
pyg-common
0.0.1-SNAPSHOT
org.apache.activemq
activemq-pool
5.13.3
(2)创建引导类
package com.pyg;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SmsApplication {
public static void main(String[] args) {
//入口
SpringApplication.run(SmsApplication.class, args);
}
}
(3)创建配置文件 application.properties
server.port=90
#activeMQ url
spring.activemq.broker-url=tcp://192.168.25.128:61616
spring.activemq.pool.enabled=true
spring.activemq.packages.trust-all=true
spring.activemq.pool.max-connections=3000
#template code
template_code=你的模板code
#
accessKeyId=你的keyid
accessKeySecret=你的秘钥
(3)创建短信工具类
package com.pyg.utils;
public class SmsUtils {
//产品名称:云通信短信API产品,开发者无需替换
static final String product = "Dysmsapi";
//产品域名,开发者无需替换
static final String domain = "dysmsapi.aliyuncs.com";
// TODO 此处需要替换成开发者自己的AK(在阿里云访问控制台寻找)
// static final String accessKeyId = "LTAIdSQD6Aw6mgLV";
//final String accessKeySecret = "KFCVQ32sJmf3Yov8qRETNjX6jEfARR";
public static SendSmsResponse sendSms(String mobile,String signName,String templateCode,String code,String accessKeyId,String accessKeySecret) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(mobile);
//必填:短信签名-可在短信控制台中找到
request.setSignName(signName);
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(templateCode);
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为
request.setTemplateParam(code);
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
request.setOutId("yourOutId");
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
return sendSmsResponse;
}
public static QuerySendDetailsResponse querySendDetails(String mobile,String signName,String templateCode,String code,String accessKeyId,String accessKeySecret,String bizId) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//组装请求对象
QuerySendDetailsRequest request = new QuerySendDetailsRequest();
//必填-号码
request.setPhoneNumber(mobile);
//可选-流水号
request.setBizId(bizId);
//必填-发送日期 支持30天内记录查询,格式yyyyMMdd
SimpleDateFormat ft = new SimpleDateFormat("yyyyMMdd");
request.setSendDate(ft.format(new Date()));
//必填-页大小
request.setPageSize(10L);
//必填-当前页码从1开始计数
request.setCurrentPage(1L);
//hint 此处可能会抛出异常,注意catch
QuerySendDetailsResponse querySendDetailsResponse = acsClient.getAcsResponse(request);
return querySendDetailsResponse;
}
}
(4)消息监听类
@Component
public class SmsListener {
@Autowired
private SmsUtil smsUtil;
@JmsListener(destination="sms")
public void sendSms(Map map){
try {
SendSmsResponse response = smsUtil.sendSms(
map.get("mobile"),
map.get("template_code"),
map.get("sign_name"),
map.get("param") );
System.out.println("Code=" + response.getCode());
System.out.println("Message=" + response.getMessage());
System.out.println("RequestId=" + response.getRequestId());
System.out.println("BizId=" + response.getBizId());
} catch (ClientException e) {
e.printStackTrace();
}
}
}
代码测试
新建SMSController
package com.pyg.controller;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSON;
@RestController
public class SmsController {
// 注入jms模版对象
@Autowired
private JmsTemplate jmsTemplate;
/**
* 需求:测试短信网关服务: 发送消息 队列:queue
*/
@RequestMapping("/sendSms")
public void sendSms() {
// 创建map对象
Map mapMessage = new HashMap();
// 放入消息
// 手机号
mapMessage.put("mobile", "18926243061");
// 签名
mapMessage.put("signName", "黑马");
// 创建map
Map map = new HashMap();
map.put("code", "787878");
mapMessage.put("number", JSON.toJSONString(map));
// 给短信发送网关服务发送消息 pyg-sms
jmsTemplate.convertAndSend("smsQueue",JSON.toJSONString(mapMessage));
}
}
启动 pyg_sms
地址栏输入:http://localhost:8088/sendsms.do
观察控制台输出 然后短信发送成功!