Skip to content

Instantly share code, notes, and snippets.

@fredlahde
Last active April 18, 2016 18:30
Show Gist options
  • Save fredlahde/753757a4e86a978060f728b1178a1032 to your computer and use it in GitHub Desktop.
Save fredlahde/753757a4e86a978060f728b1178a1032 to your computer and use it in GitHub Desktop.
MySQL Async Queue
package de.fr3d63.main;
import de.fr3d63.mysql.Queue;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main extends JavaPlugin {
private Queue queue;
private Thread thread;
@Override
public void onEnable() {
queue = new Queue();
thread = new Thread(queue);
thread.start();
String a = "SELECT * from user";
queue.put(a);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ResultSet resultSet = queue.pop(a);
try {
while (resultSet.next()) {
System.out.println(resultSet.getString("username"));
}
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onDisable() {
queue.stop();
thread.interrupt();
}
}
package de.fr3d63.mysql;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class MysqlManager {
private static MysqlManager instance;
private Connection connection;
private final String host;
private final String db;
private final String user;
private final String pw;
private MysqlManager() {
host = "IP";
db = "DB";
user = "USER";
pw = "PW";
try {
openConnection();
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
}
public static MysqlManager getInstance() {
if (instance == null) {
instance = new MysqlManager();
}
return instance;
}
private void openConnection() throws SQLException, ClassNotFoundException {
if (connection != null && !connection.isClosed()) {
return;
}
synchronized (this) {
if (connection != null && !connection.isClosed()) {
return;
}
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + 3306 + "/" + this.db, this.user, this.pw);
}
}
public Connection getConnection() {
return connection;
}
}
package de.fr3d63.mysql;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
public class Queue implements Runnable {
private boolean runs;
private Map<String, Boolean> statements;
private Statement ps;
private Map<String, ResultSet> resultSetMap;
public Queue() {
this.runs = true;
statements = new HashMap<>();
resultSetMap = new HashMap<>();
}
@Override
public void run() {
while (runs) {
if (statements.size() > 0) {
for (Map.Entry<String, Boolean> entry : statements.entrySet()) {
try {
if (!entry.getValue()) {
ps = MysqlManager.getInstance().getConnection().createStatement();
ps.execute(entry.getKey());
resultSetMap.put(entry.getKey(), ps.getResultSet());
statements.remove(entry.getKey());
}
Thread.sleep(400);
} catch (SQLException | InterruptedException e) {
e.printStackTrace();
}
}
}
}
}
public void put(String statement) {
statements.put(statement, false);
}
public ResultSet pop(String statement) {
if (statements.containsKey(statement)) {
return null;
}
ResultSet rs = resultSetMap.get(statement);
resultSetMap.remove(statement);
return rs;
}
public void stop() {
this.runs = false;
try {
MysqlManager.getInstance().getConnection().close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment