📄 boss12530svcimpl.java
字号:
package com.asiainfo.batchsend.sms.boss;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.log4j.Logger;
import com.asiainfo.batchsend.model.SmsFwPush;
import com.asiainfo.batchsend.sms.boss.boss12503.Boss12530Error;
import com.asiainfo.batchsend.sms.boss.boss12503.Inser12530ErrorSvc;
import com.asiainfo.batchsend.util.Tool;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Company: Asiainfo Technologies(China),Inc </p>
* <p>Date: Nov 6, 2007 3:55:51 PM </p>
* <p>Email to: jiangyl@asiainfo.com </p>
* @author jiangyl
* @version 1.0
*/
public class Boss12530SvcImpl implements BossSvc {
private static final Logger log = Logger.getLogger(BossSendSvcImpl.class);
//判断是否有可发送的任务
String hasLoadData = " select id, target_phone_number, src_msisdn,camp_channel_id, t.send_msg " +
" from sms_fw_push t where status = ? and target_phone_number = ? fetch first 2 rows only ";
public boolean hasLoadData() {
Connection conn = null;
boolean flag = false;
try {
conn = DBCon.getShenYunDataSource().getConnection();
PreparedStatement pLoad = conn.prepareStatement(hasLoadData);
pLoad.setInt(1, TODO);
pLoad.setString(2, Config12350.NUMBER);
ResultSet rs = pLoad.executeQuery();
while(rs.next()){
flag = true;
break;
}
rs.close();
conn.close();
} catch (SQLException e) {
log.error("深运平台在从sms_push表中检查是否有发送数据时发生严重的错误",e);
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
return flag;
}
//这里只根据任务提取数据,主要是因为根据调度任务传进来的taskId已经具备发送的条件
String sql = " select id, target_phone_number, src_msisdn,camp_channel_id, t.send_msg from sms_fw_push t" +
" where status = ? and target_phone_number = ? fetch first 10 rows only ";
String udpate = " update sms_fw_push t set t.status = ? where t.id = ? ";
public List loadData() {
threadSleep(Config12350.sleepAfter12530Send);
List list = new LinkedList();
Connection conn = null;
try {
conn = DBCon.getShenYunDataSource().getConnection();
conn.setAutoCommit(false);
PreparedStatement pLoad = conn.prepareStatement(sql);
PreparedStatement ups = conn.prepareStatement(udpate);
pLoad.setInt(1, TODO);
pLoad.setString(2, Config12350.NUMBER);
ResultSet rs = pLoad.executeQuery();
while(rs.next()){
SmsFwPush sms = new SmsFwPush();
sms.setId( rs.getLong("id"));
sms.setTargetPhoneNumber(rs.getString("target_phone_number"));
sms.setCampChannelId( rs.getString("camp_channel_id"));
sms.setSendMsg( rs.getString("send_msg"));
sms.setSrcMsisdn(rs.getString("src_msisdn"));
list.add(sms);
ups.setInt(1, DONE);
ups.setLong(2, sms.getId());
ups.addBatch();
}
ups.executeBatch();
pLoad.close();
ups.close();
conn.commit();
System.out.println("深运平台12530从sms_fw_push表在装载数据时获取" + list.size() + "条记录");
} catch (Exception e) {
try{
conn.rollback();
}catch(Exception ee){
}
log.error("深运平台在sms_fw_push装载数据时发生严重的错误",e);
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
return list;
}
public boolean sendData(List list){
List error = new LinkedList();
HttpClient httpclient = new HttpClient();
PostMethod postMethod = new PostMethod(Config12350.URL);
postMethod.addRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=GBK");
try{
for ( int i = 0; i < list.size(); i++ ){
SmsFwPush sms = (SmsFwPush)list.get(i);
String seq = Tool.getSeq(Config12350.SEQ);
String sidpwd = Tool.MD5( seq + Config12350.PWD );
NameValuePair pair0= new NameValuePair("sid",Config12350.SEQ );
NameValuePair pair1= new NameValuePair("seq",seq);
NameValuePair pair2= new NameValuePair("sidpwd",sidpwd);
NameValuePair pair3= new NameValuePair("randomsessionkey", sms.getSrcMsisdn());
NameValuePair pair4= new NameValuePair("tonecode", sms.getSendMsg());
NameValuePair pair5= new NameValuePair("flag", "0");
NameValuePair[] msg = new NameValuePair[]{pair0, pair1, pair2, pair3,pair4,pair5};
postMethod.setRequestBody(msg);
String result ="";
try{
httpclient.executeMethod(postMethod);
result = postMethod.getResponseBodyAsString().trim();
if ( result.toLowerCase().indexOf("downtone") == -1 ){
this.updateFail(sms.getId());
int k = result.indexOf("<returncode>");
if ( k > 0 ){
String code = result.substring(k+12,result.indexOf("</returncode>"));
code = code.replaceAll("\r", "");
code = code.replaceAll("\n", "");
String name = Boss12530Error.getNameByCode(code);
Inser12530ErrorSvc.insert( sms.getSrcMsisdn(), name, code);
}
}
}catch(Exception e){
this.updateFail(sms.getId());
log.error("彩铃发送API发送严重的错误",e);
}
}
log.debug("成功地发送了 " + (list.size() - error.size()) + " 记录");
}catch(Exception e){
log.error("彩铃发送API发送严重的错误",e);
}finally{
postMethod.releaseConnection();
}
return true;
}
//发送成功之后如何处理数据
public boolean successDone(List list) {
return true;
}
//发送失败后需要如何处理数据
public void failDone(List list) {
}
String upFail = "update sms_fw_push t set t.status = 2 where t.id =?";
private void updateFail(long id){
Connection conn = null;
try {
conn = DBCon.getShenYunDataSource().getConnection();
PreparedStatement fail = conn.prepareStatement(upFail);
fail.setLong(1, id);
fail.executeUpdate();
conn.close();
} catch (SQLException e) {
log.error("深运平台在转发彩玲平台时发送严重的错误",e);
} finally {
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
}
}
}
}
private void threadSleep(long msec) {
try {
Thread.sleep(msec);
} catch (Exception e) {
}
}
public String getChannel() {
return null;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -