Last active
August 24, 2016 11:19
-
-
Save zeroflag/8c7333e96ab9b8c8a09fa0a3b08bb907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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