AnsweredAssumed Answered

Problema Scheduled Action

Question asked by ruben.arjonilla on Feb 29, 2012
Buenas,

He estado siguiendo la guía de Scheduled Action del foro https://forums.alfresco.com/es/viewtopic.php?f=11&t=656

La he modificado para que haga lo que yo necesito, que basicamente es que envie un correo a todas las personas de un site, diciendoles que archivos han sido modificados en el dia anterior dentro del documentLibrary del propio site.
Este es el codigo Java:
package com.scheduledJobs;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.transaction.UserTransaction;

import net.sf.acegisecurity.Authentication;

import org.alfresco.model.ContentModel;
import org.alfresco.repo.action.executer.MailActionExecuter;
import org.alfresco.repo.security.authentication.AuthenticationComponent;
import org.alfresco.service.cmr.action.Action;
import org.alfresco.service.cmr.action.ActionService;
import org.alfresco.service.cmr.model.FileFolderService;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.repository.Path;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.ResultSet;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.transaction.TransactionService;
import org.apache.log4j.Logger;
import org.quartz.JobExecutionContext;
import org.springframework.scheduling.quartz.QuartzJobBean;

public class CronMail extends QuartzJobBean {

   private Logger logger = Logger.getLogger(CronMail.class);
   // ActionService que nos permitira ejecutar una acción de las predefinidas
   // por Alfresco
   private ActionService actionService;
   private AuthenticationComponent authenticationComponent;
   private TransactionService transactionService;
   private SiteService siteService;
   private NodeService nodeService;
   private SearchService searchService;
   private NamespaceService namespaceService;
   private FileFolderService fileFolderService;

   protected void executeInternal(JobExecutionContext jobContext) {
      try
      {
         // Aquí colocamos el código que queremos ejecutar
         logger.debug("probando el cron");
         
         // authenticate as system user
         authenticationComponent.setSystemUserAsCurrentUser();
         logger.debug("1");
         
         // save user credentials
         Authentication currentAuthentication = authenticationComponent.getCurrentAuthentication();
         logger.debug("2");
         
         //Ficheros de un site
         SiteInfo siteInfo = siteService.getSite("sitepruebamail");
         logger.debug("2.1 - "+siteInfo.getTitle());
         
         NodeRef nodeSite = siteInfo.getNodeRef();
         logger.debug("2.2");

//         NodeRef documentLibrary = fileFolderService.searchSimple(nodeSite, "documentLibrary");
         NodeRef documentLibrary = nodeService.getChildByName(nodeSite, ContentModel.ASSOC_CONTAINS, "documentLibrary");
         logger.debug("2.3");

         StoreRef storeRef = nodeSite.getStoreRef();
         logger.debug("2.4");

              Path repoScriptPath = nodeService.getPath(documentLibrary);
//         Path repoScriptPath = nodeService.getPath(nodeSite);
              logger.debug("2.5");
          
              logger.debug("3");
          
              DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
          
              Calendar cal = Calendar.getInstance();      
         String dateAvui = dateFormat.format(cal.getTime());
         
         cal.add(Calendar.DATE, -1);
              String dateAhir = dateFormat.format(cal.getTime());
          
              logger.debug("4");
          
//              String query = "+PATH:\"" + repoScriptPath.toPrefixString(namespaceService) + "//*\" AND @cm\\:modified:\""+dateAvui+"\"";
              String query = "+PATH:\"" + repoScriptPath.toPrefixString(namespaceService) + "//*\" AND @cm\\:modified:\""+dateAhir+"\"";
              logger.debug("query: "+query);
          
              ResultSet resultSet = searchService.query(storeRef, SearchService.LANGUAGE_LUCENE, query);
         
              logger.debug("5");
          
         try
              {
                  List<NodeRef> nodes = resultSet.getNodeRefs();
                  for (NodeRef nodeRef : nodes)
                  {
                      String name = (String)nodeService.getProperty(nodeRef, ContentModel.PROP_NAME);
                      if (!name.equals("doclib"))
                      {
                         archivos.add(name);
                         logger.debug("name: "+name);
                      }
                  }
              }
              finally
              {
                  resultSet.close();
              }
         
         logger.debug("6");
         
         if(archivos != null && archivos.size() != 0)
         {
            
            // Miembros de un site
            Map<String, String> mapResult = siteService.listMembers("sitepruebamail", null, null, 0, true);
            
            Iterator itr = mapResult.entrySet().iterator();
            while (itr.hasNext()) {
               Map.Entry e = (Map.Entry)itr.next();
               logger.debug("clave: "+e.getKey()+" - valor: "+e.getValue());
      
      //         Hay que poner esto para enviar a todos los miembros del site
               destinatarios.add(e.getKey().toString());
            }
            
            logger.debug("7");
            
            if(destinatarios != null && destinatarios.size() != 0)
            {
               logger.debug("8");
               
               UserTransaction tx = null;
               try {
                  tx = transactionService.getUserTransaction();
                  tx.begin();
                  
                  logger.debug("9");
                  
                  Action mailAction = actionService.createAction(MailActionExecuter.NAME);
         
                  // Estableciendo el subject del mail
                  mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, "Alfresco - Confirmación de cambios en el site: siteprovamail");
         
                  // Estableciendo el cuerpo del mensaje
                  String text = "prova <br/>Cambios en el site: "+ siteInfo.getTitle() +"<br/><br/>"+
                             "Los siguientes archivos han sido añadidos o modificados en el dia de Ayer:<br/>";
                  
                  for(int i=0; i<archivos.size();i++)
                     text = text + " - " + archivos.get(i) + "<br/>";
                     
                  text = text + "\nprueba \\nCambios en el site: "+ siteInfo.getTitle() +"\n\n"+
                             "Los siguientes archivos han sido añadidos o modificados en el dia de Ayer:\n";
                  
                  for(int i=0; i<archivos.size();i++)
                     text = text + " - " + archivos.get(i) + "\n";
                  
                  mailAction.setParameterValue(MailActionExecuter.PARAM_TEXT, text);
         
                  // Estableciento el remitente del mail
                  mailAction.setParameterValue(MailActionExecuter.PARAM_FROM, "prueba@prueba.es");
      
                  mailAction.setParameterValue(MailActionExecuter.PARAM_TO_MANY, (java.io.Serializable) destinatarios);
         
                  // Enviando mail
                  actionService.executeAction(mailAction, null);
                  
                  logger.debug("10");
               }
               catch (Throwable err) {
                  err.printStackTrace();
                  try {
                     if (tx != null) {
                        tx.rollback();
                     }
                  }
                  catch (Exception tex) {}
               } finally {
                  // restore user security context
                  authenticationComponent.setCurrentAuthentication(currentAuthentication);
               }
            }
         }
      }
      catch(Exception e)
      {
         e.printStackTrace();
         logger.debug(e.getStackTrace());
         logger.debug(e.getMessage());
         logger.debug(e.getLocalizedMessage());
      }
   }
   
   public ActionService getActionService() {
      return actionService;
   }

   public void setActionService(ActionService actionService) {
      this.actionService = actionService;
   }

   public AuthenticationComponent getAuthenticationComponent() {
      return authenticationComponent;
   }

   public void setAuthenticationComponent( AuthenticationComponent authenticationComponent) {
      this.authenticationComponent = authenticationComponent;
   }

   public TransactionService getTransactionService() {
      return transactionService;
   }

   public void setTransactionService(TransactionService transactionService) {
      this.transactionService = transactionService;
   }

   public SiteService getSiteService() {
      return siteService;
   }

   public void setSiteService(SiteService siteService) {
      this.siteService = siteService;
   }

   public NodeService getNodeService() {
      return nodeService;
   }

   public void setNodeService(NodeService nodeService) {
      this.nodeService = nodeService;
   }
   
   public SearchService getSearchService() {
      return searchService;
   }

   public void setSearchService(SearchService searchService) {
      this.searchService = searchService;
   }

   public NamespaceService getNamespaceService() {
      return namespaceService;
   }

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

   public FileFolderService getFileFolderService() {
      return fileFolderService;
   }

   public void setFileFolderService(FileFolderService fileFolderService) {
      this.fileFolderService = fileFolderService;
   }
}

Si lo ejecuto en un alfresco 3.4d me funciona correctamente y sin ningún problema.
Pero si lo ejecuto en un alfresco 3.3g me da un error (justo despues de: logger.debug("2.2");), por tanto entiendo que la linea que da el error ha de ser esta:
//         NodeRef documentLibrary = fileFolderService.searchSimple(nodeSite, "documentLibrary");
         NodeRef documentLibrary = nodeService.getChildByName(nodeSite, ContentModel.ASSOC_CONTAINS, "documentLibrary");

(pongo las 2 lineas porque he probado con las 2), y me da el error justo en cualquiera de esas 2 lineas.

