Creating your own Crontab plugin
You can create your own Crontab plugin.
Conditions:
- The new Crontab plugin class must implement the “CronAdapter” interface.
- The new Crontab plugin class must be declared under the package “com.openkm.plugin.cron”.
- The new Crontab plugin class must be annotated with “@PluginImplementation”.
- The new Crontab plugin class must extend “BaseCronPlugin”.
CronAdapter interface:
package com.openkm.plugin.cron;
import net.xeoh.plugins.base.Plugin;
public interface CronAdapter extends Runnable, Plugin { /** * The cron name */ String getName();
/** * The default cron expression what will be registered the first time */ String getCronExpression();
/** * Return if task is running or not */ boolean isRunning();
/** * Cron task execution */ void execute() throws Throwable;}The new class must be loaded into the package com.openkm.plugin.cron because the application plugin system will try to load it from there.
Method description
Section titled “Method description”| Method | Type | Description |
|---|---|---|
| getName() | String | Returns the name of the crontab. |
| getCronExpression | String | Returns the cron expression. |
| isRunning | Boolean | Returns true when the cron task is running. |
| execute | Void | Executes the cron task. |
Example of the disposition cron implementation
Section titled “Example of the disposition cron implementation”package com.openkm.plugin.cron;
import com.openkm.api.OKMRepository;import com.openkm.bean.Folder;import com.openkm.module.db.stuff.DbSessionManager;import com.openkm.util.FileLogger;import net.xeoh.plugins.base.annotations.PluginImplementation;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;
@PluginImplementationpublic class TestCron extends BaseCronPlugin implements CronAdapter { private static Logger log = LoggerFactory.getLogger(TestCron.class); private static String LOG_NAME = "TestCron";
@Autowired private OKMRepository okmRepository;
@Override public String getName() { return "Cron test"; }
@Override public String getCronExpression() { return "0 * * * * *"; }
@Override public void execute() { try { FileLogger.info(LOG_NAME, ">>> Started"); // Use always system token in your cron String token = DbSessionManager.getInstance().getSystemToken(); Folder rootFolder = okmRepository.getRootFolder(token); FileLogger.info(LOG_NAME, "Root folder uuid: " + rootFolder.getUuid()); FileLogger.info(LOG_NAME, ">>> Finished"); } catch (Exception e) { log.error(e.getMessage()); } }}Understanding the crontab job execution context
Section titled “Understanding the crontab job execution context”Execution context:
- Crontab job is executed by internal users named “system” ( the job does not have a valid user session ).
- Crontab job is executed by the administrator user ( the job has a valid user session ).
It is good practice to use the system token when calling API methods in a crontab task:
String systemToken = DbSessionManager.getInstance().getSystemToken();OKMFolder.getInstance().createSimple(systemToken, "/okm:root/test")