ScheduledExecutorService tylko raz zapętla
Usiłuję zaimplementować wątek ScheduledExecutorService, który zapętla co sekundę, ale od tej chwili tylko raz zapętla.
Moje pytanie brzmi: jak ustawić to tak, aby okresowo zapętlało się zamiast jednej iteracji?
Ponadto, w jaki sposób przekazać pulę połączeń do wątku, aby móc wysyłać zapytania do bazy danych każdej iteracji? Każda pomoc jest bardzo ceniona.
public static void main(String[] args) throws InterruptedException {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
AdminManager frame = new AdminManager();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
BoneCP connectionPool = null;
Connection connection = null;
try {
// load the database driver (make sure this is in your classpath!)
Class.forName("com.mysql.jdbc.Driver");
} catch (Exception e) {
e.printStackTrace();
return;
}
try {
// setup the connection pool
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername("root");
config.setPassword("");
connectionPool = new BoneCP(config); // setup the connection pool
connection = connectionPool.getConnection(); // fetch a connection
if (connection != null){
System.out.println("Connection successful!");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor();
exec.schedule(new Runnable(){
@Override
public void run(){
System.out.println("Working ... ");
}
}, 1, TimeUnit.SECONDS);
//connectionPool.shutdown(); // shutdown connection pool.
}