Skip to content

Instantly share code, notes, and snippets.

@madsravn
Created August 20, 2013 19:39
Show Gist options
  • Save madsravn/6286172 to your computer and use it in GitHub Desktop.
Save madsravn/6286172 to your computer and use it in GitHub Desktop.
Threading
package dk.madsravn;
import java.sql.*;
public class runner {
public static void main(String[] args) {
// Let's connect to the database first
ThreadStarter ts = new ThreadStarter();
while(true) {
ts.check();
try {
Thread.sleep(4000);
} catch (Exception e) {
System.err.println(e);
}
}
}
}
package dk.madsravn;
import org.apache.commons.dbcp.BasicDataSource;
import javax.sql.DataSource;
import java.sql.*;
import java.util.Calendar;
/**
* Created with IntelliJ IDEA.
* User: mads
* Date: 8/18/13
* Time: 1:13 AM
* To change this template use File | Settings | File Templates.
*/
public class ThreadStarter {
void threadMessage(String message) {
String threadName =
Thread.currentThread().getName();
System.out.format("%s: %s%n",
threadName,
message);
}
private class MessageLoop
implements Runnable {
private int id;
private Connection con;
MessageLoop(int id, DataSource ds) {
this.id = id;
try {
this.con = ds.getConnection();
} catch(Exception e) {
}
}
public void run() {
String importantInfo[] = {
"Mares eat oats",
"Does eat oats",
"Little lambs eat ivy",
"A kid will eat ivy too"
};
try {
startTime(id);
for (int i = 0;
i < importantInfo.length;
i++) {
// Pause for 4 seconds
Thread.sleep(4000);
// Print a message
threadMessage(importantInfo[i]);
}
} catch (InterruptedException e) {
threadMessage("I wasn't done!");
} catch(Exception e) {
System.err.println(e);
} finally {
stopTime(id);
}
}
private void stopTime(int id) {
System.out.println("Stopping");
long time = Calendar.getInstance().getTime().getTime()/1000;
String query = "UPDATE jobs SET stop=? WHERE id=?";
try {
//Connection con = ds.getConnection();
if(con!=null) {
PreparedStatement setStop = con.prepareStatement(query);
setStop.setLong(1, time);
setStop.setInt(2, id);
setStop.executeQuery();
}
} catch(Exception e) {
System.err.println("Stop " + e);
}
}
private void startTime(int id) {
System.out.println("Starting");
long time = Calendar.getInstance().getTime().getTime()/1000;
String query = "UPDATE jobs SET start=? WHERE id=?";
try {
//Connection con = ds.getConnection();
if(con!=null) {
PreparedStatement setStart = con.prepareStatement(query);
setStart.setLong(1, time);
setStart.setInt(2, id);
setStart.executeQuery();
con.close();
}
} catch(Exception e) {
System.err.println("Start " + e);
}
}
}
private DataSource ds;
ThreadStarter() {
ds = setupDataSource();
}
public DataSource setupDataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUsername("postgres");
ds.setPassword("code");
ds.setUrl("jdbc:postgresql://localhost:5432/threads");
return ds;
}
public void check() {
Statement stmt = null;
String query =
"SELECT id, start, stop, command" +
" FROM jobs WHERE start = ?;";
System.out.println("Checking");
try {
Connection con = ds.getConnection();
if(con != null) {
PreparedStatement findID = con.prepareStatement(query);
findID.setInt(1, -1);
ResultSet rs = findID.executeQuery();
//ResultSet rs = stmt.executeQuery(query);
while(rs.next()) {
Thread t = new Thread(new MessageLoop(rs.getInt(1),ds));
t.start();
}
con.close();
}
} catch (Exception e ) {
System.err.println(e);
} finally {
try {
if(stmt != null) {
stmt.close();
}
} catch (Exception e) {
System.err.println(e);
}
}
}
public void startThread(int id) {
//TODO: Selection is not necessary here.
Statement stmt = null;
String query =
"SELECT id, start, stop, command" +
" FROM jobs where id = ? ;";
try {
Connection con = ds.getConnection();
if(con != null) {
PreparedStatement findID = con.prepareStatement(query);
findID.setInt(1, id);
ResultSet rs = findID.executeQuery();
//ResultSet rs = stmt.executeQuery(query);
rs.next(); // Call to go to the first
System.out.println("Is first: " + rs.isFirst());
System.out.println(rs.getInt(1) + ", " + rs.getInt(2) + ", " + rs.getInt(3) + ", " + rs.getString(4));
}
} catch (Exception e ) {
System.err.println(e);
} finally {
try {
if(stmt != null) {
stmt.close();
}
} catch (Exception e) {
System.err.println(e);
}
}
}
public void close() {
try {
BasicDataSource bds = (BasicDataSource) ds;
bds.close();
} catch (Exception e) {
System.err.println("Couldn't close BasicDataSource " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment