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();
}
}
}
});
}
}

java注解类定时器的更多相关文章

  1. Java 工具类 —— 定时器(Timer/TimerTask)

    import java.util.Timer; import java.util.TimerTask; public class Timer { .... public void schedule(T ...

  2. 认识下java注解的实现原理

    1,什么是注解 注解也叫元数据,例如常见的@Override和@Deprecated,注解是JDK1.5版本开始引入的一个特性,用于对代码进行说明,可以对包.类.接口.字段.方法参数.局部变量等进行注 ...

  3. java自定义注解类

    一.前言 今天阅读帆哥代码的时候,看到了之前没有见过的新东西, 比如java自定义注解类,如何获取注解,如何反射内部类,this$0是什么意思? 于是乎,学习并整理了一下. 二.代码示例 import ...

  4. Java 注释类之常用元注解

    Java 注释类之常用元注解 自定义Java注释类时,我们使用方式如下:   @Retention(RetentionPolicy.RUNTIME)@Target(ElementType.METHOD ...

  5. Java中自定义注解类,并加以运用

    在Java框架中,经常会使用注解,而且还可以省很多事,来了解下自定义注解. 注解是一种能被添加到java代码中的元数据,类.方法.变量.参数和包都可以用注解来修饰.注解对于它所修饰的代码并没有直接的影 ...

  6. Java注解

    Java注解其实是代码里的特殊标记,使用其他工具可以对其进行处理.注解是一种元数据,起到了描述.配置的作用,生成文档,所有的注解都隐式地扩展自java.lang.annotation.Annotati ...

  7. 19.Java 注解

    19.Java注解 1.Java内置注解----注解代码 @Deprecated                                    //不推荐使用的过时方法 @Deprecated ...

  8. Java注解入门

    注解的分类   按运行机制分:   源码注解:只在源码中存在,编译后不存在 编译时注解:源码和编译后的class文件都存在(如@Override,@Deprecated,@SuppressWarnin ...

  9. java注解(Annotation)解析

    注解(Annotation)在java中应用非常广泛.它既能帮助我们在编码中减少错误,(比如最常见的Override注解),还可以帮助我们减少各种xml文件的配置,比如定义AOP切面用@AspectJ ...

随机推荐

  1. fscanf和feof的组合使用

    http://stackoverflow.com/questions/15719360/using-fscanf-using-feof 靶子代码: #include<stdio.h> vo ...

  2. Yii Framework2.0开发教程(3)数据库mysql入门

    沿用教程(2)的代码 第一步.在本地mysql数据库中新建数据库zhyoulun 第二步.在数据库中新建表并插入若干条数据 CREATE TABLE `country` ( `code` CHAR(2 ...

  3. unity, Global和Local编辑模式

    下图表示是在Local模式下: 下图表示是在Global模式下: 不要搞反.

  4. 理解x64代码模型

    原作者:Eli Bendersky http://eli.thegreenplace.net/2012/01/03/understanding-the-x64-code-models 在编写x64架构 ...

  5. 编程算法 - 高速排序算法 代码(C)

    高速排序算法 代码(C) 本文地址: http://blog.csdn.net/caroline_wendy 经典的高速排序算法, 作为一个编程者, 不论什么时候都要完整的手写. 代码: /* * m ...

  6. hdu 1711 Number Sequence KMP 基础题

    Number Sequence Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  7. atitit.html5动画特效----打水漂 ducks_and_drakes

    atitit.html5动画特效----打水漂  ducks_and_drakes 1. 原理 1 2. fly jquery插件 1 3. ---------code 2 4. 参考 4 1. 原理 ...

  8. js获取网页宽高

    <script> function getInfo() { var s = ""; s += " 网页可见区域宽:"+ document.body. ...

  9. 安装gstreamer开发环境

    ubuntu中安装gstreamer开发环境: * 安装gstreamer基本库,工具,以及插件 sudo apt--dev gstreamer-tools gstreamer0.-tools gst ...

  10. python学习之urlparse()

    urlparse(url, scheme='', allow_fragments=True) <scheme>://<netloc>/<path>;<para ...