--- dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/18 07:45:28 2324 +++ dao/DaoAdresseService/src/dk/daoas/daoadresseservice/GeocodeHelper.java 2015/02/24 13:00:46 2368 @@ -13,47 +13,98 @@ import com.google.code.geocoder.model.GeocoderStatus; import com.google.gson.Gson; +import dk.daoas.daoadresseservice.admin.ServiceConfig; import dk.daoas.daoadresseservice.beans.OSMAddress; import dk.daoas.daoadresseservice.util.HttpUtil; +import dk.thoerup.circuitbreaker.CircuitBreaker; +import dk.thoerup.circuitbreaker.CircuitBreakerManager; +import dk.thoerup.circuitbreaker.CircuitInvocation; public class GeocodeHelper { - - - + public static void main(String[] args) throws IOException { + ServiceConfig conf = new ServiceConfig(); + conf.osmTimeout = 1000; + int post = 8700; String vej = "Enebarvej"; long start1 = System.currentTimeMillis(); - System.out.println( "Google:" + GeocodeHelper.googleHelper(post, vej) ); + System.out.println( "Google:" + GeocodeHelper.googleHelper(conf, post, vej) ); long stop1 = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); - System.out.println( "OSM:" + GeocodeHelper.openstreetmapHelper(post, vej) ); + System.out.println( "OSM:" + GeocodeHelper.openstreetmapHelper(conf, post, vej) ); long stop2 = System.currentTimeMillis(); System.out.println("Google: " + (stop1-start1)); System.out.println("OSM: " + (stop2-start2)); } - public static String openstreetmapHelper(int postnr, String vejnavn) { + public static String openstreetmapHelper(ServiceConfig conf, int postnr, String vejnavn) { try { - + + OSMInvocation wrapper = new OSMInvocation( conf, postnr, vejnavn ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); + + return (String) breaker.invoke(wrapper); + + + } catch (Exception e) { + System.out.println( "OSMError: " + e.getClass().getName() +" / "+ e.getMessage() ); + } + return null; + } + + + + public static String googleHelper(ServiceConfig conf, int postnr, String vejnavn) { + try { + GoogleInvocation wrapper = new GoogleInvocation( conf, postnr, vejnavn ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google"); + + return (String) breaker.invoke(wrapper); + + } catch (Exception e) { + System.out.println( "GoogleError: " + e.getClass().getName() +" / "+ e.getMessage() ); + } + return null; + } + + + public static class OSMInvocation implements CircuitInvocation { + int postnr; + String vejnavn; + ServiceConfig conf; + + public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) { + this.conf = conf; + this.postnr= postnr; + this.vejnavn = vejnavn; + } + + @Override + public String proceed() throws Exception { + //TimingHelper timer = new TimingHelper(); String encVej = URLEncoder.encode(vejnavn, "UTF-8"); - String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1"; + + String url = conf.nominatimBase + "/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1"; + //String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&format=json&addressdetails=1"; //System.out.println (url); - String json = HttpUtil.getContentString(url, 1000); + String json = HttpUtil.getContentString(url, conf.osmTimeout); //System.out.println(json); Gson gson = new Gson(); OSMAddress adrList[] = gson.fromJson(json, OSMAddress[].class); + //timer.printElapsed("OSM elapsed"); + //System.out.println("Count: " + adrList.length); if (adrList.length != 1) return null; @@ -62,29 +113,51 @@ return adrList[0].address.road; } - - } catch (Exception e) { - System.out.println( "Error: " + e.getMessage() ); - } - return null; + return null; + } } - - public static String googleHelper(int postnr, String vejnavn) { - try { - //Todo: Load api key from context config - final Geocoder geocoder = new Geocoder(); + public static class GoogleInvocation implements CircuitInvocation { + ServiceConfig conf; + int postnr; + String vejnavn; + + public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) { + this.conf = conf; + this.postnr= postnr; + this.vejnavn = vejnavn; + + } + + @Override + public String proceed() throws Exception { + + final Geocoder geocoder; + if ( conf.googleApiKey != null ) { + geocoder = new Geocoder(conf.googleApiUser, conf.googleApiKey); //Throws InvalidKeyException + } else { + geocoder = new Geocoder(); + } + //TimingHelper timer = new TimingHelper(); String search = vejnavn + ", " + postnr + ", Denmark"; GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest); - + + //timer.printElapsed("Google elapsed"); //System.out.println( "Status: >" + geocoderResponse.getStatus() + "<"); - if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { - System.out.println("Google responded with " + geocoderResponse.getStatus() ); - return null; + if ( geocoderResponse.getStatus() != GeocoderStatus.OK) { + + if (geocoderResponse.getStatus() == GeocoderStatus.ZERO_RESULTS) { + return null; + } else { + System.out.println("Google responded with " + geocoderResponse.getStatus() ); + //Hvis det er alvorlige fejl skal vi afbryde med exception og trigger circuitbreakeren + throw new Exception("Google responded with " + geocoderResponse.getStatus() ); + } + } List resList = geocoderResponse.getResults(); @@ -102,11 +175,8 @@ return c.getLongName(); } } - - } catch (IOException e) { - System.out.println( "GoogleError: " + e.getMessage() ); + return null; } - return null; }