AnsweredAssumed Answered

CouncurrencyFailureExecption

Question asked by john69 on Dec 6, 2010
Hi guys,

I developed a behaviour that sometimes causes a CouncurrencyFailureExecption.
It does nothing special (just a specialise type), but of course there are also other behaviour and actions configured.

Do I have to do something to be able to use a nodeService?

Thanks


/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* Alfresco is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Alfresco is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
*/
package com.test.alfresco.behaviours;

import java.util.concurrent.ThreadPoolExecutor;

import org.alfresco.repo.node.NodeServicePolicies;
import org.alfresco.repo.policy.Behaviour;
import org.alfresco.repo.policy.Behaviour.NotificationFrequency;
import org.alfresco.repo.policy.BehaviourFilter;
import org.alfresco.repo.policy.JavaBehaviour;
import org.alfresco.repo.policy.PolicyComponent;
import org.alfresco.repo.transaction.AlfrescoTransactionSupport;
import org.alfresco.repo.transaction.RetryingTransactionHelper;
import org.alfresco.repo.transaction.RetryingTransactionHelper.RetryingTransactionCallback;
import org.alfresco.repo.transaction.TransactionListener;
import org.alfresco.repo.transaction.TransactionListenerAdapter;
import org.alfresco.service.cmr.repository.ChildAssociationRef;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class SpecialiseTypeBehaviour implements NodeServicePolicies.OnCreateNodePolicy
{
   private static final String CHILD_NODE_REF = "target-node-ref";
   private static final String PARENT_NODE_REF = "parent-node-ref";
   private static final QName MYTYPE_QNAME = QName.createQName(NamespaceService.ALFRESCO_URI, "document");

   private static Log logger = LogFactory.getLog(SpecialiseTypeBehaviour.class);

   private PolicyComponent policyComponent;
   private BehaviourFilter policyFilter;
   private NodeService nodeService;

   private TransactionService transactionService;
   private ThreadPoolExecutor threadExecuter;
   private TransactionListener transactionListener;
   private NamespaceService namespaceService;
   private Behaviour onCreateNode;

   public NamespaceService getNamespaceService() {
      return namespaceService;
   }

   public void setNamespaceService(NamespaceService namespaceService) {
      this.namespaceService = namespaceService;
   }

   /**
    * Default constructor for bean construction
    */
   public SpecialiseTypeBehaviour()
   {
      this.transactionListener = new SpecialiseTypeTransactionListener();
   }

   /**
    * Sets the policy component
    *
    * @param policyComponent   the policy component
    */
   public void setPolicyComponent(PolicyComponent policyComponent)
   {
      this.policyComponent = policyComponent;
   }

   /**
    * Set the component to filter out behaviour
    *
    * @param policyFilter      the policy behaviour filter
    */
   public void setPolicyFilter(BehaviourFilter policyFilter)
   {
      this.policyFilter = policyFilter;
   }

   /**
    * Sets the node service
    *
    * @param nodeService   the node service
    */
   public void setNodeService(NodeService nodeService)
   {
      this.nodeService = nodeService;
   }

   /**
    * Set the service that allows new transactions
    *
    * @param transactionService    the transaction service
    */
   public void setTransactionService(TransactionService transactionService)
   {
      this.transactionService = transactionService;
   }

   /**
    * Set the thread pool that will handle the post-commit write transactions.
    *
    * @param threadExecuter        an <i>executor</i>
    */
   public void setThreadExecuter(ThreadPoolExecutor threadExecuter)
   {
      this.threadExecuter = threadExecuter;
   }

   /**
    * Spring initilaise method used to register the policy behaviours
    */
   public void init()
   {
      // Create behaviours
      this.onCreateNode = new JavaBehaviour(this, "onCreateNode", NotificationFrequency.TRANSACTION_COMMIT);

      // Bind behaviours to node policies
      this.policyComponent.bindClassBehaviour(QName.createQName(NamespaceService.ALFRESCO_URI, "onCreateNode"),
            QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "content"),
            this.onCreateNode);
   }

   @Override
   public void onCreateNode(ChildAssociationRef assoc) {
      AlfrescoTransactionSupport.bindListener(transactionListener);
      NodeRef parent = assoc.getParentRef();
      NodeRef child = assoc.getChildRef();
      AlfrescoTransactionSupport.bindResource(CHILD_NODE_REF, child);
      AlfrescoTransactionSupport.bindResource(PARENT_NODE_REF, parent);
   }

   private class SpecialiseTypeTransactionListener extends TransactionListenerAdapter
   {
      @Override
      public void afterCommit()
      {
         final NodeRef childNodeRef = (NodeRef) AlfrescoTransactionSupport.getResource(CHILD_NODE_REF);
            RetryingTransactionHelper txnHelper = transactionService.getRetryingTransactionHelper();
            RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>()
            {
               public Object execute() throws Throwable
               {
                  try {
                     nodeService.setType(childNodeRef, MYTYPE_DOCUMENT_QNAME);
                  }
                  catch (Exception ex) {
                     ex.printStackTrace();
                  }

                  return null;
               }
            };

            try
            {
               policyFilter.disableBehaviour(childNodeRef, QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "content"));
               txnHelper.doInTransaction(callback, false, true);
            }
            finally
            {
               policyFilter.enableBehaviour(QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "content"));
            }
         }
   }

}

Outcomes