#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. * Cleanup temporary files.
* *
* @param event Temporary file cleanup event * @param event Temporary file cleanup event
* @throws Exception * @throws Exception e
*/ */
@Subscribe @Subscribe
public void on(final TemporaryFileCleanupAsyncEvent event) throws Exception { public void on(final TemporaryFileCleanupAsyncEvent event) throws Exception {

View File

@ -65,8 +65,6 @@ public class TransactionUtil {
return; return;
} }
ThreadLocalContext.cleanup();
// No error in the current request : commit the transaction // No error in the current request : commit the transaction
if (em.isOpen()) { if (em.isOpen()) {
if (em.getTransaction() != null && em.getTransaction().isActive()) { 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.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Iterator;
import java.util.List; import java.util.List;
/** /**
@ -112,14 +113,19 @@ public class ThreadLocalContext {
* Fire all pending async events. * Fire all pending async events.
*/ */
public void fireAllAsyncEvents() { 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); AppContext.getInstance().getAsyncEventBus().post(asyncEvent);
} }
if (!temporaryFileList.isEmpty()) { if (!temporaryFileList.isEmpty()) {
// Some files were created during this request, add a cleanup event to the queue // Some files were created during this request, add a cleanup event to the queue
// It works because we are using a one thread executor // 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();
} }
} }
} }

View File

@ -188,14 +188,14 @@
</select> </select>
</div> </div>
<a class="text-center"><span class="glyphicon glyphicon-plus"></span> Add this search query on the home</a> <!--<a class="text-center"><span class="glyphicon glyphicon-plus"></span> Add this search query on the home</a>-->
<div class="pull-left" title="{{ 'document.upgrade_quota' | translate }}" <div class="pull-left small text-muted" title="{{ 'document.upgrade_quota' | translate }}"
translate="document.quota" translate="document.quota"
translate-values="{ current: userInfo.storage_current / 1000000, percent: userInfo.storage_current / userInfo.storage_quota * 100, total: userInfo.storage_quota / 1000000 }"> translate-values="{ current: userInfo.storage_current / 1000000, percent: userInfo.storage_current / userInfo.storage_quota * 100, total: userInfo.storage_quota / 1000000 }">
</div> </div>
<div class="text-right" > <div class="text-right small text-muted">
<span ng-if="totalDocuments" translate="document.count" translate-values="{ count: totalDocuments }"></span> <span ng-if="totalDocuments" translate="document.count" translate-values="{ count: totalDocuments }"></span>
<span ng-if="!totalDocuments">&nbsp;</span> <span ng-if="!totalDocuments">&nbsp;</span>
</div> </div>

View File

@ -297,7 +297,7 @@ public class TestAppResource extends BaseJerseyTest {
// Search for added documents // Search for added documents
json = target().path("/document/list") json = target().path("/document/list")
.queryParam("search", "tag:Inbox") .queryParam("search", "tag:Inbox full:content")
.request() .request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class); .get(JsonObject.class);
@ -317,7 +317,7 @@ public class TestAppResource extends BaseJerseyTest {
// Search for added documents // Search for added documents
json = target().path("/document/list") json = target().path("/document/list")
.queryParam("search", "tag:Inbox") .queryParam("search", "tag:Inbox full:content")
.request() .request()
.cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken) .cookie(TokenBasedSecurityFilter.COOKIE_NAME, adminToken)
.get(JsonObject.class); .get(JsonObject.class);