--- 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/23 15:49:00 2352 @@ -13,21 +13,25 @@ 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(); + 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(); @@ -41,7 +45,46 @@ public static String openstreetmapHelper(int postnr, String vejnavn) { try { - + + OSMInvocation wrapper = new OSMInvocation( postnr, vejnavn ); + CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); + + return (String) breaker.invoke(wrapper); + + + } catch (Exception e) { + System.out.println( "Error: " + 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.getMessage() ); + } + return null; + } + + + public static class OSMInvocation implements CircuitInvocation { + int postnr; + String vejnavn; + + public OSMInvocation(int postnr, String vejnavn) { + this.postnr= postnr; + this.vejnavn = vejnavn; + } + + @Override + public String proceed() throws Exception { String encVej = URLEncoder.encode(vejnavn, "UTF-8"); String url = "http://nominatim.openstreetmap.org/search?country=DK&street=" + encVej + "&postalcode=" + postnr + "&format=json&addressdetails=1"; @@ -62,29 +105,50 @@ 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(); + } + String search = vejnavn + ", " + postnr + ", Denmark"; GeocoderRequest geocoderRequest = new GeocoderRequestBuilder().setAddress(search).setLanguage("en").getGeocoderRequest(); GeocodeResponse geocoderResponse = geocoder.geocode(geocoderRequest); - + //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.ZERO_RESULTS) { + return null; + } else { + //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 +166,8 @@ return c.getLongName(); } } - - } catch (IOException e) { - System.out.println( "GoogleError: " + e.getMessage() ); + return null; } - return null; }