1010import com .mengcraft .simpleorm .provider .IRedisProvider ;
1111import com .mengcraft .simpleorm .redis .RedisProviders ;
1212import com .zaxxer .hikari .HikariDataSource ;
13+ import lombok .Getter ;
1314import lombok .NonNull ;
1415import lombok .SneakyThrows ;
1516import lombok .val ;
2526import java .util .HashMap ;
2627import java .util .Map ;
2728import java .util .Objects ;
29+ import java .util .concurrent .CompletableFuture ;
2830import java .util .function .Supplier ;
31+ import java .util .logging .Level ;
2932
3033import static com .mengcraft .simpleorm .lib .Tuple .tuple ;
3134
@@ -38,9 +41,11 @@ public class ORM extends JavaPlugin {
3841 private static RedisWrapper globalRedisWrapper ;
3942 private static MongoWrapper globalMongoWrapper ;
4043 private static DataSource sharedDs ;
41- private static ORM plugin ;
44+ static ORM plugin ;
4245 private static IDataSourceProvider dataSourceProvider = new DataSourceProvider ();
4346 private static IRedisProvider redisProvider ;
47+ @ Getter
48+ private static FluxWorkers workers ;
4449
4550 @ Override
4651 public void onLoad () {
@@ -78,6 +83,7 @@ private static void loadExtLibrary(JavaPlugin plugin) {
7883 @ SneakyThrows
7984 public void onEnable () {
8085 new MetricsLite (this );
86+ workers = new FluxWorkers (getConfig ().getInt ("cpus" , 8 ));
8187 if (nil (globalRedisWrapper )) {
8288 if (redisProvider == null ) {
8389 FileConfiguration config = getConfig ();
@@ -97,6 +103,16 @@ public void onEnable() {
97103 getLogger ().info ("Welcome!" );
98104 }
99105
106+ @ Override
107+ public void onDisable () {
108+ workers .close ();
109+ try {
110+ workers .awaitClose (Integer .MAX_VALUE );
111+ } catch (InterruptedException e ) {
112+ getLogger ().log (Level .WARNING , "Error occurred while await workers closed" , e );
113+ }
114+ }
115+
100116 public static boolean nil (Object any ) {
101117 return any == null ;
102118 }
@@ -251,4 +267,20 @@ public static void setRedisProvider(IRedisProvider redisProvider) {
251267 Preconditions .checkState (!isFullyEnabled (), "Cannot be set after ORM enabled" );
252268 ORM .redisProvider = redisProvider ;
253269 }
270+
271+ public static CompletableFuture <Void > enqueue (Runnable runnable ) {
272+ return CompletableFuture .runAsync (runnable , workers .of ());
273+ }
274+
275+ public static CompletableFuture <Void > enqueue (String ns , Runnable runnable ) {
276+ return CompletableFuture .runAsync (runnable , workers .of (ns ));
277+ }
278+
279+ public static <T > CompletableFuture <T > enqueue (Supplier <T > supplier ) {
280+ return CompletableFuture .supplyAsync (supplier , workers .of ());
281+ }
282+
283+ public static <T > CompletableFuture <T > enqueue (String ns , Supplier <T > supplier ) {
284+ return CompletableFuture .supplyAsync (supplier , workers .of (ns ));
285+ }
254286}
0 commit comments