How to add a cron job
Question
What is the way to schedule a job in Jahia?
Answer
With Jahia 8.2
You can take as an example the following source code.
Before Jahia 8.2
First, you need to create your Job class that extends the QuartzJobBean
. Here is a basic example of Job that does nothing except displaying a message on debug mode. Note that we get some external data from the spring.
package package org.jahia.modules.mymodule.jobs;
import org.quartz.JobDataMap;
import org.slf4j.Logger;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.scheduling.quartz.QuartzJobBean;
import java.util.List;
public class MyJob extends QuartzJobBean {
private transient static Logger logger = org.slf4j.LoggerFactory.getLogger(MyJob.class);
/**
* Execute the actual job. The job data map will already have been
* applied as bean property values by execute. The contract is
* exactly the same as for the standard Quartz execute method.
*
* @see #execute
*/
@Override
protected void executeInternal(JobExecutionContext context) throws JobExecutionException {
logger.debug("Executing job here...");
// get the jobDataAsMap from spring file
JobDataMap mergedJobDataMap = context.getMergedJobDataMap();
String var1 = (String) mergedJobDataMap.get("var1");
logger.debug("var1=" + var1);
String var2 = (String) mergedJobDataMap.get("var2");
logger.debug("var2=" + var2);
String var3 = (String) mergedJobDataMap.get("var3");
logger.debug("var3=" + var3);
List<String> otherVarAsList = (List<String>) mergedJobDataMap.get("otherVarAsList");
logger.debug("otherVarAsList=" + otherVarAsList.toString());
}
}
And then you need to create an XML spring file in the resources/META-INF/spring
directory. In this example, some properties are set using the jobDataAsMap
property.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean parent="jobSchedulingBean">
<property name="ramJob" value="false"/>
<property name="trigger">
<bean class="org.quartz.CronTrigger">
<property name="name" value="ReferentielJobTrigger"/>
<!-- Will start the process at 4 a.m -->
<property name="cronExpression" value="${sitemapCronExpression:0 0 4 * * ?}"/>
</bean>
</property>
<property name="jobDetail">
<bean class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="name" value="myJob"/>
<property name="group" value="MyGroup"/>
<property name="jobClass" value="org.jahia.modules.mymodule.jobs.MyJob"/>
<property name="description" value="This is the description of the Job" />
<property name="jobDataAsMap">
<map>
<entry key="var1" value="value1" />
<entry key="var2" value="value2" />
<entry key="var3" value="value3" />
<entry key="otherVarAsList">
<list>
<value>item1</value>
<value>item2</value>
<value>item3</value>
</list>
</entry>
</map>
</property>
</bean>
</property>
</bean>
</beans>
Once it's deployed, you should be able to view the job on the Job Administration (from the tools /modules/tools/
jobadmin
.jsp
)
and the related details when we click on the info icon.
Note that this Job is a non ramJob (the value is set to false
(default value).
<property name="ramJob" value="false"/>
It means that this Job is persistent and is only executed on the processing server. If you want to schedule a job on each cluster nodes, then you will need to set the value to true
.
<property name="ramJob" value="true"/>