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
DataSourceds = ...
Conditioncondition = newMySQLSleepBasedCondition(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
DataSourceds = ...
BlockingQueue<String> queue = newMySQLBasedQueue<String>(ds, "queue name", String.class);
queue.add("Some String");
// on another thread (or process, or machine)Strings = queue.poll(); // Non blocking// orStrings = queue.take(); // Blocks until element availableThe 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:performhttps://blog.engineyard.com/2011/5-subtle-ways-youre-using-mysql-as-a-queue-and-why-itll-bite-you