mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
ACS-8323 Allow sending events in a transaction (#2782)
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (C) 2005-2023 Alfresco Software Limited.
|
* Copyright (C) 2005-2024 Alfresco Software Limited.
|
||||||
*
|
*
|
||||||
* This file is part of Alfresco
|
* 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.
|
* be used only for cleaning up resources after a rollback has occurred.
|
||||||
*/
|
*/
|
||||||
void afterRollback();
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
* #%L
|
* #%L
|
||||||
* Alfresco Repository
|
* Alfresco Repository
|
||||||
* %%
|
* %%
|
||||||
* Copyright (C) 2005 - 2023 Alfresco Software Limited
|
* Copyright (C) 2005 - 2024 Alfresco Software Limited
|
||||||
* %%
|
* %%
|
||||||
* This file is part of the Alfresco software.
|
* This file is part of the Alfresco software.
|
||||||
* If the software was purchased under a paid Alfresco license, the terms of
|
* 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");
|
setAssociationBehaviour(BeforeDeleteAssociationPolicy.QNAME, "beforeDeleteAssociation");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isSendingEventBeforeCommitRequired()
|
||||||
|
{
|
||||||
|
return eventSender.shouldParticipateInTransaction();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Disable Events2 generated events
|
* Disable Events2 generated events
|
||||||
*/
|
*/
|
||||||
@@ -546,21 +551,38 @@ public class EventGenerator extends AbstractLifecycleBean implements Initializin
|
|||||||
|
|
||||||
protected class EventTransactionListener extends TransactionListenerAdapter
|
protected class EventTransactionListener extends TransactionListenerAdapter
|
||||||
{
|
{
|
||||||
|
@Override
|
||||||
|
public void beforeCommit(boolean readOnly)
|
||||||
|
{
|
||||||
|
if (isSendingEventBeforeCommitRequired())
|
||||||
|
{
|
||||||
|
sendAllEvents();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterCommit()
|
public void afterCommit()
|
||||||
{
|
{
|
||||||
if (isTransactionCommitted())
|
if (!isSendingEventBeforeCommitRequired())
|
||||||
{
|
{
|
||||||
try
|
sendAllEvents();
|
||||||
{
|
|
||||||
sendEvents();
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
// Must consume the exception to protect other TransactionListeners
|
|
||||||
LOGGER.error("Unexpected error while sending repository events", e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@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;
|
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()
|
private void sendEvents()
|
||||||
{
|
{
|
||||||
final Consolidators consolidators = getTxnConsolidators(this);
|
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 REF entityReference, final CON eventConsolidator, final TriPredicate<REF, CON, EventInfo> entityToEventEligibilityVerifier)
|
||||||
{
|
{
|
||||||
final EventInfo eventInfo = getEventInfo(AuthenticationUtil.getFullyAuthenticatedUser());
|
final EventInfo eventInfo = getEventInfo(AuthenticationUtil.getFullyAuthenticatedUser());
|
||||||
|
if (isSendingEventBeforeCommitRequired())
|
||||||
|
{
|
||||||
|
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
transactionService.getRetryingTransactionHelper().doInTransaction((RetryingTransactionCallback<Void>) () -> {
|
transactionService.getRetryingTransactionHelper().doInTransaction((RetryingTransactionCallback<Void>) () -> {
|
||||||
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
|
eventSender.accept(() -> createEvent(entityReference, eventConsolidator, eventInfo, entityToEventEligibilityVerifier));
|
||||||
return null;
|
return null;
|
||||||
}, true, true);
|
}, true, true);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates events from various kinds of entities.
|
* Creates events from various kinds of entities.
|
||||||
|
@@ -49,4 +49,9 @@ public interface EventSender
|
|||||||
{
|
{
|
||||||
//no initialization by default
|
//no initialization by default
|
||||||
}
|
}
|
||||||
|
|
||||||
|
default boolean shouldParticipateInTransaction()
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -2,7 +2,7 @@
|
|||||||
* #%L
|
* #%L
|
||||||
* Alfresco Repository
|
* Alfresco Repository
|
||||||
* %%
|
* %%
|
||||||
* Copyright (C) 2005 - 2016 Alfresco Software Limited
|
* Copyright (C) 2005 - 2024 Alfresco Software Limited
|
||||||
* %%
|
* %%
|
||||||
* This file is part of the Alfresco software.
|
* This file is part of the Alfresco software.
|
||||||
* If the software was purchased under a paid Alfresco license, the terms of
|
* 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);
|
bound = bindListener(listener, COMMIT_ORDER_CACHE);
|
||||||
}
|
}
|
||||||
|
else if (listener.getCustomOrder() != null)
|
||||||
|
{
|
||||||
|
bound = bindListener(listener, listener.getCustomOrder());
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
bound = bindListener(listener, COMMIT_ORDER_NORMAL);
|
bound = bindListener(listener, COMMIT_ORDER_NORMAL);
|
||||||
|
Reference in New Issue
Block a user