From e75941d75a6fe11fdebf062d2c3a8a726b2017e4 Mon Sep 17 00:00:00 2001 From: Govindass Date: Mon, 18 May 2020 15:34:43 +0300 Subject: [PATCH] Make thread pool configurable --- build.gradle | 4 +- .../java/com/btk5h/skriptdb/SkriptDB.java | 38 ++++++++++++++++++- .../skriptdb/skript/EffExecuteStatement.java | 2 +- src/main/resources/plugin.yml | 3 +- 4 files changed, 42 insertions(+), 5 deletions(-) diff --git a/build.gradle b/build.gradle index 0fdc2c1..8c6191f 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'com.btk5h.skript-db' -version '0.1.1' +version '1.1.0' buildscript { repositories { @@ -53,4 +53,4 @@ task fatJar(type: Jar) { baseName = project.name + '-all' from { configurations.compile.collect { it.isDirectory() ? it : zipTree(it) } } with jar -} \ No newline at end of file +} diff --git a/src/main/java/com/btk5h/skriptdb/SkriptDB.java b/src/main/java/com/btk5h/skriptdb/SkriptDB.java index cd7b4c0..a94ecde 100644 --- a/src/main/java/com/btk5h/skriptdb/SkriptDB.java +++ b/src/main/java/com/btk5h/skriptdb/SkriptDB.java @@ -25,9 +25,11 @@ package com.btk5h.skriptdb; +import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.plugin.java.JavaPlugin; -import java.io.IOException; +import java.io.*; +import java.nio.charset.StandardCharsets; import java.sql.SQLException; import javax.sql.rowset.RowSetFactory; @@ -49,6 +51,7 @@ public final class SkriptDB extends JavaPlugin { private static SkriptAddon addonInstance; private static RowSetFactory rowSetFactory; + protected FileConfiguration config; public SkriptDB() { if (instance == null) { @@ -58,6 +61,34 @@ public final class SkriptDB extends JavaPlugin { } } + private void setupConfig() throws IOException { + //don't check if it exists, because mkdir already does that + File file = new File("plugins/skript-db/config.yml"); + if (file.getParentFile().mkdirs()) { + BufferedWriter out = null; + try { + out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("plugins/skript-db/config.yml", false), StandardCharsets.UTF_8)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + try { + if (out == null) return; + + out.write("# How many connections can be awaited for simultaneously, may be useful to increase if mysql database is hosted on a separate machine to account for ping.\n"); + out.write("# If it is hosted within the same machine, set it to the count of cores your processor has or the count of threads your processor can process at once.\n"); + out.write("thread-pool-size: " + (Runtime.getRuntime().availableProcessors() + 1) + "\n"); + } catch (IOException e) { + e.printStackTrace(); + } + + try { + out.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + @Override public void onEnable() { try { @@ -69,6 +100,11 @@ public final class SkriptDB extends JavaPlugin { } catch (IOException e) { e.printStackTrace(); } + try { + setupConfig(); + } catch (IOException e) { + e.printStackTrace(); + } } public static SkriptAddon getAddonInstance() { diff --git a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java index 390ec2a..8ffaa07 100644 --- a/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java +++ b/src/main/java/com/btk5h/skriptdb/skript/EffExecuteStatement.java @@ -51,7 +51,7 @@ public class EffExecuteStatement extends Delay { static String lastError; private static final ExecutorService threadPool = - Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); + Executors.newFixedThreadPool(SkriptDB.getInstance().getConfig().getInt("thread-pool-size")); private Expression query; private Expression dataSource; diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2df1b4e..2288390 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,4 +1,5 @@ name: skript-db -version: 0.1.1 +version: 1.1.0 main: com.btk5h.skriptdb.SkriptDB depend: [Skript] +authors: [btk5h, FranKusmiruk, Govindas]