El error que me da es este:
10:15:00,026 User:System ERROR [quartz.core.JobRunShell] Job DEFAULT.MiJob threw an unhandled Exception: 
org.alfresco.error.AlfrescoRuntimeException: 01280189 Transaction must be active and synchronization is required: Thread[org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1,5,main]
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.registerSynchronizations(AlfrescoTransactionSupport.java:454)
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.getSynchronization(AlfrescoTransactionSupport.java:438)
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.getResource(AlfrescoTransactionSupport.java:231)
   at org.alfresco.repo.domain.hibernate.DirtySessionMethodInterceptor.getFlushData(DirtySessionMethodInterceptor.java:208)
   at org.alfresco.repo.domain.hibernate.DirtySessionMethodInterceptor.invoke(DirtySessionMethodInterceptor.java:386)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invokeInternal(SingleEntryTransactionResourceInterceptor.java:148)
   at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invoke(SingleEntryTransactionResourceInterceptor.java:132)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy8.getNodePair(Unknown Source)
   at org.alfresco.repo.node.db.DbNodeServiceImpl.getNodePairNotNull(DbNodeServiceImpl.java:140)
   at org.alfresco.repo.node.db.DbNodeServiceImpl.getChildByName(DbNodeServiceImpl.java:1783)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   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 $Proxy10.getChildByName(Unknown Source)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.alfresco.repo.service.StoreRedirectorProxyFactory$RedirectorInvocationHandler.invoke(StoreRedirectorProxyFactory.java:215)
   at $Proxy11.getChildByName(Unknown Source)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:303)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:303)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:269)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:269)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy10.getChildByName(Unknown Source)
   at com.scheduledJobs.CronMail.executeInternal(CronMail.java:66)
   at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
10:15:00,030 User:System ERROR [quartz.core.ErrorLogger] Job (DEFAULT.MiJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception. [See nested exception: org.alfresco.error.AlfrescoRuntimeException: 01280189 Transaction must be active and synchronization is required: Thread[org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1,5,main]]
   at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
   at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:529)
Caused by: org.alfresco.error.AlfrescoRuntimeException: 01280189 Transaction must be active and synchronization is required: Thread[org.springframework.scheduling.quartz.SchedulerFactoryBean#0_Worker-1,5,main]
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.registerSynchronizations(AlfrescoTransactionSupport.java:454)
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.getSynchronization(AlfrescoTransactionSupport.java:438)
   at org.alfresco.repo.transaction.AlfrescoTransactionSupport.getResource(AlfrescoTransactionSupport.java:231)
   at org.alfresco.repo.domain.hibernate.DirtySessionMethodInterceptor.getFlushData(DirtySessionMethodInterceptor.java:208)
   at org.alfresco.repo.domain.hibernate.DirtySessionMethodInterceptor.invoke(DirtySessionMethodInterceptor.java:386)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invokeInternal(SingleEntryTransactionResourceInterceptor.java:148)
   at org.alfresco.repo.transaction.SingleEntryTransactionResourceInterceptor.invoke(SingleEntryTransactionResourceInterceptor.java:132)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy8.getNodePair(Unknown Source)
   at org.alfresco.repo.node.db.DbNodeServiceImpl.getNodePairNotNull(DbNodeServiceImpl.java:140)
   at org.alfresco.repo.node.db.DbNodeServiceImpl.getChildByName(DbNodeServiceImpl.java:1783)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   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 $Proxy10.getChildByName(Unknown Source)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.alfresco.repo.service.StoreRedirectorProxyFactory$RedirectorInvocationHandler.invoke(StoreRedirectorProxyFactory.java:215)
   at $Proxy11.getChildByName(Unknown Source)
   at sun.reflect.GeneratedMethodAccessor378.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
   at java.lang.reflect.Method.invoke(Method.java:597)
   at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:307)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
   at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:303)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.MLPropertyInterceptor.invoke(MLPropertyInterceptor.java:303)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:269)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.alfresco.repo.node.NodeRefPropertyMethodInterceptor.invoke(NodeRefPropertyMethodInterceptor.java:269)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at $Proxy10.getChildByName(Unknown Source)
   at com.scheduledJobs.CronMail.executeInternal(CronMail.java:66)
   at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:86)
   at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
   … 1 more

este es el archivo de los beans:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>
<beans>
   <!– Mi Job para el envio del mail –>
   <bean id="MiJob" class="org.springframework.scheduling.quartz.JobDetailBean">
      <property name="jobClass" value="com.scheduledJobs.CronMail"/>
      <property name="jobDataAsMap">
         <map>
            <entry key="actionService" value-ref="actionService"/>
            <entry key="transactionService" value-ref="transactionService"/>
            <entry key="authenticationComponent" value-ref="authenticationComponent"/>
            <entry key="siteService" value-ref="siteService"/>
            <entry key="nodeService" value-ref="nodeService"/>
            <entry key="searchService" value-ref="searchService"/>
            <entry key="namespaceService" value-ref="namespaceService"/>
            <entry key="fileFolderService" value-ref="fileFolderService"/>
         </map>
      </property>
   </bean>
   
   <!– Definición del trigger –>
   <bean id="cronMiJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
      <property name="jobDetail" ref="MiJob"/>
      <property name="cronExpression" value="0 55 11 * * ?"/>
   </bean>
   
   <!– Listamos el trigger en la factory para arrancarlo –>
   <bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
      <property name="triggers">
         <list>
            <ref bean="cronMiJobTrigger"/>
         </list>
      </property>
   </bean>
</beans>

Creo que tengo bien definidos los servicios.
Necesito hacer funcionar este código en la 3.3g, hay algo que he echo mal?

Muchas gracias de antemano

Outcomes