10 |
import com.google.code.geocoder.model.GeocoderResult; |
import com.google.code.geocoder.model.GeocoderResult; |
11 |
import com.google.code.geocoder.model.GeocoderStatus; |
import com.google.code.geocoder.model.GeocoderStatus; |
12 |
|
|
13 |
|
import com.google.common.util.concurrent.RateLimiter; |
14 |
|
|
15 |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
import dk.daoas.daoadresseservice.admin.ServiceConfig; |
16 |
import dk.daoas.daoadresseservice.beans.SearchRequest; |
import dk.daoas.daoadresseservice.beans.SearchRequest; |
17 |
import dk.daoas.daoadresseservice.beans.SearchResult; |
import dk.daoas.daoadresseservice.beans.SearchResult; |
22 |
public class GoogleStreetnameHelper implements StreetnameHelper { |
public class GoogleStreetnameHelper implements StreetnameHelper { |
23 |
|
|
24 |
ServiceConfig conf; |
ServiceConfig conf; |
25 |
|
RateLimiter limiter; |
26 |
|
|
27 |
public GoogleStreetnameHelper(ServiceConfig conf) { |
public GoogleStreetnameHelper(ServiceConfig conf) { |
28 |
this.conf = conf; |
this.conf = conf; |
29 |
|
|
30 |
|
limiter = RateLimiter.create( 10.0 ); //max 10 req /sec |
31 |
} |
} |
32 |
|
|
33 |
@Override |
@Override |
35 |
if (conf.useGoogle == false) |
if (conf.useGoogle == false) |
36 |
return null; |
return null; |
37 |
|
|
38 |
|
|
39 |
result.google = true; |
result.google = true; |
40 |
|
|
41 |
try { |
try { |
42 |
GoogleInvocation wrapper = new GoogleInvocation( conf, request.postnr, request.vejnavn ); |
GoogleInvocation wrapper = new GoogleInvocation( limiter, conf, request.postnr, request.vejnavn ); |
43 |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google"); |
CircuitBreaker breaker = CircuitBreakerManager.getManager().getCircuitBreaker("google"); |
44 |
|
|
45 |
|
|
57 |
ServiceConfig conf; |
ServiceConfig conf; |
58 |
int postnr; |
int postnr; |
59 |
String vejnavn; |
String vejnavn; |
60 |
|
RateLimiter limiter; |
61 |
|
|
62 |
public GoogleInvocation(ServiceConfig conf, int postnr, String vejnavn) { |
public GoogleInvocation(RateLimiter limiter, ServiceConfig conf, int postnr, String vejnavn) { |
63 |
|
this.limiter = limiter; |
64 |
this.conf = conf; |
this.conf = conf; |
65 |
this.postnr= postnr; |
this.postnr= postnr; |
66 |
this.vejnavn = vejnavn; |
this.vejnavn = vejnavn; |
69 |
|
|
70 |
@Override |
@Override |
71 |
public String proceed() throws Exception { |
public String proceed() throws Exception { |
72 |
|
|
73 |
|
limiter.acquire(); |
74 |
|
|
75 |
final Geocoder geocoder; |
final Geocoder geocoder; |
76 |
if ( conf.googleApiKey != null ) { |
if ( conf.googleApiKey != null ) { |