需求:在一個扣款操做後,若是預付款帳戶餘額不足時,新啓一個線程去調用短信平臺的接口,超過30秒無響應時,自動關閉,並記錄在數據庫。html
思路:對於這種與其餘平臺進行交互的需求,最好的設計思路就是使用MQ,只要成功放到消息隊列之後,另外一個平臺就能夠在隊列裏讀取到,不用擔憂超時的問題。這次的需求是調用他們平臺的接口,可能會出現三種狀況:成功,失敗,超時。成功,失敗這些狀況就不一一描述了。重點是超時,超時的因素有不少,很難去分析到,因此就選擇在本平臺設置超時時間,來對調用狀況進行判斷和記錄,通常的超時時間設置是30秒。數據庫
實現:使用線程或者線程池。這裏使用線程進行超時關閉,先設定一個A線程去調用接口,同時建立一個守護線程B來進行計時,當正常運行時,A線程收到迴應,關閉線程。當出現超時狀況,30秒事後,B線程跑出異常,A線程跳出循環。ide
代碼:this
線程Aspa
1 final Thread t = new Thread(){ 2 //自定義守護線程 3 TimeoutThread tTime = new TimeoutThread(30000,new TimeoutException("短信發送超時"),smsRequestService,feePrepay,contentType); 4 public void run(){ 5 while (true){ 6 try { 7 tTime.start(); 8 new XIYSMSService().smsSendMssage(feePrepay,contentType,smsRequestService); 9 tTime.cancel(); 10 break; 11 } catch (Exception e) { 12 System.err.println(e); 13 break; 14 } 15 } 16 } 17 };
守護線程B線程
1 /** 2 * 本線程設置了一個超時時間 3 * 該線程開始運行後,通過指定超時時間, 4 * 該線程會拋出一個未檢查異常通知調用該線程的程序超時 5 * 在超時結束前能夠調用該類的cancel方法取消計時 6 * @author liheng 7 */ 8 public class TimeoutThread extends Thread{ 9 /** 10 * 計時器超時時間 11 */ 12 private long timeout; 13 /** 14 * 計時是否被取消 15 */ 16 private boolean isCanceled = false; 17 /** 18 * 當計時器超時時拋出的異常 19 */ 20 21 private TimeoutException timeoutException; 22 23 private SmsRequestService smsRequestService; 24 25 private FeePrepay feePrepay; 26 27 private String contentType; 28 /** 29 * 構造器 30 * @param timeout 指定超時的時間 31 */ 32 public TimeoutThread(long timeout,TimeoutException timeoutErr,SmsRequestService smsRequestService,FeePrepay feePrepay,String contentType) { 33 super(); 34 this.timeout = timeout; 35 this.timeoutException = timeoutErr; 36 this.smsRequestService = smsRequestService; 37 this.feePrepay = feePrepay; 38 this.contentType = contentType; 39 //設置本線程爲守護線程 40 this.setDaemon(true); 41 } 42 /** 43 * 取消計時 44 */ 45 public synchronized void cancel(){ 46 isCanceled = true; 47 } 48 /** 49 * 啓動超時計時器 50 */ 51 public void run(){ 52 try { 53 Thread.sleep(timeout); 54 if(!isCanceled){ 55 smsRequestService.saveFeePrePay(feePrepay, "超時", contentType); 56 System.err.println(timeoutException); 57 } 58 } catch (InterruptedException e) { 59 System.err.println(e); 60 } 61 } 62 }
歡迎你們留言交流,指出不足之處,萬分感謝!設計
原文出處:https://www.cnblogs.com/yiqi520/p/10286816.htmlcode