mirror of
https://github.com/sismics/docs.git
synced 2025-12-14 10:16:21 +00:00
Closes #113: Fire async events after request transaction commit
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package com.sismics.docs.core.service;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.google.common.util.concurrent.AbstractScheduledService;
|
||||
import com.sismics.docs.core.constant.Constants;
|
||||
import com.sismics.docs.core.event.RebuildIndexAsyncEvent;
|
||||
import com.sismics.docs.core.model.context.AppContext;
|
||||
import com.sismics.docs.core.util.DirectoryUtil;
|
||||
import com.sismics.docs.core.util.TransactionUtil;
|
||||
import org.apache.lucene.index.CheckIndex;
|
||||
import org.apache.lucene.index.CheckIndex.Status;
|
||||
import org.apache.lucene.index.CheckIndex.Status.SegmentInfoStatus;
|
||||
@@ -16,12 +18,9 @@ import org.apache.lucene.util.Version;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import com.google.common.util.concurrent.AbstractScheduledService;
|
||||
import com.sismics.docs.core.constant.Constants;
|
||||
import com.sismics.docs.core.event.RebuildIndexAsyncEvent;
|
||||
import com.sismics.docs.core.model.context.AppContext;
|
||||
import com.sismics.docs.core.util.DirectoryUtil;
|
||||
import com.sismics.docs.core.util.TransactionUtil;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Path;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Indexing service.
|
||||
@@ -129,16 +128,6 @@ public class IndexingService extends AbstractScheduledService {
|
||||
return Scheduler.newFixedDelaySchedule(0, 1, TimeUnit.HOURS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroy and rebuild Lucene index.
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public void rebuildIndex() throws Exception {
|
||||
RebuildIndexAsyncEvent rebuildIndexAsyncEvent = new RebuildIndexAsyncEvent();
|
||||
AppContext.getInstance().getAsyncEventBus().post(rebuildIndexAsyncEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Getter of directory.
|
||||
*
|
||||
|
||||
@@ -22,12 +22,12 @@ public class TransactionUtil {
|
||||
/**
|
||||
* Encapsulate a process into a transactionnal context.
|
||||
*
|
||||
* @param runnable
|
||||
* @param runnable Runnable
|
||||
*/
|
||||
public static void handle(Runnable runnable) {
|
||||
EntityManager em = ThreadLocalContext.get().getEntityManager();
|
||||
|
||||
if (em != null) {
|
||||
if (em != null && em.isOpen()) {
|
||||
// We are already in a transactional context, nothing to do
|
||||
runnable.run();
|
||||
return;
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package com.sismics.util.context;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.sismics.docs.core.model.context.AppContext;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Context associated to a user request, and stored in a ThreadLocal.
|
||||
@@ -17,7 +21,12 @@ public class ThreadLocalContext {
|
||||
* Entity manager.
|
||||
*/
|
||||
private EntityManager entityManager;
|
||||
|
||||
|
||||
/**
|
||||
* List of async events posted during this request.
|
||||
*/
|
||||
private List<Object> asyncEventList = Lists.newArrayList();
|
||||
|
||||
/**
|
||||
* Private constructor.
|
||||
*/
|
||||
@@ -63,4 +72,22 @@ public class ThreadLocalContext {
|
||||
public void setEntityManager(EntityManager entityManager) {
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an async event to the queue to be fired after the current request.
|
||||
*
|
||||
* @param asyncEvent Async event
|
||||
*/
|
||||
public void addAsyncEvent(Object asyncEvent) {
|
||||
asyncEventList.add(asyncEvent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire all pending async events.
|
||||
*/
|
||||
public void fireAllAsyncEvents() {
|
||||
for (Object asyncEvent : asyncEventList) {
|
||||
AppContext.getInstance().getAsyncEventBus().post(asyncEvent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user