--- android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/10/02 13:44:31 386 +++ android/TrainInfoService/src/dk/thoerup/traininfoservice/banedk/TimetableFetcher.java 2009/10/20 10:32:19 451 @@ -5,10 +5,18 @@ import java.net.URL; import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.logging.Logger; -import com.gargoylesoftware.htmlunit.*; -import com.gargoylesoftware.htmlunit.html.*; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.RefreshHandler; +import com.gargoylesoftware.htmlunit.WebClient; +import com.gargoylesoftware.htmlunit.html.DomNodeList; +import com.gargoylesoftware.htmlunit.html.HtmlElement; +import com.gargoylesoftware.htmlunit.html.HtmlPage; + +import dk.thoerup.curcuitbreaker.CircuitBreaker; +import dk.thoerup.curcuitbreaker.CircuitBreakerManager; public class TimetableFetcher { @@ -17,10 +25,27 @@ } } + + Map> cache = new TimeoutMap>(120 * 1000); + Logger logger = Logger.getLogger(TimetableFetcher.class.getName()); + + + List cachedLookupTimetable(String trainID, String type) throws Exception { + String key = trainID+type; + List list = cache.get(key); + + if (list == null) { + list = lookupTimetable(trainID,type); + cache.put(key, list); + } else { + logger.info("Timetable: Cache hit " + trainID); + } + return list; + } - List lookupTimetable(String trainID, String type) throws Exception { + List lookupTimetable(String trainID, String type) throws Exception { List timetableList = new ArrayList(); String url = "http://www.bane.dk/visRute.asp?W=" + type + "&TogNr=" + trainID + "&artikelId=4276"; @@ -33,8 +58,11 @@ webClient.setCssEnabled(false); - final HtmlPage page = webClient.getPage(url); + BanedkInvocation wrapper = new BanedkInvocation(webClient, url); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("banedk"); + HtmlPage page = (HtmlPage) breaker.invoke(wrapper); + boolean currentStation = false; boolean currentStationSaved = false;