gpt4 book ai didi

java - 在同一进程中使用 SMSLib 发送多条短信

转载 作者:搜寻专家 更新时间:2023-11-01 03:55:41 26 4
gpt4 key购买 nike

我正在使用 SMSLib 通过我的三星 gsm 调制解调器发送短信。我创建了一个单独的线程,每 20 秒从服务器获取一次消息如果它收到消息,它会调用 SendMessage。这是 SendMessage 的代码

public class SendMessage {
public boolean doIt(String num, String umsg) {

try {
OutboundNotification outboundNotification = new OutboundNotification();
System.out.println("Example: Send message from a serial gsm modem.");
System.out.println(Library.getLibraryDescription());
System.out.println("Version: " + Library.getLibraryVersion());
SerialModemGateway gateway = new SerialModemGateway("modem.com10","COM10", 115200, "Samsung", "");
gateway.setInbound(true);
gateway.setOutbound(true);
gateway.setSimPin("0000");

gateway.setSmscNumber("+919826012311");
Service.getInstance().setOutboundMessageNotification(outboundNotification);
Service.getInstance().addGateway(gateway);
Service.getInstance().startService();
System.out.println();
System.out.println("Modem Information:");
System.out.println(" Manufacturer: " + gateway.getManufacturer());
System.out.println(" Model: " + gateway.getModel());
System.out.println(" Serial No: " + gateway.getSerialNo());
System.out.println(" SIM IMSI: " + gateway.getImsi());
System.out.println(" Signal Level: " + gateway.getSignalLevel()+ " dBm");
System.out.println(" Battery Level: " + gateway.getBatteryLevel()+ "%");
System.out.println();

OutboundMessage msg = new OutboundMessage(num, umsg);
Service.getInstance().sendMessage(msg);

System.out.println(msg);

Service.getInstance().stopService();
gateway.stopGateway();

return true;
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {

try {
Service.getInstance().stopService();
} catch (TimeoutException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (GatewayException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SMSLibException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return false;
}

public class OutboundNotification implements IOutboundMessageNotification {
public void process(AGateway gateway, OutboundMessage msg) {
System.out.println("Outbound handler called from Gateway: "
+ gateway.getGatewayId());
System.out.println(msg);
}
}

}

这段代码在我第一次调用 doIt 时运行良好。但如果我的线程收到更多短信从服务器然后如果我调用 doIt ,它会抛出异常

org.smslib.GatewayException: Comm library exception: java.lang.RuntimeException: javax.comm.PortInUseException: Port currently owned by 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:275)

问题出在哪里?

最佳答案

看起来您没有正确关闭 SmsLib,因为异常似乎暗示与串行端口的连接保持打开状态。

关于java - 在同一进程中使用 SMSLib 发送多条短信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7037399/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com