Skip to content

Instantly share code, notes, and snippets.

@zeroflag
Last active August 24, 2016 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zeroflag/8c7333e96ab9b8c8a09fa0a3b08bb907 to your computer and use it in GitHub Desktop.
Save zeroflag/8c7333e96ab9b8c8a09fa0a3b08bb907 to your computer and use it in GitHub Desktop.
// Prevayler + Blocking queue, doesn't work
// Blocking queue
public class PrevaylerDocuments implements Documents {
private final Prevayler<BlockingQueue<Document>> prevayler;
public static PrevaylerDocuments base(String base) {
try {
return new PrevaylerDocuments(PrevaylerFactory.createPrevayler(new LinkedBlockingDeque<>(), base));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private PrevaylerDocuments(Prevayler<BlockingQueue<Document>> prevayler) {
this.prevayler = prevayler;
}
@Override
public void add(Document document) {
prevayler.execute(new AddDocument(document));
}
@Override
public Document take() {
try {
return prevayler.execute(new TakeDocument());
} catch (Exception e) {
if (e instanceof RuntimeException)
throw (RuntimeException) e;
throw new RuntimeException(e);
}
}
}
// Transaction commands
public class TakeDocument implements TransactionWithQuery<BlockingQueue<Document>, Document> {
@Override
public Document executeAndQuery(BlockingQueue<Document> documents, Date date) throws Exception {
try {
return documents.take(); // XXX blocks transaction
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
public class AddDocument implements Transaction<BlockingQueue<Document>> {
private final Document document;
public AddDocument(Document document) {
this.document = document;
}
@Override
public void executeOn(BlockingQueue<Document> documents, Date date) {
try {
documents.put(document); // XXX blocks transaction
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment