by Andrew Brampton 2013
Some useful java code backed by JDBC that implements some common patterns.
So far a Condition object, and a Queue are implemented.
<dependency>
<groupId>net.bramp.db-patterns</groupId>
<artifactId>db-patterns</artifactId>
<version>0.1.1</version>
</dependency>
A distributed Java Condition
DataSource ds = ...
Condition condition = new MySQLSleepBasedCondition(ds, "lockname");
condition.await(); // Blocks until a notify
// on another thread (or process, or machine)
condition.notify();
The MySQLSleepBasedCondition is based on the MySQL SLEEP()
and KILL QUERY
The thread that is woken up is guaranteed to be the one that has waited the longest.
A distributed MySQL backed Java BlockingQueue
DataSource ds = ...
BlockingQueue<String> queue = new MySQLBasedQueue<String>(ds, "queue name", String.class);
queue.add("Some String");
// on another thread (or process, or machine)
String s = queue.poll(); // Non blocking
// or
String s = queue.take(); // Blocks until element available
The MySQLBasedQueue uses the MySQLSleepBasedCondition to help form a blocking queue, that can work without polling the database for new work.
To build this project use mvn
.
To push a release to maven central use the standard maven release plugin, and Sonatype's OSS repo:
mvn release:prepare
mvn release:perform
https://blog.engineyard.com/2011/5-subtle-ways-youre-using-mysql-as-a-queue-and-why-itll-bite-you