blueprint.sdk.util.queue.JdbcQueue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of blueprint-sdk Show documentation
Show all versions of blueprint-sdk Show documentation
Personal library for Java development. Deployed on OSSRH for Apache Maven.
The newest version!
/*
License:
blueprint-sdk is licensed under the terms of Eclipse Public License(EPL) v1.0
(http://www.eclipse.org/legal/epl-v10.html)
Distribution:
Maven Central - https://search.maven.org/artifact/io.github.lempel/blueprint-sdk
MVN Repository - https://mvnrepository.com/artifact/io.github.lempel/blueprint-sdk
*/
package blueprint.sdk.util.queue;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.UUID;
/**
* Prototype for JDBC based Queue.
*
* Must call {@link JdbcQueue#init()} before use.
*
* @author [email protected]
* @since 2013. 8. 26.
*/
public abstract class JdbcQueue extends MessageQueue {
/**
* actual queue
*/
final LinkedList queue = new LinkedList<>();
/**
* DataSource for persistence
*/
final DataSource datasrc;
/**
* Constructor
*
* @param datasrc DataSource for persistence
*/
@SuppressWarnings("WeakerAccess")
public JdbcQueue(DataSource datasrc) {
super();
this.datasrc = datasrc;
if (datasrc == null) {
throw new NullPointerException("DataSource is null");
}
}
/**
* create table and load queue contents from DataSource
*
* @throws SQLException
*/
public void init() throws SQLException {
synchronized (queue) {
createTable();
load();
}
}
@Override
public void clear() throws JdbcQueueException {
synchronized (queue) {
try {
emptyTable();
queue.clear();
} catch (SQLException e) {
throw new JdbcQueueException(e);
}
}
}
@Override
public void push(String element) {
push(element, true);
}
/**
* Push an element to queue.
*
* @param element Element to push
* @param persistent true: store to Database, false: don't store
*/
@SuppressWarnings({"SameParameterValue", "WeakerAccess"})
public void push(String element, boolean persistent) {
if (element == null) {
throw new NullPointerException("Can't push null");
}
JdbcElement item = new JdbcElement();
item.uuid = UUID.randomUUID().toString();
item.content = element;
item.persistent = persistent;
try {
if (persistent) {
insert(item);
}
synchronized (queue) {
queue.push(item);
}
notifyWaiter();
} catch (SQLException e) {
throw new JdbcQueueException(e);
}
}
@Override
public String pop() throws JdbcQueueException {
String result = null;
try {
JdbcElement element;
synchronized (queue) {
element = queue.pop();
}
if (element != null) {
try {
if (element.persistent) {
delete(element);
}
result = element.content;
} catch (SQLException e) {
synchronized (queue) {
queue.push(element);
}
notifyWaiter();
throw new JdbcQueueException(e);
}
}
} catch (NoSuchElementException ignored) {
// just return null
}
return result;
}
/**
* Load queue contents from DBMS
*
* @throws SQLException Can't load
*/
protected abstract void load() throws SQLException;
/**
* Create table for queue (if not exits)
*
* @throws SQLException Can't create
*/
protected abstract void createTable() throws SQLException;
/**
* Empty table for queue
*
* @throws SQLException Can't delete
*/
protected abstract void emptyTable() throws SQLException;
/**
* Insert queue element to table
*
* @param element element to insert
* @throws SQLException Can't insert
*/
protected abstract void insert(Element element) throws SQLException;
/**
* Delete queue element from table
*
* @param element element to delete
* @throws SQLException Can't delete
*/
protected abstract void delete(Element element) throws SQLException;
}
/**
* Internal element of JdbcQueue
*
* @author [email protected]
* @since 2013. 10. 25.
*/
class JdbcElement extends Element {
/**
* true: store to Database, false: don't store
*/
// default true for load()
public boolean persistent = true;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy