@FunctionalInterface public interface IInitializationCallback
This interface is used with IP2LocationService.Builder.buildAsync(IInitializationCallback)
to receive notification when service initialization completes. The callback is invoked
exactly once, either on success or failure.
The callback is invoked from the initialization thread, not the thread that
called buildAsync(). Implementations should be thread-safe if they
interact with shared state.
On successful initialization:
success is trueerror is nullOn initialization failure:
success is falseerror contains the exception that caused the failureIP2LocationService.dispose())
IP2LocationService service = new IP2LocationService.Builder()
.downloadToken("your-token")
.database(Database.DB11_FULL)
.buildAsync((svc, success, error) -> {
if (success) {
System.out.println("Service ready for lookups!");
} else {
System.err.println("Initialization failed: " + error.getMessage());
// Optionally retry or fall back to alternative
}
});
// Service reference is available immediately, but may not be ready
// Use awaitReady() if you need to block until initialization completes
service.awaitReady(30, TimeUnit.SECONDS);
As a functional interface, callbacks can be expressed as lambdas:
// Simple success check
builder.buildAsync((svc, ok, err) -> {
if (ok) startAcceptingRequests();
});
// With logging
builder.buildAsync((svc, ok, err) -> {
if (ok) {
log.info("IP2Location ready: IPv4={}, IPv6={}",
svc.isIPv4Ready(), svc.isIPv6Ready());
} else {
log.error("IP2Location init failed", err);
}
});
IP2LocationService.Builder.buildAsync(IInitializationCallback),
IP2LocationService.awaitReady(long, java.util.concurrent.TimeUnit)| Modifier and Type | Method and Description |
|---|---|
default IInitializationCallback |
andThen(IInitializationCallback other)
Combines this callback with another, invoking both on completion.
|
static IInitializationCallback |
noOp()
Returns a callback that does nothing.
|
void |
onInitialized(IP2LocationService service,
boolean success,
Throwable error)
Called when service initialization completes.
|
void onInitialized(IP2LocationService service, boolean success, Throwable error)
This method is called exactly once per buildAsync() invocation,
after the service has either successfully loaded its databases or
encountered an error during initialization.
The callback runs on the initialization thread. Long-running operations in this callback will delay the thread's availability for other tasks.
service - The service instance that was initialized. This is the same
instance returned by buildAsync(). Never null.success - true if initialization completed successfully and the
service is ready for lookups; false if an error occurred
during initializationerror - The exception that caused initialization to fail, or null
if initialization succeeded. Common errors include:
IOException - Database file I/O errorsUnknownHostException - Network errorsSecurityException - File permission issuesstatic IInitializationCallback noOp()
Useful when you want async initialization but don't need notification:
IP2LocationService service=builder.buildAsync(IInitializationCallback.noOp()); // Later: check service.isReady() or call service.awaitReady()
default IInitializationCallback andThen(IInitializationCallback other)
The callbacks are invoked in order: this callback first, then the other. If this callback throws an exception, the other callback is still invoked.
other - The additional callback to invokeNullPointerException - if other is nullPhantom® and NetPhantom® are registered trademarks of Mindus SARL.
© 2026 Mindus SARL. All rights reserved.