Skip to content

Instantly share code, notes, and snippets.

@madsravn
Created August 20, 2013 21:07
Show Gist options
  • Save madsravn/6287349 to your computer and use it in GitHub Desktop.
Save madsravn/6287349 to your computer and use it in GitHub Desktop.
Threading
package dk.madsravn;
import java.sql.*;
import java.util.Calendar;
public class runner {
public static void main(String[] args) {
// Let's connect to the database first
ThreadStarter ts = new ThreadStarter();
long timestart = Calendar.getInstance().getTime().getTime();
while(true) {
if(Calendar.getInstance().getTime().getTime() - timestart > 5000) {
ts.check();
timestart = Calendar.getInstance().getTime().getTime();
}
}
}
}
package dk.madsravn;
import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
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, BoneCP bcp) {
this.id = id;
try {
this.con = bcp.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 BoneCP bcp;
ThreadStarter() {
//ds = setupDataSource();
try {
bcp = setupBoneCP();
} catch (Exception e) {
System.err.println(e);
}
}
public DataSource setupDataSource() {
BasicDataSource ds = new BasicDataSource();
ds.setDriverClassName("org.postgresql.Driver");
ds.setUsername("postgres");
ds.setPassword("brumm87r");
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 = bcp.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()) {
if(bcp != null) {
Thread t = new Thread(new MessageLoop(rs.getInt(1),bcp));
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 = bcp.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 BoneCP setupBoneCP() throws SQLException {
BoneCPConfig config = new BoneCPConfig();
config.setJdbcUrl("jdbc:postgresql://localhost:5432/threads"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
config.setUsername("postgres");
config.setPassword("brumm87r");
config.setMinConnectionsPerPartition(5);
config.setMaxConnectionsPerPartition(10);
config.setPartitionCount(1);
return new BoneCP(config); // setup the connection pool
}
public void close() {
try {
bcp.close();
} catch (Exception e) {
System.err.println("Couldn't close BoneCP " + e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment