我正在使用 Google 的 JSMPP 从 SMS 服务中心接收 SMS 消息。有时,我的程序停止从 SMSC 接收短信,我必须关闭程序并重新打开程序。然后来自 SMSC 的排队 SMSes 开始接收。这会在一段时间后发生,比如 7 或 8 小时后。这是我用过的代码
SMPP 初始化代码
l.info("SMPP Initialization");
SMPPSession s = new SMPPSession();
s.setMessageReceiverListener(new Receive(s));
s.connectAndBind(Settings.smsc_host,Settings.smsc_port, BindType.BIND_TRX,
Settings.smsc_user, Settings.smsc_password,Settings.smsc_msg_setting, TypeOfNumber.UNKNOWN,
NumberingPlanIndicator.UNKNOWN, null, Settings.smsc_timeout);
ProcessSMS.s = s;
l.info("SMPP Initialization Success");
短信接收代码
package sms;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.apache.log4j.Logger;
import org.jsmpp.bean.AlertNotification;
import org.jsmpp.bean.DataSm;
import org.jsmpp.bean.DeliverSm;
import org.jsmpp.bean.MessageType;
import org.jsmpp.extra.ProcessRequestException;
import org.jsmpp.session.DataSmResult;
import org.jsmpp.session.MessageReceiverListener;
import org.jsmpp.session.Session;
import processor.ProcessSMS;
public class Receive implements MessageReceiverListener {
private static final ExecutorService pool = Executors.newFixedThreadPool(10);
private static final Logger l = Logger.getLogger(Receive.class);
private Thread thread;
public Receive(){
super();
}
public Receive(Session s){
this();
}
@Override
public DataSmResult onAcceptDataSm(DataSm arg0, Session arg1)
throws ProcessRequestException {
return null;
}
@Override
public void onAcceptAlertNotification(AlertNotification arg0) {
}
@Override
public void onAcceptDeliverSm(DeliverSm arg0)
throws ProcessRequestException {
l.info("Received SMS " + arg0);
if(MessageType.SMSC_DEL_RECEIPT.containedIn(arg0.getEsmClass())){
}else{
pool.submit(new ProcessSMS(arg0));
//thread = new Thread(new ProcessSMS(arg0));
//thread.start();
}
}
}
这是状态更改类
package sms;
import java.io.IOException;
import global.Shared;
import log.SMSCStateLogger;
import org.jsmpp.bean.BindType;
import org.jsmpp.bean.NumberingPlanIndicator;
import org.jsmpp.bean.TypeOfNumber;
import org.jsmpp.extra.SessionState;
import org.jsmpp.session.SMPPSession;
import org.jsmpp.session.Session;
import org.jsmpp.session.SessionStateListener;
import processor.ProcessSMS;
import settings.Settings;
public class StateChange implements SessionStateListener{
private static SMSCStateLogger l = new SMSCStateLogger(StateChange.class);
private Session s;
@Override
public void onStateChange(SessionState arg0, SessionState arg1, Object arg2) {
//arg0 = new State
//arg1 = old State
if(!arg0.isBound() && arg1.isBound()){
int con = Shared.getNextReConnectInterval();
l.info("State changed from " + arg1 + " to " + arg0 + " on " + arg2);
while(true){
l.info("Re Connection in " + con + " ms");
try{
Thread.sleep(con);
}catch(InterruptedException iex){
l.fatal("Re Connection failed due to exception " + iex);
break;
}
s = new SMPPSession();
((SMPPSession) s).setMessageReceiverListener(new Receive(s));
s.addSessionStateListener(new StateChange());
try{
((SMPPSession) s).connectAndBind(Settings.smsc_host,Settings.smsc_port, BindType.BIND_TRX,
Settings.smsc_user, Settings.smsc_password,Settings.smsc_msg_setting, TypeOfNumber.UNKNOWN,
NumberingPlanIndicator.UNKNOWN, null, Settings.smsc_timeout);
}catch(IOException ioex){
l.fatal("Connection failed due to " + ioex.getMessage());
}
ProcessSMS.s = (SMPPSession) s;
l.info("Re Connection success");
break;
}
}
}
}
有人知道发生了什么吗?