--- dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java 2015/03/20 13:57:47 2457 +++ dao/DaoAdresseService/src/main/java/dk/daoas/daoadresseservice/OSMStreetnameHelper.java 2015/07/16 06:31:51 2620 @@ -4,6 +4,9 @@ import com.google.gson.Gson; +import com.google.common.util.concurrent.RateLimiter; + + import dk.daoas.daoadresseservice.admin.ServiceConfig; import dk.daoas.daoadresseservice.beans.OSMAddress; import dk.daoas.daoadresseservice.beans.SearchRequest; @@ -16,9 +19,12 @@ public class OSMStreetnameHelper implements StreetnameHelper { ServiceConfig conf; + RateLimiter limiter; public OSMStreetnameHelper(ServiceConfig conf) { this.conf = conf; + + limiter = RateLimiter.create( 10.0 ); //Max 10 requests pr sekund } @Override @@ -26,11 +32,12 @@ if (conf.useOpenStreetMaps == false) return null; + result.osm = true; try { - OSMInvocation wrapper = new OSMInvocation( conf, request.postnr, request.vejnavn ); + OSMInvocation wrapper = new OSMInvocation( limiter, conf, request.postnr, request.vejnavn ); CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("osm"); result.osmVej = (String) breaker.invoke(wrapper); @@ -46,8 +53,10 @@ int postnr; String vejnavn; ServiceConfig conf; + RateLimiter limiter; - public OSMInvocation(ServiceConfig conf, int postnr, String vejnavn) { + public OSMInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) { + this.limiter = limiter; this.conf = conf; this.postnr= postnr; this.vejnavn = vejnavn; @@ -55,6 +64,9 @@ @Override public String proceed() throws Exception { + limiter.acquire(); + + //TimingHelper timer = new TimingHelper(); String encVej = URLEncoder.encode(vejnavn, "UTF-8"); @@ -76,8 +88,8 @@ if (adrList.length != 1) return null; - if (adrList[0].address != null) { - return adrList[0].address.road; + if (adrList[0].getAddress() != null) { + return adrList[0].getAddress().getRoad(); } return null;