Skip to content

Commit

Permalink
prepare the grounds for async dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 2, 2017
1 parent 7bc0d95 commit f426c3b
Showing 1 changed file with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package de.metas.ui.web.window.model;

import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import javax.annotation.PostConstruct;

import org.adempiere.ad.dao.cache.CacheInvalidateRequest;
import org.compiere.util.CacheMgt;
import org.compiere.util.ICacheResetListener;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Component;

import de.metas.logging.LogManager;
import lombok.NonNull;

/*
* #%L
Expand All @@ -35,7 +40,7 @@

/**
* This component listens to all cache invalidation events (see {@link CacheMgt}) and invalidates the right documents or included documents from {@link DocumentCollection}.
*
*
* @author metas-dev <dev@metasfresh.com>
*
*/
Expand All @@ -47,29 +52,48 @@ public class DocumentCacheInvalidationDispatcher implements ICacheResetListener
@Autowired
private DocumentCollection documents;

private final Executor async;

public DocumentCacheInvalidationDispatcher()
{
final CustomizableThreadFactory asyncThreadFactory = new CustomizableThreadFactory(DocumentCacheInvalidationDispatcher.class.getSimpleName());
asyncThreadFactory.setDaemon(true);

async = Executors.newSingleThreadExecutor(asyncThreadFactory);
}

@PostConstruct
private void postConstruct()
{
CacheMgt.get().addCacheResetListener(this);
}

@Override
public int reset(final CacheInvalidateRequest request)
public int reset(@NonNull final CacheInvalidateRequest request)
{
// FIXME: atm if we are reseting async, the events are no longer aggregated because there is no trx.
// async.execute(() -> resetNow(request));
resetNow(request);

return 1; // not relevant
}

public void resetNow(final CacheInvalidateRequest request)
{
logger.debug("Got {}", request);

final String rootTableName = request.getRootTableName();
if (rootTableName == null)
{
logger.debug("Nothing to do, no rootTableName: {}", request);
return 0;
return;
}

final int rootRecordId = request.getRootRecordId();
if (rootRecordId < 0)
{
logger.debug("Nothing to do, rootRecordId < 0: {}", request);
return 0;
return;
}

final String childTableName = request.getChildTableName();
Expand All @@ -79,13 +103,11 @@ public int reset(final CacheInvalidateRequest request)
{
logger.debug("Invalidating the root document: {}", request);
documents.invalidateDocumentByRecordId(rootTableName, rootRecordId);
return 1;
}
else
{
logger.debug("Invalidating the included document: {}", request);
documents.invalidateIncludedDocumentsByRecordId(rootTableName, rootRecordId, childTableName, childRecordId);
return 1;
}
}

Expand Down

0 comments on commit f426c3b

Please sign in to comment.