Sometimes we have requirements where we need to do certain processes or set of operations on a regular basis. It could be archival of contents or processing of existing documents in Alfresco repository to generate some reports, or another case is where you had a bunch of documents dropped by users on a shared drive and you want to upload them into Alfresco every day automatically to manage them easily. The possibilities are endless. Alfresco uses this kind of scheduled job for content archival and purging. Alfresco use spring’s cron job capabilities to meet up these requirements.
We are going to discuss how we could create our own scheduled job in alfresco. Steps to create it are as follow.
Create custom-scheduled-job-context.xml where we define job detail and job trigger beans
<bean id="customJobDetail" class="org.springframework.scheduling.quartz.JobDetailBean"> <property name="jobClass"> <value>com.company.scheduled.job.CustomScheduledJob</value> </property> <property name="jobDataAsMap"> <map> <entry key="abstractAuthenticationService"> <ref bean="authenticationService" /> </entry> </map> </property> </bean> <bean id="customJobTrigger" class="org.alfresco.util.CronTriggerBean"> <property name="jobDetail"> <ref bean="customJobDetail"/> </property> <property name="scheduler"> <ref bean="schedulerFactory"/> </property> <property name="cronExpression"> <value>0 0 21 * * ?</value> </property> </bean>
Place it under
Here we are creating custom Scheduled Job called customJobDetail and associating it with jobtrigger called customJobTrigger.
In the jobdatamap you can pass alfresco services which will be required by your job to access repository.
cronExpression value in customJobTrigger defines the time interval at which this job will be executed(provided alfresco is up and running J)
Create job class CustomScheduledJob under package com.company.scheduled.job
Which extends java.lang.Object and
We need to implement execute method and under that business logic will reside.
Package that class in jar and place the jar under
Restart the server and your scheduled job will be executed on defined time intervals. You can define the cron expression based on your requirements. Refer this guide to know more about cron expressions.
Allowed special characters
|Minutes||Yes||0-59||* / , -||-|
|Hours||Yes||0-23||* / , -||-|
|Day of month||Yes||1-31||* / , - ? L W||-|
|Month||Yes||1-12 or JAN-DEC||* / , -||-|
|Day of week||Yes||0-6 or SUN-SAT||* / , - ? L #||-|
|Year||No||1970–2099||* / , -||This field is not supported in standard/default implementations.|
For debugging you need to add your job class in log4j.properties of alfresco. You will be able to see logs of your job run on interval you have mentioned in your context file.