簡訊貓發簡訊,不能關閉服務,報端口占用,javax.comm.PortInUseException: Port currently owned by org.smslib等問題
阿新 • • 發佈:2019-01-01
簡訊貓發簡訊呼叫串列埠發簡訊時,第一條可以正常傳送,第二條的時候就報錯了!大致意思是埠被佔用,沒有可用或找不到串列埠。
分析:其主要原因是,簡訊貓接上電腦即建立了連線,執行service.startService()相當於保持一個長連線,不能用程式斷開!最後把srv設為靜態屬性,每次點選按鈕只調用 service.sendMessage(msg)方法,就實現連續傳送簡訊了。
沒有改造之前的程式碼:
package com.amc.sggk.toSendSMS; import javax.jws.WebService; import javax.xml.ws.Endpoint; import org.smslib.AGateway; import org.smslib.IOutboundMessageNotification; import org.smslib.Library; import org.smslib.Message.MessageEncodings; import org.smslib.OutboundMessage; import org.smslib.Service; import org.smslib.modem.SerialModemGateway; @WebService public class SendMessage { public String doIt( String phone , String center) { try { OutboundNotification outboundNotification = new OutboundNotification(); System.out.println("開啟服務"); System.out.println(Library.getLibraryDescription()); System.out.println("Version: " + Library.getLibraryVersion()); /* modem.com1:閘道器ID(即簡訊貓埠編號) COM4:串列埠名稱(在window中以COMXX表示埠名稱,在linux,unix平臺下以ttyS0-N或ttyUSB0-N表示埠名稱),通過埠檢測程式得到可用的埠 115200:串列埠每秒傳送資料的bit位數,必須設定正確才可以正常傳送簡訊,可通過程式進行檢測。常用的有115200、9600 Huawei:簡訊貓生產廠商,不同的簡訊貓生產廠商smslib所封裝的AT指令介面會不一致,必須設定正確.常見的有Huawei、wavecom等廠商 最後一個引數表示裝置的型號,可選 */ SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "Quectel", "EC20"); gateway.setInbound(true); //設定true,表示該閘道器可以接收簡訊,根據需求修改 gateway.setOutbound(true);//設定true,表示該閘道器可以傳送簡訊,根據需求修改 gateway.setSimPin("0000");//sim卡鎖,一般預設為0000或1234 gateway.setSmscNumber("+8613010180500");//簡訊服務中心號碼 Service.getInstance().setOutboundMessageNotification(outboundNotification); //傳送簡訊成功後的回撥函方法 Service.getInstance().addGateway(gateway); //將閘道器新增到簡訊貓服務中 Service.getInstance().S.SERIAL_POLLING = true; System.out.println("啟動服務,進入簡訊傳送就緒狀態"); Service.getInstance().startService(); //啟動服務,進入簡訊傳送就緒狀態 OutboundMessage msg = new OutboundMessage(phone, center); //引數1:手機號碼 引數2:簡訊內容 msg.setEncoding(MessageEncodings.ENCUCS2);//這句話是發中文簡訊必須的 Service.getInstance().sendMessage(msg); //執行傳送簡訊 /*System.in.read();*/ Service.getInstance().stopService(); System.out.println("關閉服務"); return "傳送成功!"; } catch (Exception e) { e.printStackTrace(); return "簡訊模組呼叫失敗!"; } } /* 簡訊傳送成功後,呼叫該介面。並將傳送簡訊的閘道器和簡訊內容物件傳給process介面 */ public class OutboundNotification implements IOutboundMessageNotification { public void process(AGateway gateway, OutboundMessage msg) { System.out.println("簡訊傳送成功!"); System.out.println(msg); } } /*public static void main(String args[]) { SendMessage app = new SendMessage(); try { app.doIt("15652194735","ceshi"); } catch (Exception e) { e.printStackTrace(); } }*/ public static void main(String[] args) { /** * 引數1:服務的釋出地址(http://192.168.43.173/SendMsnService) * 引數2:服務的實現者 */ Endpoint.publish("http://192.168.4.52:8080/toSendMsn", new SendMessage()); } }
改造好的程式碼:
package com.amc.sggk.toSendSMS; import java.io.IOException; import javax.jws.WebService; import javax.xml.ws.Endpoint; import org.smslib.AGateway; import org.smslib.GatewayException; import org.smslib.IOutboundMessageNotification; import org.smslib.Library; import org.smslib.Message.MessageEncodings; import org.smslib.OutboundMessage; import org.smslib.Service; import org.smslib.TimeoutException; import org.smslib.modem.SerialModemGateway; @WebService public class SendMessage { public static Service service; static { try { init(); } catch (Exception e) { e.printStackTrace(); } } public static void init() throws Exception { //配置資訊只加載一遍 OutboundNotification outboundNotification = new OutboundNotification(); SerialModemGateway gateway = new SerialModemGateway("modem.com3", "COM3", 115200, "Quectel", "EC20"); gateway.setInbound(true); //設定true,表示該閘道器可以接收簡訊,根據需求修改 gateway.setOutbound(true);//設定true,表示該閘道器可以傳送簡訊,根據需求修改 gateway.setSimPin("0000");//sim卡鎖,一般預設為0000或1234 gateway.setSmscNumber("+8613010180500");//簡訊服務中心號碼 service.getInstance().setOutboundMessageNotification(outboundNotification); //傳送簡訊成功後的回撥函方法 service.getInstance().addGateway(gateway); //將閘道器新增到簡訊貓服務中 service.getInstance().S.SERIAL_POLLING = true; System.out.println("啟動服務,進入簡訊傳送就緒狀態"); service.getInstance().startService(); //啟動服務,進入簡訊傳送就緒狀態 } //呼叫發簡訊 public void doIt( String phone , String center) throws Exception { OutboundMessage msg = new OutboundMessage(phone, center); //引數1:手機號碼 引數2:簡訊內容 msg.setEncoding(MessageEncodings.ENCUCS2);//這句話是發中文簡訊必須的 service.getInstance().sendMessage(msg); //執行傳送簡訊 System.out.println("傳送成功"); //service.getInstance().stopService(); } /*public static void main(String[] args) { try { doIt("15652194735","123"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }*/ /* 簡訊傳送成功後,呼叫該介面。並將傳送簡訊的閘道器和簡訊內容物件傳給process介面 */ public static class OutboundNotification implements IOutboundMessageNotification { public void process(AGateway gateway, OutboundMessage msg) { System.out.println("簡訊傳送成功!"); System.out.println(msg); } } public static void main(String[] args) { /** * 引數1:服務的釋出地址(http://192.168.43.173/SendMsnService) * 引數2:服務的實現者 */ Endpoint.publish("http://192.168.4.52:8080/toSendMsn", new SendMessage()); } }