2012-09-24 35 views
0

我想創建web應用程序發送短信通過gsm調制解調器在JSP中首先我把目的地手機號碼和短信文本在url中並通過request.getparameter和第一條消息得到沒有問題,但發送通過referenshing同一頁面再次發送郵件時,我得到這個異常:org.smslib端口在使用異常

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: gnu.io.PortInUseException: org.smslib 
    at org.smslib.modem.SerialModemDriver.connectPort(SerialModemDriver.java:102) 
    at org.smslib.modem.AModemDriver.connect(AModemDriver.java:114) 
    at org.smslib.modem.ModemGateway.startGateway(ModemGateway.java:189) 
    at org.smslib.Service$1Starter.run(Service.java:276) 

我試圖阻止網關和停止服務,但不希望 我的代碼:

public boolean sendMessage(String strMobileNo,String strSMSText) 
    { 
     try 
     { 
      OutboundMessage outboundMessage=new OutboundMessage(); 
      SMS message=new SMS(); 
      SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200"); 
      gateway.setInbound(true); 
      gateway.setOutbound(true); 
      gateway.setSimPin("0000"); 
      gateway.setSmscNumber("+9647701144010"); 
      Service.getInstance().setOutboundMessageNotification(message); 
      Service.getInstance().addGateway(gateway); 
      Service.getInstance().startService(); 
      outboundMessage.setText(strSMSText); 
      outboundMessage.setRecipient(strMobileNo); 
      outboundMessage.setEncoding(Message.MessageEncodings.ENCUCS2); 
      //outboundMessage.setDeliveryDelay(5000); 
      Service.getInstance().sendMessage(outboundMessage); 
      System.out.println(outboundMessage); 
      gateway.stopGateway(); 
      Service.getInstance().stopService(); 
      Thread.sleep(10000); 
      return true; 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
      return false; 
    } 
    } 

回答

1

在你的代碼的問題是每次創建新的SerialModemGateway請求時,不應該這樣做。

嘗試讓SerialModemGateway gateway = new SerialModemGateway("modem.com1", "COM12", 9600, "Huawie", "EF200");語句只在您的應用程序運行時調用,而不是每次發出請求時調用。

+0

如果我在改變SerialModemGateway的定義靜態上課會解決問題嗎? – danarj

+0

只是使其超出方法,使其靜態或不會相同,有一件事會在session過期時調用'gateway.stopGateway();或其他流關閉操作。 –

1

嘗試這個Service.getInstance().stopService()去年代碼其工作的,你也可以再次運行

0

之前終止你的節目,我解決了這個問題, Service.getInstance().removeGateway(gateway);