AnsweredAssumed Answered

using Alfresco core services in a separate thread

Question asked by vincent-kali on Dec 11, 2014
Latest reply on Dec 11, 2014 by vincent-kali
Hi all,

For some reason, I need to run massive updates of aspects on folders recursively.
I want to execute this in a separate thread, but I don't know how to use Alfresco services
in a runnable class.
I'm trying to do the following:
- Create Alfresco services instances in my main class using Spring injection
- create runnable class instance, and pass Alfresco services instances ref using constructor
- start runnable class instance

I'm getting null pointer when trying to access Alfresco services instances.
Could somebody explain me what is best way to do that ?

Here's my code:



public class MyClass {

   private NodeService nodeService;
   private TransactionService transactionService;
   private FileFolderService fileFolderService;

   public void init() {…}

   public void updateAspects(NodeRef nodeRef){

   …..
   RecurseAspectUpdate recurseAspectUpdate = new RecurseAspectUpdate(nodeRef, transactionService, nodeService, fileFolderService);
   (new Thread(recurseAspectUpdate)).start();
   ….
      
   }   
   
   //setters are defined here for xxServices

   
            
}            
            
public class RecurseAspectUpdate implements Runnable {

   private Logger logger = Logger.getLogger(RecurseAspectUpdate.class);
   private NodeRef nodeRef;
   private TransactionService transactionService;
   private NodeService nodeService;
   private FileFolderService fileFolderService;
   static final int MAX_TRANSACTION_RETRIES = 1;

   public RecurseAspectUpdate(NodeRef nodeRef,
         TransactionService transactionService, NodeService nodeService,
         FileFolderService fileFolderService) {

      this.nodeRef = nodeRef;
      this.transactionService = transactionService;
      this.nodeService = nodeService;
      this.fileFolderService = fileFolderService;
   }

   @Override
   public synchronized void run() {
      logger.debug("About to start thread");
      AuthenticationUtil.setRunAsUserSystem();
      RetryingTransactionCallback<Object> callback = new RetryingTransactionCallback<Object>() {
         @Override
         public Object execute() throws Throwable {
            // call local method to set aspect recursively
            addChildsAspects(nodeRef);
            return null;
         }
      };
      // run transaction
      try {
         RetryingTransactionHelper txnHelper = transactionService
               .getRetryingTransactionHelper();
         txnHelper.setMaxRetries(MAX_TRANSACTION_RETRIES);// retry one time
         txnHelper.doInTransaction(callback, false, true);
      } catch (Throwable e) {
         logger.error(" Failed to add sync aspect recursively on nodeRef: "
               + nodeRef);
         logger.error(" — Error message: " + e.getMessage());
         logger.error(" — Error cause: " + e.getCause());
         e.printStackTrace();
      }
      logger.debug(" Sync aspect added recursively on nodeRef: " + nodeRef);
   }
   
   /*
   addChildsAspects method definition
   */
   
}

Outcomes