Skip to content

xxPLUGINS/MySQL-AsyncLib

Repository files navigation

asynclib

A standalone, framework-agnostic async utility library for Java.

Concept

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.

Usage

1. Init (once at startup)

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);

2. MySQL task

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);
    });

3. Generic async task

AsyncExecutor.getInstance().submitAsyncTask(
    () -> heavyComputation(),
    (result, error) -> System.out.println("Done: " + result));

4. Delayed task

Replaces the old tick-based submitClosureTask:

AsyncExecutor.getInstance().submitDelayedTask(500, TimeUnit.MILLISECONDS,
    () -> System.out.println("500ms later"));

5. Shutdown

AsyncExecutor.shutdown();

Integrating with PowerNukkitX (example, not part of the library)

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.

Building

mvn clean package

Produces a shaded jar with embedded (relocated) HikariCP at target/asynclib-1.0.0.jar.

About

No description, website, or topics provided.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Packages

 
 
 

Contributors

Languages