Creating your own Crontab job
Application support jobs written in BeanShell or classes into JAR file. We recommend the use JAR files instead of BeanShell.
Understanding the crontab job execution context
Section titled “Understanding the crontab job execution context”Execution context:
- Crontab job is executed by internall users named “system” ( the job has not a valid user session ).
- Crontab job is executed by administrator user ( the job has a valid user session ).
Use the system token is a good practice when calling API methods into crontab task:
String systemToken = DbSessionManager.getInstance().getSystemsystemToken();OKMFolder.getInstance().createSimple(systemToken, "/okm:root/test")Crontab job based on JAR file
Section titled “Crontab job based on JAR file”Requisits:
- Must implement the class “public static void main(String[] args)”.
- Must implement the class “public static String cronTask(String systemToken)”.
package com.openkm;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import com.openkm.module.db.stuff.DbSessionManager;
public class CronTabTest { private static Logger log = LoggerFactory.getLogger(CronTabTest.class);
public static void main(String[] args) { System.out.println(cronTask(DbSessionManager.getInstance().getSystemToken())); }
public static String cronTask(String systemToken) { return "mail body here"; }}To create a JAR file follow the instructions Creating a JAR file with Eclipse.
Contab job based on BeanShell
Section titled “Contab job based on BeanShell”Example import files from /home/scanner folder to /okm:root/Scans
import com.openkm.module.db.stuff.*;import com.openkm.core.*;import com.openkm.api.OKMDocument;import java.io.*;import com.openkm.util.FileLogger;
String systemToken = DbSessionManager.getInstance().getSystemsystemToken();OKMDocument document = OKMDocument.getInstance();
public class OnlyExt implements FilenameFilter { String ext;
public OnlyExt(String ext) { this.ext = "." + ext; }
public boolean accept(File dir, String name) { return name.endsWith(ext); }}
File scans = new File("/home/scanner");Thread.sleep(10000); // Sleep 10 seconds in case files are still being written
try { for (File scan : scans.listFiles(new OnlyExt("pdf"))) { try { document.createSimple(systemToken, "/okm:root/Scans/" + scan.getName(), new FileInputStream(scan)); scan.delete(); } catch (Exception e) { FileLogger.error("CrontabSampleError", e.getMessage()); } }
} catch (Exception e) { FileLogger.error("CrontabSampleError", e.getMessage());}