📄 receivestatusreport.java
字号:
package sms;
import java.io.File;
import java.io.IOException;
import java.util.Hashtable;
import com.objectxp.msg.GsmSmsService;
import com.objectxp.msg.Message;
import com.objectxp.msg.MessageEvent;
import com.objectxp.msg.MessageEventListener;
import com.objectxp.msg.MessageException;
import com.objectxp.msg.SmsMessage;
import com.objectxp.msg.SmsService;
import com.objectxp.msg.SmsStatus;
import com.objectxp.msg.StatusReportMessage;
/**
* This example demonstrates how to handle Status Reports.
* */
public class ReceiveStatusReport implements MessageEventListener
{
/* Incoming status reports */
private Hashtable reports = new Hashtable();
public static void main(String args[]) throws IOException, MessageException, InterruptedException
{
if(args.length != 3 ) {
System.err.println("Usage: ReceiveStatusReport <config-file> <recipient> <text>");
System.exit(1);
}
SmsService service = null;
try {
// initialize SmsService (replace GsmSmsService with your licensed Service)
service = new GsmSmsService();
service.init(new File(args[0]));
// register a MessageEvent listener
ReceiveStatusReport rsr = new ReceiveStatusReport();
service.addMessageEventListener(rsr);
// prepare a sms message
SmsMessage msg = new SmsMessage();
msg.setRecipient(args[1]);
msg.setMessage(args[2]);
msg.requestStatusReport(true);
// Connect to the SMSC/GSM device
service.connect();
// receiving must be started for status report reception
service.startReceiving();
// Send the message
service.sendMessage(msg);
// Wait for the status report
StatusReportMessage status = rsr.waitForStatusReport(msg, 60000);
if( status == null) {
System.out.println("WARNING: No status report received within one minute...");
}
else {
SmsStatus smsstat = status.getStatus();
System.out.println("Received StatusReportMessage: "+smsstat);
}
} finally {
service.destroy();
}
}
/**
* Wait for the arrival of our StatusReportMessage.
*
* @param msg the originating message
* @param timeout the time to wait for an incoming status report for the message
* @return a StatusReportMessage or null
*/
private StatusReportMessage waitForStatusReport(Message msg, long timeout) throws InterruptedException
{
if(msg == null ) return null;
Object id = msg.getID();
if( id == null ) {
throw new IllegalArgumentException("Message has no ID");
}
synchronized(reports) {
Object obj = reports.get(id);
if( obj != null && obj instanceof StatusReportMessage ) {
reports.remove(id);
return (StatusReportMessage) obj;
}
// Wait for the status report
Object sync = new Object();
reports.put(id, sync);
synchronized(sync) {
sync.wait(timeout);
}
obj = reports.get(id);
if( obj != null && obj instanceof StatusReportMessage ) {
reports.remove(id);
return (StatusReportMessage) obj;
}
}
return null;
}
/**
* Handle MessageEvents from the SmsService.
*/
public void handleMessageEvent(MessageEvent event)
{
if(event.getType() == MessageEvent.STATUS_RECEIVED) {
StatusReportMessage msg = (StatusReportMessage)event.getMessage();
System.out.println("Received StatusReportMessage with id: "+msg.getID());
Object id = msg.getID();
if( id == null ) {
System.err.println("Incoming Status Report has no ID");
return;
}
synchronized(reports) {
Object obj = reports.get(id);
if( obj == null ) {
reports.put(obj, msg);
} else {
synchronized(obj) {
obj.notify();
}
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -