cancel
Showing results for 
Search instead for 
Did you mean: 

using Alfresco core services in a separate thread

vincent-kali
Star Contributor
Star Contributor
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
   */
   
}
3 REPLIES 3

mrogers
Star Contributor
Star Contributor
It should be O.K.  

Your NPE will tell you which line of code you have wrong and therefore identify which object you have forgotten to set.   But you haven't included that in your description.

Thanks a lot for your quick answer.
I found an issue in my Spring injection which is now fixed. Alfresco services are now usable in my separate thread, but I'm facing
a new issue that I just don't understand, when trying to set aspect recursively.
Do you have any idea/advice ?

thanks.

2014-12-11 17:50:40,255  ERROR [ks2.easysync.RecurseAspectUpdate] [Thread-157]  Failed to add sync aspect recursively on nodeRef: workspace://SpacesStore/66f5006e-2853-4cad-b45b-b338718a85fa
2014-12-11 17:50:40,256  ERROR [ks2.easysync.RecurseAspectUpdate] [Thread-157]  — Error message: Failed to update node 130256
2014-12-11 17:50:40,256  ERROR [ks2.easysync.RecurseAspectUpdate] [Thread-157]  — Error cause: null
org.springframework.dao.ConcurrencyFailureException: Failed to update node 130256
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.updateNodeImpl(AbstractNodeDAOImpl.java:1899)
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.touchNode(AbstractNodeDAOImpl.java:1725)
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.setNodePropertiesImpl(AbstractNodeDAOImpl.java:2309)
        at org.alfresco.repo.domain.node.AbstractNodeDAOImpl.addNodeProperties(AbstractNodeDAOImpl.java:2416)
        at org.alfresco.repo.node.db.DbNodeServiceImpl.addAspectsAndProperties(DbNodeServiceImpl.java:553)
        at org.alfresco.repo.node.db.DbNodeServiceImpl.addAspectsAndProperties(DbNodeServiceImpl.java:455)
        at org.alfresco.repo.node.db.DbNodeServiceImpl.addAspect(DbNodeServiceImpl.java:793)
        at sun.reflect.GeneratedMethodAccessor655.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
        at org.alfresco.repo.tenant.MultiTNodeServiceInterceptor.invoke(MultiTNodeServiceInterceptor.java:104)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy11.addAspect(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor655.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.alfresco.repo.service.StoreRedirectorProxyFactory$RedirectorInvocationHandler.invoke(StoreRedirectorProxyFactory.java:215)
        at com.sun.proxy.$Proxy30.addAspect(Unknown Source)
        at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:301)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.alfresco.repo.lock.mem.LockableAspectInterceptor.invoke(LockableAspectInterceptor.java:200)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:149)
        at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
        at com.sun.proxy.$Proxy11.addAspect(Unknown Source)
        at sun.reflect.GeneratedMethodAccessor655.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:606)
        at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:309)
        at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:196)
        at com.sun.proxy.$Proxy11.addAspect(Unknown Source)
        at org.alfresco.extension.ks2.easysync.RecurseAspectUpdate.addChildsAspects(RecurseAspectUpdate.java:102)
        at org.alfresco.extension.ks2.easysync.RecurseAspectUpdate.access$2(RecurseAspectUpdate.java:88)
        at org.alfresco.extension.ks2.easysync.RecurseAspectUpdate$1.execute(RecurseAspectUpdate.java:61)
        at org.alfresco.repo.transaction.RetryingTransactionHelper.doInTransaction(RetryingTransactionHelper.java:452)
        at org.alfresco.extension.ks2.easysync.RecurseAspectUpdate.run(RecurseAspectUpdate.java:72)
        at java.lang.Thread.run(Thread.java:724)

vincent-kali
Star Contributor
Star Contributor
Thanks a lot for your quick answer. You're right, I missed something when injecting alfresco services.
Vincent