A standalone, framework-agnostic async utility library for Java.
Instead of a scheduler object (like game-server frameworks have), you pass
a plain java.util.concurrent.Executor to init(), which decides how
callbacks are run "synchronously". That can be:
- the main thread of a game server (PowerNukkitX, Bukkit, ...)
- a GUI event loop (Swing, JavaFX)
- or, in a plain console application, simply
Runnable::run(run directly on the calling thread)
The library itself knows nothing about "ticks", "plugins", or server instances.
Completely standalone, e.g. in a console app:
Executor syncExecutor = Runnable::run; // run callback directly on the calling thread
MySQLConfig mysqlConfig = new MySQLConfig("127.0.0.1", 3306, "user", "password");
AsyncExecutor.init(syncExecutor, mysqlConfig);Without MySQL (generic async tasks only):
AsyncExecutor.init(Runnable::run, null);AsyncExecutor.getInstance().submitMySQLAsyncTask("mydb",
conn -> {
// runs on an async thread
try (var stmt = conn.prepareStatement("SELECT 1")) {
return stmt.executeQuery().next();
} catch (SQLException e) {
throw new RuntimeException(e);
}
},
(result, error) -> {
// runs via the syncExecutor
if (error != null) {
error.printStackTrace();
return;
}
System.out.println("Result: " + result);
});AsyncExecutor.getInstance().submitAsyncTask(
() -> heavyComputation(),
(result, error) -> System.out.println("Done: " + result));Replaces the old tick-based submitClosureTask:
AsyncExecutor.getInstance().submitDelayedTask(500, TimeUnit.MILLISECONDS,
() -> System.out.println("500ms later"));AsyncExecutor.shutdown();If you want to use this library from a PNX plugin, just build a small
Executor adapter that talks to the PNX scheduler:
Executor mainThread = task -> Server.getInstance()
.getScheduler().scheduleTask(myPlugin, task::run);
AsyncExecutor.init(mainThread, mysqlConfig);That keeps the library completely PNX-free while still being usable exactly as before in the Core plugin.
mvn clean package
Produces a shaded jar with embedded (relocated) HikariCP at
target/asynclib-1.0.0.jar.