开通短信的mns:
提示:这里是阿里云的短信mns监听开关
例如:马赛克部分是监听队列的名称
代码:
提示:先把sdk下载http://docs-aliyun.cn-hangzhou.oss.aliyun-inc.com/assets/attach/172325/cn_zh/1607068836340/dysms_mns_java_sdk%20%282%29.zip?spm=a2c4g.11186623.2.2.15fc633eO6Wd9x&file=dysms_mns_java_sdk%20%282%29.zip
只要以下几个导入项目
开始代码
创建接口继承阿里的监听
。
// An highlighted block
package com.sanxin.cloud.app.api.service;
import com.alicom.mns.tools.MessageListener;
public interface AlicomMessageListenterService extends MessageListener {
}
实现监听
。
// An highlighted block
package com.sanxin.cloud.app.api.service;
import com.aliyun.mns.model.Message;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class AlicomMessageListenterServiceImpl implements AlicomMessageListenterService{
ObjectMapper objectMapper = new ObjectMapper();
Logger logger=LoggerFactory.getLogger(AlicomMessageListenterServiceImpl.class);
@SuppressWarnings("unchecked")
@Override
public boolean dealMessage(Message message) {
try{
//接收到的信息,内容不过多解释
Map<String,Object> contentMap= objectMapper.readValue(message.getMessageBodyAsString(), HashMap.class);
String phoneNumber=(String)contentMap.get("phone_number");
String sendTime=(String)contentMap.get("send_time");
String content=(String)contentMap.get("content");
System.out.println(contentMap);
//TODO 请开始你的表演
}catch(Exception e){
// e.printStackTrace();
logger.error(e.getMessage());
}
// 返回true,则工具类自动删除已拉取的消息,阿里云也不再发送队列。
Boolean dealResult=true;
return dealResult;
}
}
package com.sanxin.cloud.app.api.service;
import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
@Component
@Order(value = 1)
public class AliMnsApplicationRunner implements ApplicationRunner {//启动项目即启动监听
@Value("${sms_aliyun_accessKeyId}")
String accessKeyId;// accessKeyId
@Value("${sms_aliyun_accessKeySecret}")
String accessKeySecret;// accessKeySecret
// @Value("${sms_reportQueue_name}")
//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
String smsReportQueueName;//即图1的马赛克部分
// @Value("${sms_upQueue_name}")
//短信回执:SmsReport,短信上行:SmsUp
String smsUpQueueName;//即图1的马赛克部分
@Autowired
private AlicomMessageListenterService alicomMessageListenerService;
@Override
public void run(ApplicationArguments args) throws Exception {
DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();
String messageType="SmsUp";
if(alicomMessageListenerService!=null){
puller.startReceiveMsg(accessKeyId,accessKeySecret ,messageType,smsUpQueueName, alicomMessageListenerService);
}
}
}
参考sdk和他人demo完成,不喜勿喷