佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 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,所以可能没办法告诉你哪里出现了问题。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 28-11-2008 11:13 AM | 显示全部楼层 |阅读模式
嗨,不知道这里有人用过 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()
  1. ipAddress = "202.160.42.227";
  2.             port = 5018;

  3.             TCPIPConnection connection = new TCPIPConnection(ipAddress, port);
  4.             connection.setReceiveTimeout(10*60000);
  5.             session = new Session(connection);

  6.             systemId = "666603";
  7.             password = "mach123";

  8.             // set values
  9.             request.setSystemId(systemId);
  10.             request.setPassword(password);
  11.             request.setSystemType(systemType);
  12.             request.setInterfaceVersion((byte)0x34);
  13.             request.setAddressRange(addressRange);

  14.             // send the request
  15.             System.out.println("Bind request " + request.debugString());
  16.             
  17.             
  18.             if (asynchronous) {
  19.                 pduListener = new SMPPTestPDUEventListener(session);
  20.                 response = session.bind(request,pduListener);
  21.             } else {
  22.                 response = session.bind(request);
  23.             }
  24.             System.out.println("Bind response " + response.debugString());
  25.             
  26.             if (response.getCommandStatus() == Data.ESME_ROK) {
  27.                 bound = true;
  28.             }
复制代码


function receive()
  1. long receiveTimeout = 10*60000;
  2. PDU pdu = null;
  3.             System.out.print(name + "# Going to receive a PDU. ");
  4.             if (receiveTimeout == Data.RECEIVE_BLOCKING) {
  5.                 System.out.print("The receive is blocking, i.e. the application "+
  6.                                  "will stop until a PDU will be received.");
  7.                 insertLog("The receive is blocking, i.e. the application "+
  8.                                  "will stop until a PDU will be received.");
  9.             } else {
  10.                 System.out.print("The receive timeout is "+(receiveTimeout/1000)+" sec.");
  11.                 insertLog("The receive timeout is " + receiveTimeout + " milisec");
  12.             }
  13.             System.out.println();
  14.             if (asynchronous) {
  15.                 ServerPDUEvent pduEvent =
  16.                     pduListener.getRequestEvent(receiveTimeout);
  17.                 if (pduEvent != null) {
  18.                     pdu = pduEvent.getPDU();
  19.                 }
  20.             } else {
  21.                 pdu = session.receive(receiveTimeout);
  22.             }
  23.             if (pdu != null) {
  24.                 System.out.println(name + "# Received PDU "+pdu.debugString());
  25.                 if (pdu.isRequest()) {
  26.                     Response response = ((Request)pdu).getResponse();
  27.                     // respond with default response
  28.                     rtnMsg = response.debugString();
  29.                 }
  30.             } else {
  31.                 System.out.println(name + "# No PDU received this time.");
  32.             }
复制代码


然后,我就这样
processor class
  1. public processor extends Thread{
  2.        public processor(){

  3.        }

  4.        public void run(){
  5.                 SMSC smsc = new SMSC(ip, port);
  6.                 if (smsc.connect()){
  7.                            smsc.receive();
  8.                 }
  9.                
  10.         }

  11. }
复制代码


然后,就弄了很多个 processor 来跑
  1. while(true){
  2.         processor ps = new processor();
  3.         ps.start();

  4.         Thread.sleep(5000);

  5.         processor ps = new processor();
  6.         ps.start();

  7.         Thread.sleep(5000);

  8.         processor ps = new processor();
  9.         ps.start();

  10.         Thread.sleep(5000);


  11. }
复制代码


第一轮的 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.
  1. class Processor extends Thread {
  2. public static SMSC smsc;
  3. static String ip = "xxxx";
  4. static int port  = 123

  5. static {
  6.     smsc = new SMSC(ip, port);
  7.   }

  8. public static void main(String[] args) throws XXXXXn {
  9.       new Processor().start();

  10.       for(;{
  11.            new ReceiveProcessor().start();
  12.        }
  13. }
  14. public void run() {
  15.    sychronized(smsc){
  16.            while(!isInterrupted()){
  17.                     if(smsc.isBound())
  18.                            smsc.disconnect();
  19.                     smsc.connect();
  20.                     smsc.wait();
  21.             }
  22.     }
  23.    }

  24.   public class ReceiveProcessor extends Thread {
  25.         public void run(){
  26.               synchronized(smsc){
  27.                     if(smsc.isBound()){
  28.                            try {
  29.                                 smsc.receive();
  30.                            }catch(IOXXXXXn ex){
  31.                                  smsc.notify(); //if IOXXXXXn, try to reconnect
  32.                            }catch(XXXXXn e){ //handle other XXXXXn }
  33.                      }else{ smsc.notify(); }
  34.                }
  35.         }
  36.   }
  37. }

  38.   
  39. }
复制代码
以上只是一个例子, 还没有test compile。这样的话,ReceiveProcessor thread每receive一次就会检查有没有XXXXXn,如果connection有问题的话,另一个thread就会handle reconnect;若是没有什么问题的话, 就从新receive.

希望能够帮到你.

[ 本帖最后由 苦瓜汤 于 4-12-2008 03:57 PM 编辑 ]
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 21-12-2025 05:11 PM , Processed in 0.107940 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表