|
查看: 2235|回复: 4
|
JAVA opensmpp 的 receive timeout
[复制链接]
|
|
|
发表于 28-11-2008 03:44 PM
|
显示全部楼层
有没有抓Exception?? Connection class 好像有receiveTimeout和commsTimeout,可以看看javadoc有什么分别。
如果那些classes不是thread-safe的话,用thread就可能会有问题了。所以,你需要参考以下doc看看multithreading是否能够派上用场。
如果方便的话,放多一些code上来。
PS: 我没用过smpp,所以可能没办法告诉你哪里出现了问题。 |
|
|
|
|
|
|
|
|
|
|
嗨,不知道这里有人用过 java 的 opensmpp api 吗?
我在 binding connection 时:
TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
connection.setReceiveTimeout(10*60000);
session =new Session(connection);
虽然 timeout 已经 set 去了10分钟,可是差不多30sec的 idle, link 就会自己断掉.....
请问有什么方法可以解决吗?
我试过了每20秒就 unbind 然后再binding回connection,可是……如果这样的话,如果有 sms 在这期间进来,我的 app 就拿不到了……
曾经也试过些了10多个thread 来 listen incoming sms (每20秒restart connection ),可是不知道为什么也是头头的30秒 ok,如果idle 太长,所有的 listening thread 也不function 了(就算connection restarted)
在想……会不会这是 opensmpp的bugs?
*我公司的 network 是 lease line, 基本上是非常稳定的……不可能莫名其妙一直断线 |
|
|
|
|
|
|
|
|
|
|
发表于 1-12-2008 05:07 PM
|
显示全部楼层
|
可能SERVER断掉你的CONNECTION..... |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 2-12-2008 05:17 PM
|
显示全部楼层
function Connect()
- ipAddress = "202.160.42.227";
- port = 5018;
- TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
- connection.setReceiveTimeout(10*60000);
- session = new Session(connection);
- systemId = "666603";
- password = "mach123";
- // set values
- request.setSystemId(systemId);
- request.setPassword(password);
- request.setSystemType(systemType);
- request.setInterfaceVersion((byte)0x34);
- request.setAddressRange(addressRange);
- // send the request
- System.out.println("Bind request " + request.debugString());
-
-
- if (asynchronous) {
- pduListener = new SMPPTestPDUEventListener(session);
- response = session.bind(request,pduListener);
- } else {
- response = session.bind(request);
- }
- System.out.println("Bind response " + response.debugString());
-
- if (response.getCommandStatus() == Data.ESME_ROK) {
- bound = true;
- }
复制代码
function receive()
- long receiveTimeout = 10*60000;
- PDU pdu = null;
- System.out.print(name + "# Going to receive a PDU. ");
- if (receiveTimeout == Data.RECEIVE_BLOCKING) {
- System.out.print("The receive is blocking, i.e. the application "+
- "will stop until a PDU will be received.");
- insertLog("The receive is blocking, i.e. the application "+
- "will stop until a PDU will be received.");
- } else {
- System.out.print("The receive timeout is "+(receiveTimeout/1000)+" sec.");
- insertLog("The receive timeout is " + receiveTimeout + " milisec");
- }
- System.out.println();
- if (asynchronous) {
- ServerPDUEvent pduEvent =
- pduListener.getRequestEvent(receiveTimeout);
- if (pduEvent != null) {
- pdu = pduEvent.getPDU();
- }
- } else {
- pdu = session.receive(receiveTimeout);
- }
- if (pdu != null) {
- System.out.println(name + "# Received PDU "+pdu.debugString());
- if (pdu.isRequest()) {
- Response response = ((Request)pdu).getResponse();
- // respond with default response
- rtnMsg = response.debugString();
- }
- } else {
- System.out.println(name + "# No PDU received this time.");
- }
复制代码
然后,我就这样
processor class
- public processor extends Thread{
- public processor(){
- }
- public void run(){
- SMSC smsc = new SMSC(ip, port);
- if (smsc.connect()){
- smsc.receive();
- }
-
- }
- }
复制代码
然后,就弄了很多个 processor 来跑
- while(true){
- processor ps = new processor();
- ps.start();
- Thread.sleep(5000);
- processor ps = new processor();
- ps.start();
- Thread.sleep(5000);
- processor ps = new processor();
- ps.start();
- Thread.sleep(5000);
- }
复制代码
第一轮的 looping, 3个 processor 都很ok,可以收到 sms, 可是当 15秒过后,loop! 全部不能 accept sms 了(虽然 status 是 connected 和 waiting incoming sms)
也问了Telco, 他们说他的 smsc 不会 disconnect 我们的
@_@
纳闷中……现在转用 nowSMS (因为 service 急着要用了),反而没有问题
还真的怀疑是不是 opensmpp api 的问题
=.=a |
|
|
|
|
|
|
|
|
|
|
发表于 4-12-2008 03:53 PM
|
显示全部楼层
PS: 不懂问什么,Cari把"E x c e p t i o"改成了"XXXXX"
虽然不曾用过opensmpp,但是看了你的代码和javadoc之后发现你的方法有点问题...
第一,
根据javadoc, received()这个method又提到
This is blocking receive, caller will wait until a PDU will be received.
意思即在receiving的时候running thread会blocked,而且这个方法只是receive一次,并没有loop。
第二,
没有handle TimeoutXXXXXn。不过这个并不是造成你问题的原因。
第三,
若是要用multithreads的话,connection和receiving应该放在两个thread.- class Processor extends Thread {
- public static SMSC smsc;
- static String ip = "xxxx";
- static int port = 123
- static {
- smsc = new SMSC(ip, port);
- }
-
- public static void main(String[] args) throws XXXXXn {
- new Processor().start();
- for(;{
- new ReceiveProcessor().start();
- }
- }
- public void run() {
- sychronized(smsc){
- while(!isInterrupted()){
- if(smsc.isBound())
- smsc.disconnect();
- smsc.connect();
- smsc.wait();
- }
- }
- }
- public class ReceiveProcessor extends Thread {
- public void run(){
- synchronized(smsc){
- if(smsc.isBound()){
- try {
- smsc.receive();
- }catch(IOXXXXXn ex){
- smsc.notify(); //if IOXXXXXn, try to reconnect
- }catch(XXXXXn e){ //handle other XXXXXn }
- }else{ smsc.notify(); }
- }
- }
- }
- }
-
- }
复制代码 以上只是一个例子, 还没有test compile。这样的话,ReceiveProcessor thread每receive一次就会检查有没有XXXXXn,如果connection有问题的话,另一个thread就会handle reconnect;若是没有什么问题的话, 就从新receive.
希望能够帮到你.
[ 本帖最后由 苦瓜汤 于 4-12-2008 03:57 PM 编辑 ] |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|