118 lines
4.0 KiB
Java
118 lines
4.0 KiB
Java
package com.tenwa.message;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.Date;
|
|
|
|
import jbo.com.tenwa.lease.comm.LB_SMS_NOTICE_TASKS;
|
|
|
|
import org.apache.commons.logging.Log;
|
|
import org.apache.commons.logging.LogFactory;
|
|
import org.quartz.Job;
|
|
import org.quartz.JobDetail;
|
|
import org.quartz.JobExecutionContext;
|
|
import org.quartz.JobExecutionException;
|
|
import org.quartz.Scheduler;
|
|
import org.quartz.Trigger;
|
|
import org.quartz.impl.StdSchedulerFactory;
|
|
|
|
import com.amarsoft.are.jbo.BizObject;
|
|
import com.amarsoft.are.jbo.BizObjectManager;
|
|
import com.amarsoft.are.jbo.JBOException;
|
|
import com.amarsoft.are.jbo.JBOFactory;
|
|
import com.amarsoft.are.jbo.JBOTransaction;
|
|
import com.tenwa.lease.util.SendMessageUtil;
|
|
|
|
public class JobSendMessage implements Job{
|
|
|
|
private Log logger = LogFactory.getLog(this.getClass());
|
|
private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
@Override
|
|
public void execute(JobExecutionContext context) throws JobExecutionException {
|
|
JBOTransaction tx = null;
|
|
try {
|
|
tx= JBOFactory.createJBOTransaction();
|
|
Scheduler sch = StdSchedulerFactory.getDefaultScheduler();
|
|
Trigger tri = context.getTrigger();
|
|
JobDetail jobDetail = sch.getJobDetail(tri.getJobKey());
|
|
BizObjectManager bomNotice = JBOFactory.getBizObjectManager(LB_SMS_NOTICE_TASKS.CLASS_NAME,tx);
|
|
BizObject notice = bomNotice.createQuery("id=:id").setParameter("id", jobDetail.getJobDataMap().getString("smsnoticeid")).getSingleResult(true);
|
|
int max = 1;//短信发送失败重新发送次数
|
|
if(notice != null){
|
|
if(notice.getAttribute("send_flag").getInt() == 0){
|
|
String result = null;
|
|
long pauseTime = 0;
|
|
try {
|
|
for(int i=0;i<max;i++){
|
|
result = SendMessageUtil.sendMessageByHttpClient(notice.getAttribute("phone_number").getString(), notice.getAttribute("sms_content").getString());
|
|
if(i>0){
|
|
break;
|
|
}
|
|
if("0".equals(result.substring(0,1))){
|
|
break;
|
|
}else{
|
|
logger.error("短信发送失败,5秒后重新发送...");
|
|
sch.pauseTrigger(tri.getKey());
|
|
pauseTime = new Date().getTime();
|
|
}
|
|
while(true){
|
|
if(new Date().getTime()-pauseTime >=5000){
|
|
sch.resumeTrigger(tri.getKey());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
logger.error("短信发送任务执行失败。", e);
|
|
result = "发送失败,发送遇到异常:" + e.getMessage();
|
|
}
|
|
notice.setAttributeValue("sms_type", "IMMEDIATELY");
|
|
notice.setAttributeValue("send_result", getResultCodeInfo(result));
|
|
notice.setAttributeValue("send_time", sdf.format(new Date()));
|
|
notice.setAttributeValue("send_flag", 1);
|
|
}
|
|
bomNotice.saveObject(notice);
|
|
}
|
|
tx.commit();
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
try {
|
|
tx.rollback();
|
|
} catch (JBOException e1) {
|
|
e1.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
public static String getResultCodeInfo(String code){
|
|
String codeMessage = "";
|
|
switch(code){
|
|
case "-1": codeMessage = "签权失败"; break;
|
|
case "-2": codeMessage = "未检索到被叫号码"; break;
|
|
case "-3": codeMessage = "被叫号码过多"; break;
|
|
case "-4": codeMessage = "内容未签名"; break;
|
|
case "-5": codeMessage = "内容过长"; break;
|
|
case "-6": codeMessage = "余额不足"; break;
|
|
case "-7": codeMessage = "暂停发送"; break;
|
|
case "-8": codeMessage = "保留"; break;
|
|
case "-9": codeMessage = "定时发送时间格式错误"; break;
|
|
case "-10": codeMessage = "下发内容为空"; break;
|
|
case "-11": codeMessage = "账户无效"; break;
|
|
case "-12": codeMessage = "Ip地址非法"; break;
|
|
case "-13": codeMessage = "操作频率快"; break;
|
|
case "-14": codeMessage = "操作失败"; break;
|
|
case "-15": codeMessage = "拓展码无效"; break;
|
|
case "-16": codeMessage = "取消定时,seqid错误"; break;
|
|
case "-18": codeMessage = "暂留"; break;
|
|
case "-19": codeMessage = "未开通上行"; break;
|
|
case "-20": codeMessage = "暂留"; break;
|
|
case "-21": codeMessage = "包含屏蔽词"; break;
|
|
default : codeMessage = code; break;
|
|
}
|
|
if("0".equals(codeMessage.substring(0,1))){
|
|
codeMessage = "操作成功";
|
|
}
|
|
return codeMessage;
|
|
}
|
|
}
|