使用wavecom短信猫批量发送短信的Java代码(单例模式)
2018-01-26 10:19
567 查看
<pre name="code" class="java">package com.jeysan.lz.message.service;
import java.util.ResourceBundle;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;
import com.jeysan.modules.utils.ResourceBundleUtils;
// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.
public class SendMessageHelper
{
private Service srv; //声明一个服务
private SerialModemGateway gateway;
private static SendMessageHelper instance = null;
private static Object lock = new Object();
public static SendMessageHelper getInstance() {
if(instance==null){
synchronized (lock) {
if(instance==null)
instance = new SendMessageHelper();
}
}
return instance;
}
private SendMessageHelper(){
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init()throws Exception
{
System.out.println(Library.getLibraryDescription());//获取类库描述
System.out.println("Version: " + Library.getLibraryVersion());//获取类库版本
srv = new Service(); //初始化服务
OutboundNotification outboundNotification = new OutboundNotification();
//115200是波特率,一般为9600。可以通过超级终端测试出来,声明网关 第一个参数为:网关ID,第二个是本机上短信猫的com口名称,第三是波特率,第四是短信猫生产厂商,第五设备的型号(可选)
String com_index = ResourceBundleUtils.getSValue("com.index");
<span style="white-space:pre"> </span>//从配置文件中读取端口号
gateway = new SerialModemGateway("modem.com"+com_index, "COM"+com_index, 9600,"wavecom", "17254"); gateway.setInbound(true);//设置true,表示该网关可以接收短信,根据需求修改 gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改 gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234 gateway.setOutboundNotification(outboundNotification);//发送短信成功后的回调函方法 srv.addGateway(gateway); //将网关添加到短信猫服务中 srv.startService(); //启动服务,进入短信发送就绪状态 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() + "%"); System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%"); System.out.println();}public void close()throws Exception{gateway.stopGateway(); srv.stopService(); } public Boolean sendSingleMessage(String PhoneNum,String Mesg) throws Exception { OutboundMessage msg = new OutboundMessage(PhoneNum, Mesg); msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的 Boolean result= srv.sendMessage(msg); return result; } //OutboundMessage继承IOutboundMessageNotification接口,重写了process方法,用于网关设置回调函数 public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("Outbound handler called from Gateway: " + gatewayId); System.out.println(msg); } }}
调用例子
[java] view
plain copy
//批量发送短信
public void sendMessages(List<MomentumMessageVO> messageList)
{
if(!messageList.isEmpty()){
try {
//初始化短信猫
SendMessageHelper sendMessageHelper = SendMessageHelper.getInstance();
//开始发送短信
for (MomentumMessageVO momentumMessagevo : messageList) {
// a变量用来判断短信是否发送成功
boolean a=false;
try {
if(momentumMessagevo.getReceiveNumber()!=null)
a = sendMessageHelper.sendSingleMessage(momentumMessagevo.getReceiveNumber().toString(), momentumMessagevo.getContent());
} catch (Exception e) {
// 发送失败
a=false;
e.printStackTrace();
}
if (a){
logger.debug("发送短信success:"+historyMessagevo.getContent());
//todo something
}else{
<pre name="code" class="java"><span style="white-space:pre"> </span>//todo something
[java] view
plain copy
<span style="white-space:pre"> </span>logger.debug("发送短信失败:"+historyMessagevo.getContent());
}}} catch (Exception e) {logger.error("短信发送失败:"+e);e.printStackTrace();}}}
import java.util.ResourceBundle;
import org.smslib.IOutboundMessageNotification;
import org.smslib.Library;
import org.smslib.OutboundMessage;
import org.smslib.Service;
import org.smslib.Message.MessageEncodings;
import org.smslib.modem.SerialModemGateway;
import com.jeysan.modules.utils.ResourceBundleUtils;
// SendMessage.java - Sample application.
//
// This application shows you the basic procedure for sending messages.
// You will find how to send synchronous and asynchronous messages.
//
// For asynchronous dispatch, the example application sets a callback
// notification, to see what's happened with messages.
public class SendMessageHelper
{
private Service srv; //声明一个服务
private SerialModemGateway gateway;
private static SendMessageHelper instance = null;
private static Object lock = new Object();
public static SendMessageHelper getInstance() {
if(instance==null){
synchronized (lock) {
if(instance==null)
instance = new SendMessageHelper();
}
}
return instance;
}
private SendMessageHelper(){
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public void init()throws Exception
{
System.out.println(Library.getLibraryDescription());//获取类库描述
System.out.println("Version: " + Library.getLibraryVersion());//获取类库版本
srv = new Service(); //初始化服务
OutboundNotification outboundNotification = new OutboundNotification();
//115200是波特率,一般为9600。可以通过超级终端测试出来,声明网关 第一个参数为:网关ID,第二个是本机上短信猫的com口名称,第三是波特率,第四是短信猫生产厂商,第五设备的型号(可选)
String com_index = ResourceBundleUtils.getSValue("com.index");
<span style="white-space:pre"> </span>//从配置文件中读取端口号
gateway = new SerialModemGateway("modem.com"+com_index, "COM"+com_index, 9600,"wavecom", "17254"); gateway.setInbound(true);//设置true,表示该网关可以接收短信,根据需求修改 gateway.setOutbound(true);//设置true,表示该网关可以发送短信,根据需求修改 gateway.setSimPin("0000");//sim卡锁,一般默认为0000或1234 gateway.setOutboundNotification(outboundNotification);//发送短信成功后的回调函方法 srv.addGateway(gateway); //将网关添加到短信猫服务中 srv.startService(); //启动服务,进入短信发送就绪状态 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() + "%"); System.out.println(" Battery Level: " + gateway.getBatteryLevel() + "%"); System.out.println();}public void close()throws Exception{gateway.stopGateway(); srv.stopService(); } public Boolean sendSingleMessage(String PhoneNum,String Mesg) throws Exception { OutboundMessage msg = new OutboundMessage(PhoneNum, Mesg); msg.setEncoding(MessageEncodings.ENCUCS2);//这句话是发中文短信必须的 Boolean result= srv.sendMessage(msg); return result; } //OutboundMessage继承IOutboundMessageNotification接口,重写了process方法,用于网关设置回调函数 public class OutboundNotification implements IOutboundMessageNotification { public void process(String gatewayId, OutboundMessage msg) { System.out.println("Outbound handler called from Gateway: " + gatewayId); System.out.println(msg); } }}
调用例子
[java] view
plain copy
//批量发送短信
public void sendMessages(List<MomentumMessageVO> messageList)
{
if(!messageList.isEmpty()){
try {
//初始化短信猫
SendMessageHelper sendMessageHelper = SendMessageHelper.getInstance();
//开始发送短信
for (MomentumMessageVO momentumMessagevo : messageList) {
// a变量用来判断短信是否发送成功
boolean a=false;
try {
if(momentumMessagevo.getReceiveNumber()!=null)
a = sendMessageHelper.sendSingleMessage(momentumMessagevo.getReceiveNumber().toString(), momentumMessagevo.getContent());
} catch (Exception e) {
// 发送失败
a=false;
e.printStackTrace();
}
if (a){
logger.debug("发送短信success:"+historyMessagevo.getContent());
//todo something
}else{
<pre name="code" class="java"><span style="white-space:pre"> </span>//todo something
[java] view
plain copy
<span style="white-space:pre"> </span>logger.debug("发送短信失败:"+historyMessagevo.getContent());
}}} catch (Exception e) {logger.error("短信发送失败:"+e);e.printStackTrace();}}}
相关文章推荐
- 使用wavecom短信猫批量发送短信的Java代码(单例模式)
- java使用smslib连接短信猫发送短信代码分享
- 使用阿里大于平台发送短信验证码java代码实现
- 使用java发送短信
- 【代码】Android: SMS TEL MAIL 发送短信、打电话、发送邮件使用集合
- 使用中国网建实现Java向手机发送短信的功能
- java 短信发送 的 代码
- 利用短信猫发送短信的Java代码
- 使用Java程序向手机发送短信
- Java使用阿里大于发送短信通知和短信验证码
- Java使用【网易云信】短信接口,给手机用户发送并校验验证码
- JAVA代码之短信发送功能(luosimao)
- 使用代理模式让应用程序使用动态java代码
- Java使用JavaMail API发送和接收邮件的代码示例
- Java使用手机发送短信程序
- Cocos2d-x3.3RC0通过JNI调用Android的Java层URI代码发送短信
- 使用java代码发送HTTP Post请求
- 使用Java编写发送邮件的代码
- java线程池的使用-发送短信验证码
- 用 Java 通过串口发送手机短信(二)简单使用