package com.jianwu.task;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jianwu.dao.SmsHttpInfoDao;
import com.jianwu.domain.SmsHttpInfo;
import com.jianwu.domain.SmsPre;
import com.jianwu.domain.SubmitReq;
import com.jianwu.manager.SmsMtManager;
import com.jianwu.manager.SmsPreManager;
import com.jianwu.service.result.SmsResult;
import com.jianwu.sms.dto.SendStatus;
import com.jianwu.util.Md5Utils;
import com.nodewind.member.sso.common.HttpUtils;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Created by tookbra on 2016/7/27.
*/
@Component
@EnableScheduling
public class SmsTask {
@Autowired
private SmsPreManager smsPreManager;
@Autowired
private SmsMtManager smsMtManager;
@Autowired
private SmsHttpInfoDao smsHttpInfoDao;
private static final ExecutorService bizThreadPool = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("smsBiz-%s").build());
private static final Logger logger = LoggerFactory.getLogger(SmsTask.class);
// @PostConstruct
// public void sendSms() {
// bizThreadPool.submit(new Runnable() {
// @Override
// public void run() {
// while (true) {
// try {
// SmsPre smsPre = Constant.smsQueue.take();
// sendSms(smsPre);
// } catch (Exception e) {
// logger.error("发送号码异常:", e);
// e.printStackTrace();
// }
// }
// }
// });
// }
//@Scheduled(cron = "0 0/1 * * * ?")
@Scheduled(fixedDelay = 1000*60*1)
void doTimeTask() {
logger.debug("定时短信扫描");
List<SmsPre> smsPreList = smsPreManager.findDestineSms();
if (!CollectionUtils.isEmpty(smsPreList)) {
logger.debug("当前定时短信"+smsPreList.size()+"条");
for (SmsPre smsPre : smsPreList) {
sendSms(smsPre);
}
} else {
logger.debug("当前没有定时短信");
}
}
public SmsResult sendSms(SmsPre smsPre) {
SmsResult smsResult = new SmsResult();
List<String> phones = smsMtManager.getPhonesByPreId(smsPre.getPreId());
if (phones.isEmpty()) {
smsResult.setSuccess(false);
smsResult.setMsgGroup("");
smsResult.setRspcod("");
return smsResult;
}
logger.info("---smsPre.getCompanyId()---:" + smsPre.getCompanyId());
SmsHttpInfo smsHttpInfo = smsHttpInfoDao.findByCompanyId(smsPre.getCompanyId());
if (smsHttpInfo == null) {
return null;
}
logger.info("---smsHttpInfo---:" + smsHttpInfo.toString());
//发送下行短信请求
HttpUtils client = HttpUtils.getInstance();
SubmitReq submitReq = new SubmitReq();
submitReq.setEcName(smsHttpInfo.getEcName());
submitReq.setApId(smsHttpInfo.getApId());
submitReq.setSecretKey(smsHttpInfo.getSecretKey());
if (!Strings.isNullOrEmpty(smsPre.getSign())) {
submitReq.setContent(smsPre.getSmsContent().concat(smsPre.getSign()));
} else {
submitReq.setContent(smsPre.getSmsContent());
}
submitReq.setMobiles(StringUtils.join(phones, ","));
logger.info("---submitReq.getMobiles()---" + submitReq.getMobiles());
submitReq.setAddSerial(smsHttpInfo.getAddSerial());
submitReq.setSign(smsHttpInfo.getSign());
//以下stringBuffer顺序不能打乱
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append(submitReq.getEcName());
stringBuffer.append(submitReq.getApId());
stringBuffer.append(submitReq.getSecretKey());
stringBuffer.append(submitReq.getMobiles());
stringBuffer.append(submitReq.getContent());
stringBuffer.append(submitReq.getSign());
stringBuffer.append(submitReq.getAddSerial());
submitReq.setMac(Md5Utils.digest(stringBuffer.toString()));
String reqText = JSON.toJSONString(submitReq);
logger.info("---reqText---:" + reqText);
//加密
String encode = Base64.encodeBase64String(reqText.getBytes());
logger.info("---encode---:" + encode);
String msg = client.sendHttpPost(smsHttpInfo.getSmsUrl(), encode, "application/json");
logger.info("---msg---:" + msg);
JSONObject json = JSON.parseObject(msg);
if (json.containsKey("rspcod")) {
smsResult.setRspcod(json.getString("rspcod"));
}
if (json.containsKey("msgGroup")) {
smsResult.setMsgGroup(json.getString("msgGroup"));
}
if (json.containsKey("success")) {
smsResult.setSuccess(json.getBoolean("success"));
}
logger.info(smsResult.getRspcod() + "," + smsResult.getMsgGroup() + "," + smsResult.isSuccess());
smsPre.setProcessResult(SendStatus.SENT.getValue());
smsPreManager.update(smsPre);
return smsResult;
}
}
、、、、、、、、、、、、、、、、、、、、、、、、、
package com.jianwu.sms.task;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.jianwu.sms.codec.cmpp.msg.sp.CmppSubmitRequestMessage;
import com.jianwu.sms.common.Constant;
import com.jianwu.sms.handler.SubmitMsgHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
/**
* Created by tookbra on 2016/7/27.
*/
@Component
public class SmsTask extends Task {
private static final ExecutorService bizThreadPool = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("smsBiz-%s").build());
private static final Logger logger = LoggerFactory.getLogger(SmsTask.class);
static SpeedControl control = new SpeedControl();
@PostConstruct
public void sendSms() {
bizThreadPool.submit(new Runnable() {
@Override
public void run() {
while (true) {
try {
List<CmppSubmitRequestMessage> sendList = Constant.smsQueue.take();
for (CmppSubmitRequestMessage cmppSubmitRequestMessage : sendList) {
logger.info("发送号码:{}", cmppSubmitRequestMessage.getDestterminalId()[0]);
submitMsgHandler.sendSms(cmppSubmitRequestMessage);
control.control(cmppSubmitRequestMessage.getMsgContent());
}
control.clear();
} catch (Exception e) {
logger.error("发送号码异常:", e);
e.printStackTrace();
}
}
}
});
}
}