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

@@ -1,5 +1,5 @@
/*
* Copyright (C) 2005-2023 Alfresco Software Limited.
* Copyright (C) 2005-2024 Alfresco Software Limited.
*
* This file is part of Alfresco
*
@@ -67,4 +67,14 @@ public interface TransactionListener
* be used only for cleaning up resources after a rollback has occurred.
*/
void afterRollback();
/**
* Allows to provide a custom listener's order.
* See {@link org.alfresco.repo.transaction.AlfrescoTransactionSupport#COMMIT_ORDER_NORMAL}
* @return custom order or null for the default one
*/
default Integer getCustomOrder()
{
return null;
}
}

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,21 +551,38 @@ 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;
}
/**
@@ -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,11 +660,18 @@ 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());
if (isSendingEventBeforeCommitRequired())
{
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
}
else
{
transactionService.getRetryingTransactionHelper().doInTransaction((RetryingTransactionCallback<Void>) () -> {
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
return null;
}, true, true);
}
}
/**
* Creates events from various kinds of entities.

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);