?? smreceiver.java
字號:
package demo.smgpdemo;
import java.util.*;
import java.sql.*;
import java.io.*;
import com.huawei.smproxy.SMGPSMProxy;
import com.huawei.smproxy.comm.smgp.*;
import com.huawei.smproxy.comm.smgp.message.*;
import com.huawei.smproxy.util.*;
public class SMReceiver extends SMGPSMProxy
implements Runnable{
//系統配置信息
private static Args arg = Env.getConfig().getArgs("SMGPConnect");
private static Thread instance;
public static Thread getInstance() {
if (instance == null) {
instance = new Thread(new SMReceiver());
}
return instance;
}
protected SMReceiver() {
super(SMReceiver.arg);
}
/**
* 連接終止的處理,由API使用者實現
* 短信中心連接終止后,需要執行動作的接口
*/
public void OnTerminate() {
System.out.println("Connection have been breaked! ");
}
/**
* 對SMGW主動下發的消息的處理接口。此例中只返回一個成功的響應。
* @param msg 收到的消息。
* @return 返回的相應消息。
*/
public SMGPMessage onDeliver(final SMGPDeliverMessage msg) {
if(msg.getIsReport() != 1){
System.out.println("\n**************************Received a new message!***************************");
System.out.println(msg.toString());
System.out.println("The Sender is: " + msg.getSrcTermID());
System.out.println("***************************End new message! **************************\n");
/*
String[] rcvMobile = new String[1];
rcvMobile[0] = msg.getSrcTermID();
//生成一個CNGP回復消息
System.out.print("Create new reply...\n");
CNGPSubmitMessage reply = new CNGPSubmitMessage(
"3001999995", //spid
1, //subtype
1, //needReport,
3, //priority
"+xkx", //serviceId
"01", //FeeType
0, //FeeUserType
"000000", //FeeCode
15, //msgFormat
null,
null, //定時發送時間(null:立即發送)
"94005", //srcTermId
"94005", //ChargeTermId
1, //destTermIdCount
rcvMobile, //destTermId
16, //msgLength
"你好,謝謝使用!", //msgmsgContent
0 //protocolValue
);
if (send(reply)) {
System.out.println("\nThe reply send OK!\n");
}
else {
System.out.println("\nThe reply send Fail!\n");
}
*/
}else{
System.out.println("\n++++++++++++++++++++++++++Received a new report!+++++++++++++++++++++++++");
System.out.println(msg.toString());
System.out.println("++++++++++++++++++++++++++End a new report!+++++++++++++++++++++++++\n");
}
return new SMGPDeliverRespMessage(msg.getMsgId(), 0);
}
/**
* 封裝父類的send()方法
* 發送一條消息,完成真正的消息發送。
* @param msg 待發送的消息。
* @return true:發送成功。false:發送失敗。
*/
/*
public boolean send(CNGPSubmitMessage msg) {
if (msg == null) {
return false;
}
CNGPSubmitRespMessage reportMsg = null;
PreparedStatement stat = null;
try {
reportMsg = (CNGPSubmitRespMessage)super.send(msg);
//System.out.println("發送狀態: " + reportMsg.toString());
}
catch (IOException ex) {
ex.printStackTrace();
return false;
}catch (java.lang.NullPointerException e) {
return false;
}
return true;
}*/
public boolean send(SMGPSubmitMessage msg) {
if (msg == null) {
return false;
}
try{
super.send(msg);
}catch(IOException e){
}
return true;
}
public void run(){
while(true){
try {
Thread.sleep(500);
}
catch (Exception ex) {}
}
}
//測試用主函數
public static void main(String[] args) {
//生成100個接收短信的手機號碼
//發送消息(發送多次)
Thread receiver = SMReceiver.getInstance();
receiver.start();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -