JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區 » 例外/錯誤處理  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
作者 Timer and TimerTask Exception Handling
hachiroku





發文: 33
積分: 0
於 2012-11-30 11:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
請問一下諸位先進

今天 Timer 會固定 2分鐘去執行 TimerTask

但 TimerTask 執行期間可能發生 SSLHandshakeException...

這時候希望是可以重新開啟該物件來重新建立 SSL 連線

我試圖將 Exception 往上層丟~但最多只能丟到 TimerTask

不能丟到 Timer 來做重新 Schedule...

請問大家都怎麼處理這類的情形?

謝謝~


reply to postreply to post
作者 Re:Timer and TimerTask Exception Handling [Re:hachiroku]
parkghost





發文: 253
積分: 0
於 2012-11-30 12:42 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
你可以把 exception handing 寫在 TimerTask 裡,或是改用 ScheduledExecutorService 將 exception 透過 Future 結構 rethrow 出來

reply to postreply to post
~流浪啊
作者 Re:Timer and TimerTask Exception Handling [Re:parkghost]
hachiroku





發文: 33
積分: 0
於 2012-11-30 13:56 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝回覆

ScheduledExecutorService 剛查了一下是讓數個執行緒來處理, 在我的案子並不用

附上簡單程式碼說明
Scheduler.java
1
2
3
4
5
6
7
8
public class Scheduler {
    public static void main(String[] args) {
        long period = 1000 * 60 * 2 ;
        TimerTaskA timertask = new TimerTaskA();
        Timer timer = new Timer();
        timer.schedule(timertask, 0, period);
    }
}


TimerTaskA.java
1
2
3
4
5
6
7
8
9
10
public class TimerTaskA extends TimerTask {
    @Override
    public void run() {
        try {
            //thorw Exeption codes;
        } catch (SSLHandshakeException shex) {
            //To do
        }
    }
}


我是在 TimerTask 中 catch exception 沒有錯, 但是我怎麼停止該 network socket 或者 重新啟動此 TimerTask ?

似乎只能 cancel()...但是那似乎是中止 TimerTask

Thanks


reply to postreply to post
作者 Re:Timer and TimerTask Exception Handling [Re:hachiroku]
parkghost





發文: 253
積分: 0
於 2012-12-01 01:22 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
用 TimerTask 的方式把錯誤處理寫在 Thread 內部
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
public class RecoverableTimerTask extends TimerTask {
    int numOfRetry;
    ConnectInfo connectInfo;
    Socket socket;
 
    RecoverableTimerTask(int numOfRetry, ConnectInfo connectInfo){
         this.numOfRetry = numOfRetry;
         this.connectInfo = connectInfo;
    }
 
    private void initialSocket(){
        ...
        socket = new ...
    }
 
    @Override
    public void run() {
        for(int i=0; i < numOfRetry; i++){
            try {
                initialSocket();
                //business logic
                //thorw exeption
                return; 
            } catch (SSLHandshakeException e) {
                // clean error state
                continue;
            } finally{
                socket.close();
            }
        }
    }
}


Executor Framework 中的 Future 結構可以用在 Thread 間回傳處理結果,實作上會複雜些
若是你的錯誤處理需要寫在 Thread 外部,這是一種方式

e.g.
1
2
3
4
5
6
Future result = ScheduledExecutorService.schedule(...)
try{
    result.get()
} catch (SSLHandshakeException e) {
    ...
}


http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/ScheduledExecutorService.html
http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/Future.html


reply to postreply to post
~流浪啊
作者 Re:Timer and TimerTask Exception Handling [Re:parkghost]
hachiroku





發文: 33
積分: 0
於 2012-12-01 15:10 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
感謝先進~

very nice solution!!!

CoolCool

非常感恩!


reply to postreply to post
» JWorld@TW »  Java 新手區 » 例外/錯誤處理

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8