Scheduled actions

Now you know how to define and execute actions manually. You can also execute these actions automatically, at a specified time. This will be helpful in use cases such as checking documents' statuses periodically, or for generating reports or executing business rules and notifying certain users.

Alfresco supports Scheduled actions so that you can configure certain actions to run at a certain time, automatically. A scheduled action is made up of three parts:

  • A cron expression,
  • A query template, and
  • An action template.

At the times specified by the cron expression, the query template will generate a query to select a set of nodes (Spaces and Documents). For each of these nodes, the action template applies the action.

The query may be similar to:

  • A document has aspect
  • It was created in the last month
  • It is due in the next month
  • It is in a category

Example of archiving expired content

Refer to the sample custom JavaScript (chapter6_archive_expired_content.js) you created earlier, which moves the expired content to the Archived space.

We have considered a scenario where the HR group maintains certain corporate forms and policies that are time-bound, that is, they will expire after some period or date. In the earlier example, you manually ran the script. In this section, you are going to create a scheduled action that executes the custom JavaScript automatically, every fifteen minutes.

You need to create a schedule action XML configuration file in your extensions folder, in order to specify the scheduled time, and the custom JavaScript. You will find the scheduled action sample file, scheduled-action-services-context.xml.sample, in the location below.

JBoss: <alfresco>/jboss/server/default/conf/alfresco/extension

Tomcat: <alfresco>/tomcat/shared/classes/alfresco/extension

This file has many good sample scheduled actions for your reference.

Copy the scheduled action sample file as scheduled-action-services-context.xml in the same folder, and edit the file as follows:

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE beans PUBLIC '-//SPRING//DTD BEAN//EN'
    'http://www.springframework.org/dtd/spring-beans.dtd'>

<beans>

    <!--
    Define the model factory used to generate object models
    suitable for use with freemarker templates.
    -->
    <bean id="templateActionModelFactory" class=
"org.alfresco.repo.action.scheduled.FreeMarkerWithLuceneExtensionsModelFactory">
        <property name="serviceRegistry">
            <ref bean="ServiceRegistry"/>
        </property>
    </bean>
    <!--
    Example Chapter 6 : Action Definition
    Action is to execute the "Company Home > Data Dictionary >
     Scripts > chapter6_archive_expired_content.js" script
    -->

    <bean id="chapter6_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:chapter6_archive_expired_content.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>


    <!--
    Example Chapter 6 : The query and scheduler definition 
    Query     - No specific query is used
    Scheduler - Run the script for every 15 minutes
    Action    - Call chapter6_runScriptAction defined above
    -->

    <bean id="chapter6_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/15 * * * ?</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="chapter6_runScriptAction"/>
           <!-- This is name of the action (bean) that gets run -->
        </property>
        <property name="transactionService">
            <ref bean="TransactionService"/>
        </property>
        <property name="runAsUser">
            <value>System</value>
        </property>
    </bean>

</beans>

Restart the Alfresco server to ensure that the configuration changes are effective. Now go to the Company Home > Intranet > Marketing Communications > Website Documents > Staging space, and add few sample documents. For each document in the Staging space, update the Effective From date property in such a way that these documents will be moved to the Production space at the specified time. Note that the custom JavaScript executes every fifteen minutes to move the effective documents to the Production space.

XML configuration file for scheduled actions

The scheduled-action-services-context.xml file has two blocks of XML configuration.

The first block, which starts with <bean id="chapter6_runScriptAction", defines the action. This is where you specified the custom JavaScript to be executed. The important things to consider are as follows:

  • actionName: The name of the action (the bean name for the implementation).
  • parameterTemplates: A map of names and value templates. These are action specific.

The second block, which starts with <bean id="chapter6_runScript", contains the query and scheduler definitions. This is where you specify the time interval to execute custom JavaScript every 15 minutes. The important things to consider are as follows:

  • transactionMode: The transaction mode to be used
    • ISOLATED_TRANSACTIONS: For each node, the action is run as an isolated transaction. Failures are logged.
    • UNTIL_FIRST_FAILURE: For each node, the action is run as an isolated transaction. The first failure stops execution.
    • ONE_TRANSACTION: The actions for all of the nodes are run in one transaction. One failure will roll back all executed transactions.
  • queryLanguage: The query language to be used.
  • stores: A list of stores to query (currently only one store is supported).
  • queryTemplate: The template string used to build the query.
  • cronExpression: The cron expression that defines when the query runs.
  • jobName: The name of the scheduled job.
  • jobGroup: The group for the scheduled job.
  • triggerName: The name of the trigger.
  • triggerGroup: The group for the trigger.
  • runAsUser: The user under whose identity the action will run.
  • templateActionDefinition: The bean that defines the action.

The cron expression

A cron expression consists of six or seven text fields that are separated by white space.

Field Name

Position

Mandatory

Allowed Values

Special Characters

Seconds

1

Yes

0-59

, - * /

Minutes

2

Yes

0-59

, - * /

Hours

3

Yes

0-23

, - * /

Day of Month

4

Yes

1-31

, - * ? / L W

Month

5

Yes

1-12 or JAN-DEC

, - * /

Day of Week

6

Yes

1-7 or SUN-SAT

, - * ? / L #

Year

7

No

empty, 1970-2099

, - * /

An explanation of the special characters is provided in the following table:

*

All values.

?

No specific value.

-

This is used to specify a range. 1-5 in the day of the week field would mean, "on days 1, 2, 3, 4 and 5". 0-11 in the hour's field would mean, "each hour in the morning".

,

A list of values. In the minutes field, 0,15,30,45 would mean "when the minute is 0, 15, 30 OR 45". In the day field, MON, TUES would mean "on Mondays and Tuesdays".

/

When used after a value, this specifies increments. In the minutes field, 0/15 is equivalent to 0,15,30,45; */15 is equivalent to */15; and 10/15 is equivalent to 10, 25, 40, 55. In the day of the month field 3/7 means "every seven days starting on the third of the month" which is; 3, 10, 17, 24, ...

L

Last.

W

The nearest week day.

LW

The last week day of the month.

#

The nth day of the week.

Here are a few examples for you to your reference:

Daily run at 23:59:00. That is, run the action a minute before midnight each day:

0 59 23 * * ? 

Run every hour from 8 AM to 3 PM on working days:

0 0 8-15 ? * MON-FRI 

Run every 15 minutes for the first 10 days of the month:

0 0/15 * 1-10 * ? 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset