public static class IP2LocationService.Builder extends Object
IP2LocationService instances.
downloadToken(String) - Your IP2Location download token| Method | Default | Description |
|---|---|---|
database(Database) | DB11 | Database type |
includeIPv6(boolean) | false | Enable IPv6 lookups |
directory(Path) | temp/ip2location | Storage directory |
logger(IEventLogger) | Console | Event logger |
checkIntervalMs(long) | 6 hours | Update check interval |
useMemoryMappedFile(boolean) | false | Memory-mapped mode |
threadFactory(IThreadFactory) | Platform threads | Thread creation |
attemptRecovery() | false | Clear directory on startup |
enabled(boolean) | true | Enable/disable service |
build() - Blocking initialization, waits until readybuildAsync() - Returns immediately, initializes in backgroundbuildAsync(IInitializationCallback) - With completion callbackIP2LocationService| Constructor and Description |
|---|
Builder()
Creates a new Builder with default settings.
|
| Modifier and Type | Method and Description |
|---|---|
IP2LocationService.Builder |
attemptRecovery()
Enables recovery mode to clear the directory before initialization.
|
IP2LocationService |
build()
Builds and initializes the service synchronously.
|
IP2LocationService |
buildAsync()
Builds the service with asynchronous initialization.
|
IP2LocationService |
buildAsync(IInitializationCallback callback)
Builds the service with asynchronous initialization and callback.
|
IP2LocationService.Builder |
checkIntervalMs(long intervalMs)
Sets the interval between automatic update checks.
|
IP2LocationService.Builder |
database(Database database)
Sets the database type to use.
|
IP2LocationService.Builder |
directory(Path directory)
Sets the directory for storing database files.
|
IP2LocationService.Builder |
downloadToken(String token)
Sets the download token for IP2Location API authentication.
|
IP2LocationService.Builder |
enabled(boolean enabled)
Sets whether the service is enabled.
|
Path |
getDirectory()
Returns the configured directory path.
|
IP2LocationService.Builder |
includeIPv6(boolean include)
Sets whether to enable IPv6 lookups.
|
IP2LocationService.Builder |
logger(IEventLogger logger)
Sets the event logger for operational messages.
|
IP2LocationService.Builder |
threadFactory(IThreadFactory factory)
Sets the thread factory for creating background threads.
|
IP2LocationService.Builder |
useMemoryMappedFile(boolean useMemoryMapped)
Sets whether to use memory-mapped files for database access.
|
public IP2LocationService.Builder downloadToken(String token)
Get your token by signing up at lite.ip2location.com and navigating to the download area.
token - The download token (required)public IP2LocationService.Builder database(Database database)
All database types support both IPv4 and IPv6 lookups. The IPv6 database
is automatically downloaded when includeIPv6(boolean) is enabled.
database - The database type (default: DB11)public IP2LocationService.Builder includeIPv6(boolean include)
When enabled:
When disabled (default):
STATUS_IPV6_NOT_SUPPORTEDinclude - true to enable IPv6 support (default: false)public IP2LocationService.Builder directory(Path directory)
The directory will be created if it doesn't exist.
directory - Path to the storage directorypublic IP2LocationService.Builder logger(IEventLogger logger)
logger - The event logger implementationIEventLoggerpublic IP2LocationService.Builder checkIntervalMs(long intervalMs)
intervalMs - Interval in milliseconds (default: 6 hours, minimum: 1 minute)IllegalArgumentException - if interval is less than 1 minutepublic IP2LocationService.Builder useMemoryMappedFile(boolean useMemoryMapped)
useMemoryMapped - true for memory-mapped (default: false)public IP2LocationService.Builder threadFactory(IThreadFactory factory)
Use this to integrate with your application's thread management or to use virtual threads in Java 21+.
factory - The thread factory (default: platform threads)IThreadFactorypublic IP2LocationService.Builder attemptRecovery()
When enabled, all existing database files, state files, and temporary files are deleted from the configured directory before initialization. This forces a fresh download of the databases.
Use this when:
// Normal startup failed, try recovery
IP2LocationService service = new IP2LocationService.Builder()
.downloadToken("YOUR_TOKEN")
.database(Database.DB11)
.directory(Paths.get("/var/lib/ip2location"))
.attemptRecovery()
.build();
public IP2LocationService.Builder enabled(boolean enabled)
When disabled:
STATUS_SERVICE_DISABLEDThis is useful for:
boolean useGeoLocation = Boolean.parseBoolean(
System.getProperty("app.geolocation.enabled", "true"));
IP2LocationService service = new IP2LocationService.Builder()
.downloadToken("YOUR_TOKEN")
.enabled(useGeoLocation) // Can be disabled via system property
.build();
enabled - true to enable the service (default), false to disablepublic Path getDirectory()
Used internally by IP2LocationManager for directory comparison.
public IP2LocationService build()
This method blocks until:
If attemptRecovery() was called, the directory is cleared first.
After this method returns, IP2LocationService.isReady() will
return true if at least one database was loaded successfully.
IllegalStateException - if downloadToken is not setpublic IP2LocationService buildAsync()
Returns immediately with a service instance. Initialization runs
in a background thread. Use IP2LocationService.awaitReady(long, TimeUnit)
to wait for completion, or IP2LocationService.isReady() to check status.
If attemptRecovery() was called, the directory is cleared first
(during the async initialization).
IllegalStateException - if downloadToken is not setpublic IP2LocationService buildAsync(IInitializationCallback callback)
Returns immediately. The callback is invoked when initialization completes (successfully or with an error).
If attemptRecovery() was called, the directory is cleared first
(during the async initialization, before the callback is invoked).
callback - Callback for initialization result (may be null)IllegalStateException - if downloadToken is not setPhantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.