ITPub博客

Nagios调用WSDL WebServer接口发送短信

原创 服务器/存储 作者:sundog315 时间:2014-05-29 16:49:17 1 删除 编辑

使用JAVA调用短信网关WebService


import java.text.SimpleDateFormat;
import org.apache.commons.lang.StringEscapeUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
import java.io.PrintWriter;

public class AddSMSList {
       public static void main(String[] args) {
              try {
                     String endpoint = "http://10.1.0.128:8080/SMSG/services/SMS";
                     Service service = new Service();
                     Call call = (Call) service.createCall();
                     call.setTargetEndpointAddress(new java.net.URL(endpoint));
                     call.setOperationName(new QName(endpoint, "AddSMSList"));            
       call.addParameter("validation", org.apache.axis.encoding.XMLType.XSD_STRING,
                                   javax.xml.rpc.ParameterMode.IN);
                     call.addParameter("XML", org.apache.axis.encoding.XMLType.XSD_STRING,
                                   javax.xml.rpc.ParameterMode.IN);
       call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
                     String valid = "POWERU-SMS";

       SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
       String dt = sdf.format(new Date());
       dt = dt.substring(0,16);
       dt = dt + "0";
       //System.out.println(dt);
   
       String ar = args[1];
       String content = StringEscapeUtils.escapeHtml(ar);
       //System.out.println(content);

       String xmlbody = "";  //短信接口格式
       //System.out.println(xmlbody);

       String result = (String)call.invoke(new Object[]{valid, xmlbody});
       System.out.println("result is "+result);

       File tofile=new File("/usr/local/nagios/var/sms.log"); 
       FileWriter fw=new FileWriter(tofile,true); 
       BufferedWriter bw=new BufferedWriter(fw); 
       PrintWriter pw=new PrintWriter(bw); 

       pw.println(dt + ":" + xmlbody + ":" + result);  //记录日志
       pw.close(); 
       bw.close(); 
       fw.close();
              }
              catch (Exception e) {
                     System.err.println(e.toString());
              }
       }
}


编译

javac -classpath .:./javaee.jar:./axis-1.4.jar:./commons-logging-1.1.1.jar:./commons-discovery-20040218.194635.jar:./wsdl4j-1.6.2.jar:./javax.activation.jar:./javax.mail-1.3.3.01.jar:./commons-lang-2.4.jar AddSMSList.java


发送脚本

sendSMS.sh

export JAVA_HOME=/usr/local/java
export PATH=$PATH:$JAVA_HOME/bin

LOCAL_PATH=$(cd "$(dirname "$0")"; pwd)

java -classpath $LOCAL_PATH:$LOCAL_PATH/javaee.jar:$LOCAL_PATH/axis-1.4.jar:$LOCAL_PATH/commons-logging-1.1.1.jar:$LOCAL_PATH/commons-discovery-20040218.194635.jar:$LOCAL_PATH/wsdl4j-1.6.2.jar:$LOCAL_PATH/javax.activation.jar:$LOCAL_PATH/javax.mail-1.3.3.01.jar:$LOCAL_PATH/commons-lang-2.4.jar AddSMSList "$1" "$2"

请登录后发表评论 登录
全部评论

注册时间:2010-01-05

  • 博文量
    126
  • 访问量
    494748