2010-08-09 51 views
0

我有一種情況,我必須檢查數據庫中的值,如aValue。 如果aValue可用,則執行aValueProcess()。 如果該值不可用,我只能等待30分鐘,並且需要每10分鐘檢查一次數據庫的值(3次)。 如果超過30分鐘退出程序。定期連接數據庫

任何人都可以給我最好的方法來做到這一點。任何幫助表示讚賞。

回答

1

這裏是我散列這至少應該告訴你的邏輯(注意我做大多數是C#,所以你可能要轉變職能。

val aValue = aValueProcess(); 
    int attempts = 0; 

    //Wait 10 minutes and try again if value is null and we have not tried 
    //3 times (30 minutes of trying) 
    while(aValue == null && attempts < 3) 
    { 
     thread.sleep(600000); //10 minutes in milliseconds 
     attempts += 1; 
     aValue = aValueProcess(); 
    }