CronTab samples
Basics
Section titled “Basics”Suggested code sample
Section titled “Suggested code sample”First, you must create the web service object:
OKMWebservices ws = OKMWebservicesFactory.newInstance(host);Then you should log in using the “login” method. You can access the “login” method from the web service object “ws” as shown below:
ws.login(user, password);At this point, you can use all the CronTab methods from the “cronTab” class as shown below:
List<CronTab> cronTabs = ws.cronTab.findAll();Methods
Section titled “Methods”findAll
Section titled “findAll”Description:
| Method | Return values | Description |
|---|---|---|
| findAll() | List |
Returns a list of all CronTab entries registered in the system. |
Each CronTab object in the returned list contains the following properties:
- id: Unique identifier of the cron tab.
- name: Name of the cron tab.
- expression: Cron expression that defines the execution schedule.
- className: Java class name associated with the cron task.
- active: Indicates whether the cron tab is currently enabled.
- lastBegin: Date and time of the last execution start.
- lastEnd: Date and time of the last execution end.
- error: Last error message, if any.
Example:
using System;using System.Collections.Generic;using com.openkm.sdk4csharp;using com.openkm.sdk4csharp.bean;
namespace OKMRest{ public class Program { static void Main(string[] args) { String host = "http://localhost:8080/openkm"; String user = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host);
try { ws.login(user, password); List<CronTab> cronTabs = ws.cronTab.findAll(); foreach (CronTab ct in cronTabs) { System.Console.WriteLine(ct.toString()); } } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } }}update
Section titled “update”Description:
| Method | Return values | Description |
|---|---|---|
| update(long id, String name, String expression, bool active) | void | Updates the configuration of an existing cron tab. |
The value of id is the unique identifier of the cron tab to be updated.
The value of name is the new name to assign to the cron tab.
The value of expression is the new cron expression that defines the execution schedule.
The value of active determines whether the cron tab should be enabled (true) or disabled (false).
Example:
using System;using System.Collections.Generic;using com.openkm.sdk4csharp;using com.openkm.sdk4csharp.bean;
namespace OKMRest{ public class Program { static void Main(string[] args) { String host = "http://localhost:8080/openkm"; String user = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host);
try { ws.login(user, password); List<CronTab> cronTabs = ws.cronTab.findAll(); CronTab ct = cronTabs.Find(c => c.name == "Purge Activity Log"); // Disable the cron tab ws.cronTab.update(ct.id, ct.name, ct.expression, false); // Re-enable the cron tab ws.cronTab.update(ct.id, ct.name, ct.expression, true); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } }}Description:
| Method | Return values | Description |
|---|---|---|
| reset(long id) | void | Resets the execution state of a cron tab, clearing its last execution data. |
The value of id is the unique identifier of the cron tab to be reset.
Example:
using System;using System.Collections.Generic;using com.openkm.sdk4csharp;using com.openkm.sdk4csharp.bean;
namespace OKMRest{ public class Program { static void Main(string[] args) { String host = "http://localhost:8080/openkm"; String user = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host);
try { ws.login(user, password); List<CronTab> cronTabs = ws.cronTab.findAll(); CronTab ct = cronTabs.Find(c => c.name == "Purge Activity Log"); ws.cronTab.reset(ct.id); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } }}execute
Section titled “execute”Description:
| Method | Return values | Description |
|---|---|---|
| execute(long id) | void | Triggers the immediate execution of a cron tab, regardless of its scheduled expression. |
The value of id is the unique identifier of the cron tab to be executed.
Example:
using System;using System.Collections.Generic;using System.Threading;using com.openkm.sdk4csharp;using com.openkm.sdk4csharp.bean;
namespace OKMRest{ public class Program { static void Main(string[] args) { String host = "http://localhost:8080/openkm"; String user = "okmAdmin"; String password = "admin"; OKMWebservices ws = OKMWebservicesFactory.newInstance(host);
try { ws.login(user, password); List<CronTab> cronTabs = ws.cronTab.findAll(); CronTab ct = cronTabs.Find(c => c.name == "Purge Activity Log"); ws.cronTab.execute(ct.id); // Wait for execution to complete Thread.Sleep(5000); cronTabs = ws.cronTab.findAll(); CronTab executed = cronTabs.Find(c => c.name == "Purge Activity Log"); System.Console.WriteLine("Last execution ended: " + executed.lastEnd); } catch (Exception e) { System.Console.WriteLine(e.ToString()); } } }}