使用wavecom短信猫批量发送短信的Java代码(单例模式)

<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); } }}


 
 

调用例子

//批量发送短信
	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
<span style="white-space:pre">					</span>logger.debug("发送短信失败:"+historyMessagevo.getContent());
}}} catch (Exception e) {logger.error("短信发送失败:"+e);e.printStackTrace();}}}

 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值