我试图实现一个简单的侦听器,它从客户端接收 SMS 消息并将其显示在控制台中。我使用了来自https://tripleights.blogspot.com/2014/10/tutorial-java-jsmpp.html的代码,但遇到了两个我无法解决的问题:
监听器收到消息后停止。
当 IntelliJ Idea 尝试执行时
session.unbindAndClose();
会产生错误:[main] ERROR org.jsmpp.session.AbstractSession - Timeout waiting unbind response org.jsmpp.extra.ResponseTimeoutException: No response after waiting for 2000 millis when executing unbind with session f72860eb and sequence_number 4 at org.jsmpp.session.AbstractSession.executeSendCommand(AbstractSession.java:323) at org.jsmpp.session.AbstractSession.unbind(AbstractSession.java:403) at org.jsmpp.session.AbstractSession.unbindAndClose(AbstractSession.java:418) at SMPPServer.main(SMPPServer.java:100) Caused by: org.jsmpp.extra.ResponseTimeoutException: No response after 2000 millis at org.jsmpp.extra.PendingResponse.waitDone(PendingResponse.java:118) at org.jsmpp.session.AbstractSession.executeSendCommand(AbstractSession.java:317) ... 3 more [PDUReaderWorker-f72860eb] INFO org.jsmpp.session.SMPPServerSession - Reading PDU session f72860eb in state BOUND_TRX: Socket Closed
监听器代码如下:
import org.apache.log4j.BasicConfigurator;
import org.jsmpp.PDUStringException;
import org.jsmpp.SMPPConstant;
import org.jsmpp.bean.*;
import org.jsmpp.extra.ProcessRequestException;
import org.jsmpp.session.*;
import org.jsmpp.util.MessageIDGenerator;
import org.jsmpp.util.MessageId;
import org.jsmpp.util.RandomMessageIDGenerator;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class SMPPServer {
public static void main(String[] args) {
BasicConfigurator.configure();
try {
// prepare generator of Message ID
final MessageIDGenerator messageIdGenerator = new RandomMessageIDGenerator();
// prepare the message receiver
ServerMessageReceiverListener messageReceiverListener = new ServerMessageReceiverListener() {
public MessageId onAcceptSubmitSm(SubmitSm submitSm,
SMPPServerSession source)
throws ProcessRequestException {
System.out.println("Receiving message : " + new String(submitSm.getShortMessage()));
// need message_id to response submit_sm
return messageIdGenerator.newMessageId();
}
public QuerySmResult onAcceptQuerySm(QuerySm querySm,
SMPPServerSession source)
throws ProcessRequestException {
return null;
}
public SubmitMultiResult onAcceptSubmitMulti(
SubmitMulti submitMulti, SMPPServerSession source)
throws ProcessRequestException {
return null;
}
public DataSmResult onAcceptDataSm(DataSm dataSm, Session source)
throws ProcessRequestException {
return null;
}
public void onAcceptCancelSm(CancelSm cancelSm,
SMPPServerSession source)
throws ProcessRequestException {
}
public void onAcceptReplaceSm(ReplaceSm replaceSm,
SMPPServerSession source)
throws ProcessRequestException {
}
};
System.out.println("Listening ...");
SMPPServerSessionListener sessionListener = new SMPPServerSessionListener(6742);
// set all default ServerMessageReceiverListener for all accepted SMPPServerSessionListener
sessionListener.setMessageReceiverListener(messageReceiverListener);
// accepting connection, session still in OPEN state
SMPPServerSession session = sessionListener.accept();
// or we can set for each accepted session session.setMessageReceiverListener(messageReceiverListener)
System.out.println("Accept connection");
try {
BindRequest request = session.waitForBind(5000);
System.out.println("Receive bind request");
if (request.getSystemId().equals("smpp") &&
request.getPassword().equals("password")) {
// accepting request and send bind response immediately
System.out.println("Accepting bind request");
request.accept("sys");
try {
Thread.sleep(20000);
} catch (InterruptedException e) {
}
} else {
System.out.println("Rejecting bind request");
request.reject(SMPPConstant.STAT_ESME_RINVPASWD);
}
} catch (TimeoutException e) {
System.out.println("No binding request made after 5000 millisecond");
e.printStackTrace();
}
System.out.println("Closing session");
System.out.println("Closing session listener");
sessionListener.close();
session.unbindAndClose();
} catch (PDUStringException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如何解决?