diff options
author | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 11:17:18 +1300 |
---|---|---|
committer | Anton Tananaev <anton.tananaev@gmail.com> | 2015-10-06 11:17:18 +1300 |
commit | c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d (patch) | |
tree | a86dd0c94803608b6f72f6a23ecb084d3ec68728 /src/org/traccar/geocode/JsonReverseGeocoder.java | |
parent | 923a49d6bc33f94bf39b8be13c77b1dad4d1dda0 (diff) | |
download | traccar-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.tar.gz traccar-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.tar.bz2 traccar-server-c8ad5cc5c45ab47abe35e3ad2e4f607d520e627d.zip |
Implement async reverse geocoding
Diffstat (limited to 'src/org/traccar/geocode/JsonReverseGeocoder.java')
-rw-r--r-- | src/org/traccar/geocode/JsonReverseGeocoder.java | 42 |
1 files changed, 22 insertions, 20 deletions
diff --git a/src/org/traccar/geocode/JsonReverseGeocoder.java b/src/org/traccar/geocode/JsonReverseGeocoder.java index fd86f06d5..25b8e7853 100644 --- a/src/org/traccar/geocode/JsonReverseGeocoder.java +++ b/src/org/traccar/geocode/JsonReverseGeocoder.java @@ -15,6 +15,9 @@ */ package org.traccar.geocode; +import com.ning.http.client.AsyncCompletionHandler; +import com.ning.http.client.Response; +import org.traccar.Context; import org.traccar.helper.Log; import javax.json.Json; @@ -24,6 +27,7 @@ import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.AbstractMap; +import java.util.Collections; import java.util.LinkedHashMap; import java.util.Map; @@ -36,50 +40,48 @@ public abstract class JsonReverseGeocoder implements ReverseGeocoder { public JsonReverseGeocoder(String url, final int cacheSize) { this.url = url; if (cacheSize > 0) { - this.cache = new LinkedHashMap<Map.Entry<Double, Double>, String>() { + this.cache = Collections.synchronizedMap(new LinkedHashMap<Map.Entry<Double, Double>, String>() { protected boolean removeEldestEntry(Map.Entry eldest) { return size() > cacheSize; } - }; + }); } } @Override - public String getAddress(AddressFormat format, double latitude, double longitude) { + public void getAddress(final AddressFormat format, final double latitude, final double longitude, final ReverseGeocoderCallback callback) { if (cache != null) { String cachedAddress = cache.get(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude)); if (cachedAddress != null) { - return cachedAddress; + callback.onResult(cachedAddress); + return; } } - try { - HttpURLConnection conn = (HttpURLConnection) new URL(String.format(url, latitude, longitude)).openConnection(); - conn.setRequestProperty("Connection", "close"); // don't keep-alive connections - try { - InputStreamReader streamReader = new InputStreamReader(conn.getInputStream()); - try (JsonReader reader = Json.createReader(streamReader)) { + Context.getAsyncHttpClient().prepareGet(String.format(url, latitude, longitude)).execute(new AsyncCompletionHandler() { + @Override + public Object onCompleted(Response response) throws Exception { + try (JsonReader reader = Json.createReader(response.getResponseBodyAsStream())) { Address address = parseAddress(reader.readObject()); - while (streamReader.read() > 0); // make sure we reached the end if (address != null) { String formattedAddress = format.format(address); - if (cache != null) { cache.put(new AbstractMap.SimpleImmutableEntry<>(latitude, longitude), formattedAddress); } - - return formattedAddress; + callback.onResult(formattedAddress); + } else { + callback.onResult(null); } } - } finally { - conn.disconnect(); + return null; } - } catch(Exception error) { - Log.warning(error); - } - return null; + @Override + public void onThrowable(Throwable t) { + callback.onResult(null); + } + }); } protected abstract Address parseAddress(JsonObject json); |