ACS-8323 Allow sending events in a transaction (#2782)

This commit is contained in:
Piotr Żurek
2024-08-28 07:00:33 +02:00
committed by GitHub
parent 1df8702e16
commit 67ca73820b
4 changed files with 79 additions and 16 deletions

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2023 Alfresco Software Limited
* Copyright (C) 2005 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -166,6 +166,11 @@ public class EventGenerator extends AbstractLifecycleBean implements Initializin
setAssociationBehaviour(BeforeDeleteAssociationPolicy.QNAME, "beforeDeleteAssociation");
}
private boolean isSendingEventBeforeCommitRequired()
{
return eventSender.shouldParticipateInTransaction();
}
/**
* Disable Events2 generated events
*/
@@ -546,23 +551,40 @@ public class EventGenerator extends AbstractLifecycleBean implements Initializin
protected class EventTransactionListener extends TransactionListenerAdapter
{
@Override
public void beforeCommit(boolean readOnly)
{
if (isSendingEventBeforeCommitRequired())
{
sendAllEvents();
}
}
@Override
public void afterCommit()
{
if (isTransactionCommitted())
if (!isSendingEventBeforeCommitRequired())
{
try
{
sendEvents();
}
catch (Exception e)
{
// Must consume the exception to protect other TransactionListeners
LOGGER.error("Unexpected error while sending repository events", e);
}
sendAllEvents();
}
}
@Override
public Integer getCustomOrder()
{
return isSendingEventBeforeCommitRequired() ? getBeforeCommitOrder() : getAfterCommitOrder();
}
protected Integer getBeforeCommitOrder()
{
return 3;
}
protected Integer getAfterCommitOrder()
{
return null;
}
/**
* @return true if a node transaction is not only active, but also committed with modifications.
* This means that a {@link TransactionEntity} object was created.
@@ -572,6 +594,21 @@ public class EventGenerator extends AbstractLifecycleBean implements Initializin
return nodeDAO.getCurrentTransactionCommitTime() != null;
}
protected void sendAllEvents()
{
if (isTransactionCommitted())
{
try
{
sendEvents();
} catch (Exception e)
{
// Must consume the exception to protect other TransactionListeners
LOGGER.error("Unexpected error while sending repository events", e);
}
}
}
private void sendEvents()
{
final Consolidators consolidators = getTxnConsolidators(this);
@@ -623,10 +660,17 @@ public class EventGenerator extends AbstractLifecycleBean implements Initializin
final REF entityReference, final CON eventConsolidator, final TriPredicate<REF, CON, EventInfo> entityToEventEligibilityVerifier)
{
final EventInfo eventInfo = getEventInfo(AuthenticationUtil.getFullyAuthenticatedUser());
transactionService.getRetryingTransactionHelper().doInTransaction((RetryingTransactionCallback<Void>) () -> {
if (isSendingEventBeforeCommitRequired())
{
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
return null;
}, true, true);
}
else
{
transactionService.getRetryingTransactionHelper().doInTransaction((RetryingTransactionCallback<Void>) () -> {
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
return null;
}, true, true);
}
}
/**

View File

@@ -49,4 +49,9 @@ public interface EventSender
{
//no initialization by default
}
default boolean shouldParticipateInTransaction()
{
return false;
}
}

View File

@@ -2,7 +2,7 @@
* #%L
* Alfresco Repository
* %%
* Copyright (C) 2005 - 2016 Alfresco Software Limited
* Copyright (C) 2005 - 2024 Alfresco Software Limited
* %%
* This file is part of the Alfresco software.
* If the software was purchased under a paid Alfresco license, the terms of
@@ -253,6 +253,10 @@ public abstract class AlfrescoTransactionSupport extends TransactionSupportUtil
{
bound = bindListener(listener, COMMIT_ORDER_CACHE);
}
else if (listener.getCustomOrder() != null)
{
bound = bindListener(listener, listener.getCustomOrder());
}
else
{
bound = bindListener(listener, COMMIT_ORDER_NORMAL);