1
0
mirror of https://github.com/sismics/docs.git synced 2025-12-14 10:16:21 +00:00

#189: fire async event after transactionutil.handle

This commit is contained in:
Benjamin Gamard
2018-03-05 11:57:56 +01:00
parent e540260377
commit 4f6de892b5
5 changed files with 20 additions and 10 deletions

View File

@@ -23,7 +23,7 @@ public class TemporaryFileCleanupAsyncListener {
* Cleanup temporary files.
*
* @param event Temporary file cleanup event
* @throws Exception
* @throws Exception e
*/
@Subscribe
public void on(final TemporaryFileCleanupAsyncEvent event) throws Exception {

View File

@@ -65,8 +65,6 @@ public class TransactionUtil {
return;
}
ThreadLocalContext.cleanup();
// No error in the current request : commit the transaction
if (em.isOpen()) {
if (em.getTransaction() != null && em.getTransaction().isActive()) {
@@ -79,6 +77,12 @@ public class TransactionUtil {
}
}
}
// Fire all pending async events after request transaction commit.
// This way, all modifications done during this request are available in the listeners.
context.fireAllAsyncEvents();
ThreadLocalContext.cleanup();
}
/**

View File

@@ -8,6 +8,7 @@ import javax.persistence.EntityManager;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Iterator;
import java.util.List;
/**
@@ -112,14 +113,19 @@ public class ThreadLocalContext {
* Fire all pending async events.
*/
public void fireAllAsyncEvents() {
for (Object asyncEvent : asyncEventList) {
Iterator<Object> iterator = asyncEventList.iterator();
while (iterator.hasNext()) {
Object asyncEvent = iterator.next();
iterator.remove();
AppContext.getInstance().getAsyncEventBus().post(asyncEvent);
}
if (!temporaryFileList.isEmpty()) {
// Some files were created during this request, add a cleanup event to the queue
// It works because we are using a one thread executor
AppContext.getInstance().getAsyncEventBus().post(new TemporaryFileCleanupAsyncEvent(temporaryFileList));
AppContext.getInstance().getAsyncEventBus().post(
new TemporaryFileCleanupAsyncEvent(Lists.newArrayList(temporaryFileList)));
temporaryFileList.clear();
}
}
}