cancel
Showing results for 
Search instead for 
Did you mean: 

Behavior Execution onCommit and not beforeCommit

xian
Champ on-the-rise
Champ on-the-rise
Hi
I have a policy that detects node creation and passes the reference to another thread for processing. The problem is that the policies behaviour is executed before the transaction has finished committing and so the worker thread can't 'see' the new node in its transaction if it begins execution soon enough. Is there a way to trigger the policy execution after the commit, or in the policy queue some kind of task for when it finishes comitting?

Some code from the policy class:

   public void init() {
      policyComponent.bindClassBehaviour(
         NodeServicePolicies.OnCreateNodePolicy.QNAME,
         UncModel.TYPE_STATE,
         new JavaBehaviour(
            this,
            "onCreateNode",
            NotificationFrequency.TRANSACTION_COMMIT
         )
      );
   }

and


   public void onCreateNode(ChildAssociationRef car) {
      sender.queueItem(car.getChildRef(), false);
   }
2 REPLIES 2

mrogers
Star Contributor
Star Contributor
The action service allows you to queue actions that will run post commit, it underpins the RuleService. That's probably your best bet.

I don't think a policy will help, although you could register a TransactionListener to listen for the commit.

xian
Champ on-the-rise
Champ on-the-rise
I had quite a bit of help on email too and have implemented the TransactionListener. It's quite simple and works well.

You should look at the "SDK Custom Aspect" in the SDK, which uses a TransactionListenerAdapter to execute stuff in its afterCommit() method so that it is effectively executed after the transaction has been committed.

The working code is below. The Aggregator is just another bean that starts a thread to do the work.


package com.culturenetcymru.unc;

import com.culturenetcymru.unc.senders.Aggregator;
import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;

/**
*
* @author Xian Stannard
*/
public class SendStatePolicy extends TransactionListenerAdapter implements NodeServicePolicies.OnCreateNodePolicy {
   public static final String NODE_KEY = SendStatePolicy.class.getName() + ".node";
   private PolicyComponent policyComponent;
   private Aggregator sender;


   public void setPolicyComponent(PolicyComponent policyComponent) {
      this.policyComponent = policyComponent;
   }

   public void setSender(Aggregator sender) {
      this.sender = sender;
   }

   public void init() {
      policyComponent.bindClassBehaviour(
         NodeServicePolicies.OnCreateNodePolicy.QNAME,
         UncModel.TYPE_STATE,
         new JavaBehaviour(
            this,
            "onCreateNode",
            NotificationFrequency.TRANSACTION_COMMIT
         )
      );
   }

   public void onCreateNode(ChildAssociationRef car) {
      AlfrescoTransactionSupport.bindListener(this);
      AlfrescoTransactionSupport.bindResource(NODE_KEY, car.getChildRef());
   }

   @Override
   public void afterCommit() {
      NodeRef node = AlfrescoTransactionSupport.getResource(SendStatePolicy.NODE_KEY);
      if(node != null)
         sender.queueItem(node, false);
   }
}