Reputation: 123
I try to use GeoLite2-City.mmdb in spring boot to get location data. To that effect, I created a bean like this in the configuration file:
@Bean
public static synchronized DatabaseReader mmdbReader() throws IOException {
if ( reader != null ) {
return reader;
}
ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:static/mmdb/GeoLite2-City.mmdb");
InputStream database = resource.getInputStream();
reader = new DatabaseReader.Builder(database).fileMode(com.maxmind.db.Reader.FileMode.MEMORY)
.withCache(new CHMCache()).build();
return reader;
}
I also created a util function to retrieve the location like this:
public static GeoLocation getLocation(HttpServletRequest request, DatabaseReader databaseReader) throws IOException, GeoIp2Exception{
if(request.getRemoteAddr().equals("0:0:0:0:0:0:0:1") || request.getRemoteAddr().equals("127.0.0.1")) {
return new GeoLocation("localhost", "Redmond", "USA", 47.673988, -122.121513);
}
InetAddress ipAddress = InetAddress.getByName(request.getRemoteAddr());
CityResponse response = databaseReader.city(ipAddress);
Country country = response.getCountry();
City city = response.getCity();
Location location = response.getLocation();
return new GeoLocation(ipAddress.toString(), city.getName(), country.getName(), location.getLatitude(), location.getLongitude());
}
However, every time I deployed the app I get this error
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3236)
at java.io.ByteArrayOutputStream.grow(ByteArrayOutputStream.java:118)
at java.io.ByteArrayOutputStream.ensureCapacity(ByteArrayOutputStream.java:93)
at java.io.ByteArrayOutputStream.write(ByteArrayOutputStream.java:153)
at com.maxmind.db.BufferHolder.<init>(BufferHolder.java:64)
at com.maxmind.db.Reader.<init>(Reader.java:89)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:64)
at com.maxmind.geoip2.DatabaseReader.<init>(DatabaseReader.java:54)
at com.maxmind.geoip2.DatabaseReader$Builder.build(DatabaseReader.java:160)
at com.microsoft.apie.configuration.ApplicationConfiguration.mmdbReader(ApplicationConfiguration.java:104)
My java settings are:
-Xms512m -Xmx2048m
Can you please assist?
Upvotes: 1
Views: 2011
Reputation: 123
I solved this issue by doing this:
a. I removed the file GeoLite2-City.mmdb from the jar and put in the same folder than the jar
b. I refactored the method and this:
@Bean
public static synchronized DatabaseReader mmdbReader() throws IOException {
if ( reader != null ) {
return reader;
}
/*ResourceLoader resourceLoader = new DefaultResourceLoader();
Resource resource = resourceLoader.getResource("classpath:static/mmdb/GeoLite2-Country.mmdb");
InputStream database = resource.getInputStream();
*/
FileSystemResource resource = new FileSystemResource("/path/to/file/GeoLite2-City.mmdb");
reader = new DatabaseReader.Builder(resource.getFile()).fileMode(com.maxmind.db.Reader.FileMode.MEMORY_MAPPED)
.withCache(new CHMCache()).build();
return reader;
}
c. Repackaged the app and it works!
Thanks Strelok for this suggestion
Upvotes: 3