您的位置:首页 > 编程语言 > Java开发

使用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();}}}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: