| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- package cn.com.goldenwater.dcproj.config;
- import cn.com.goldenwater.dcproj.service.impl.tac.TacInspYearBatchGroupPersServiceImpl;
- import cn.com.goldenwater.dcproj.utils.SpringUtils;
- import com.alicom.mns.tools.DefaultAlicomMessagePuller;
- import com.alicom.mns.tools.MessageListener;
- import com.aliyun.mns.model.Message;
- import com.aliyuncs.exceptions.ClientException;
- import com.google.gson.Gson;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
- import org.springframework.stereotype.Component;
- import javax.annotation.PostConstruct;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- @Component
- @ConditionalOnProperty(value = "open.SmsReportPuller")
- public class SmsReportPuller {
- private Logger logger = LoggerFactory.getLogger(getClass());
- private static String apiKey = "LTAIcedbS4TWjMl3";
- private static String apiSecrect = "Pm7ZFyf195buPjH7YdxZGSbCadEX6p";
- private static String messageType = "SmsUp";
- private static String queueName = "Alicom-Queue-1210800490235418-SmsUp";
- @PostConstruct
- public void init() {
- // logger.info("接收上行短信start");
- DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();
- //设置异步线程池大小及任务队列的大小,还有无数据线程休眠时间
- puller.setConsumeMinThreadSize(6);
- puller.setConsumeMaxThreadSize(16);
- puller.setThreadQueueSize(200);
- puller.setPullMsgThreadSize(1);
- //和服务端联调问题时开启,平时无需开启,消耗性能
- puller.openDebugLog(false);
- /*
- * TODO 将messageType和queueName替换成您需要的消息类型名称和对应的队列名称
- *云通信产品下所有的回执消息类型:
- *1:短信回执:SmsReport
- *2:短息上行:SmsUp
- *3:语音呼叫:VoiceReport
- *4:流量直冲:FlowReport
- // */
- // String messageType = "SmsReport";//此处应该替换成相应产品的消息类型
- // String queueName = "";//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName,格式类似Alicom-Queue-xxxxxx-SmsReport
- try {
- puller.startReceiveMsg(apiKey, apiSecrect, messageType, queueName,new MyMessageListener());
- } catch (ClientException e) {
- logger.info("ClientException"+e.getErrMsg());
- } catch (ParseException e) {
- logger.info("ParseException"+e.getMessage());
- }catch(Exception e){
- logger.info("Exception"+e.getMessage());
- }
- }
- static class MyMessageListener implements MessageListener {
- private Gson gson=new Gson();
- private Logger logger = LoggerFactory.getLogger(getClass());
- private TacInspYearBatchGroupPersServiceImpl tacInspYearBatchGroupPersService= SpringUtils.getBean(TacInspYearBatchGroupPersServiceImpl.class);
- @Override
- public boolean dealMessage(Message message) {
- SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
- logger.info("message receiver time from mns:" + format.format(new Date()));
- logger.info("message handle: " + message.getReceiptHandle());
- logger.info("message body: " + message.getMessageBodyAsString());
- logger.info("message id: " + message.getMessageId());
- logger.info("message dequeue count:" + message.getDequeueCount());
- logger.info("Thread:" + Thread.currentThread().getName());
- try{
- Map<String,Object> contentMap=gson.fromJson(message.getMessageBodyAsString(), HashMap.class);
- //TODO 根据文档中具体的消息格式进行消息体的解析
- //TODO 这里开始编写您的业务代码
- int i = tacInspYearBatchGroupPersService.confirmerGroupPers(contentMap);
- logger.info("短信回复内容接收成功"+i);
- }catch(com.google.gson.JsonSyntaxException e){
- //理论上不会出现格式错误的情况,所以遇见格式错误的消息,只能先delete,否则重新推送也会一直报错
- return true;
- } catch (Throwable e) {
- //您自己的代码部分导致的异常,应该return false,这样消息不会被delete掉,而会根据策略进行重推
- return false;
- }
- //消息处理成功,返回true, SDK将调用MNS的delete方法将消息从队列中删除掉
- return true;
- }
- }
- }
|