Scheduled Action in Alfresco

Alfresco provide one great feature which allows you to schedule particular operation so that it will be executed repeatedly after defined time intervals. Many times this kind of requirement comes which where this feature come in handy for instance if we want to archive the old contents which are older or send report mail to moderators on daily basis from repository.

In this post we had discussed how to create java based cron jobs where we have java bean act as a backend and all business logic resided in that. Alfresco share has one more exciting feature which is called “Scheduled Actions” this is very similar to earlier case but here we have flexibility to use javascript instead of java as a backend. Along with this we could also pass many parameters which could narrow down execution and run that action or job on particular set of records only.

By following these steps you could create such action in alfresco share.

Step 1:

Create xml file called “custom-action-context.xml” (You could give any name which follows this pattern *-context.xml)

Step 2:

Add following scheduler definitions under file created in step1

<?xml version='1.0' encoding='UTF-8'?>

<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN' 'http://www.springframework.org/dtd/spring-beans.dtd'>

 

<beans>

   <bean id="templateActionModelFactory" class="org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">

       <property name="serviceRegistry">

           <ref bean="ServiceRegistry"/>

       </property>

   </bean>

  

       <bean id="runScriptAction" class="org.alfresco.repo.action.scheduled.SimpleTemplateActionDefinition">

       <property name="actionName">

           <value>script</value>

       </property>

       <property name="parameterTemplates">

           <map>

               <entry>

                   <key>

                       <value>script-ref</value>

                   </key>

                   <value>\$\{selectSingleNode('workspace://SpacesStore', 'lucene', 'PATH:"/app:company_home/app:dictionary/app:scripts/cm:removeNewFolderAspect.js"' )\}</value>

               </entry>

           </map>

       </property>

       <property name="templateActionModelFactory">

           <ref bean="templateActionModelFactory"/>

       </property>

       <property name="dictionaryService">

           <ref bean="DictionaryService"/>

       </property>

       <property name="actionService">

           <ref bean="ActionService"/>

       </property>

       <property name="templateService">

           <ref bean="TemplateService"/>

       </property>

   </bean>

 

     <bean id="runScript" class="org.alfresco.repo.action.scheduled.CronScheduledQueryBasedTemplateActionDefinition">

       <property name="transactionMode">

           <value>UNTIL_FIRST_FAILURE</value>

       </property>

       <property name="compensatingActionMode">

           <value>IGNORE</value>

       </property>

       <property name="searchService">

           <ref bean="SearchService"/>

       </property>

       <property name="templateService">

           <ref bean="TemplateService"/>

       </property>

       <property name="queryLanguage">

           <value>lucene</value>

       </property>

       <property name="stores">

           <list>

               <value>workspace://SpacesStore</value>

           </list>

       </property>

       <property name="queryTemplate">

           <value>PATH:"/app:company_home"</value>

       </property>

       <property name="cronExpression">

           <value>0 0/5 * * * ?</value>

       </property>

       <property name="jobName">

            <value>jobD</value>

       </property>

       <property name="jobGroup">

           <value>jobGroup</value>

       </property>

       <property name="triggerName">

           <value>triggerD</value>

       </property>

       <property name="triggerGroup">

           <value>triggerGroup</value>

       </property>

       <property name="scheduler">

           <ref bean="schedulerFactory"/>

       </property>

       <property name="actionService">

           <ref bean="ActionService"/>

      </property>

       <property name="templateActionModelFactory">

           <ref bean="templateActionModelFactory"/>

       </property>

       <property name="templateActionDefinition">

           <ref bean="runScriptAction"/> <!-- This is name of the action (bean) that gets run -->

       </property>

       <property name="transactionService">

           <ref bean="TransactionService"/>

       </property>

       <property name="runAsUser">

           <value>admin</value>

       </property>

   </bean>

</beans>  

 

This bean “runScriptAction” specify all parameters related to action which is to be executed at regular interval.

parameterTemplatesà Indicate script which will be executed during job run.

Second bean “runScript” is the actual bean which will be invoked first during job run which in turn invoke first bean. If you note carefully there is a parameter called “cronExpression” which specify time interval for each job run. For more details on cron expression you can refer this wiki link.

Step 3:

Create script which you had specified in above bean definition. In this case script is called “removeNewFolderAspect.js”. For guideline of writing business logic within alfresco javascript you can refer Alfresco Javascript Usage blog.

Step 4:

Upload the script into alfresco as specified location within bean.

In this case location is “Data Dictionary>scripts” as specified in “runScript” bean definition.

Step 5:

Restart alfresco server and notice your job execution through logs or user interface.

Summary: This is very powerful and handy tool which can be used to done some scheduled activities. In this case it is used to remove newFolder aspect which I am adding to each of new folder. It is my custom aspect.

Looking for quality Alfresco Web Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , , , , , | Posted under Alfresco | RSS 2.0

Author Spotlight

mitpatoliya

mitpatoliya

I love opensource technologies working with those technologies from the time I have stepped in to the Software Industry. Alfresco CMS is my area of expertise. I have worked on various complex implementations which involved integration of Alfresco with other technologies, extensively worked with JBPM workflows and Webscripts.

Leave a Reply

Your email address will not be published. Required fields are marked *


× 3 = 15

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>