# 9.3 Using External Configuration Files

The previous example only showed the Java hard-coded configuration. In more application scenarios, external configuration files are used for flexible scheduling strategies, making it easier to change the scheduling strategy at any time. Below is an example code for external configuration:

cron4j=task1, task2
task1.cron=* * * * *
task1.class=com.xxx.TaskAaa
task1.daemon=true
task1.enable=true
 
task2.cron=* * * * *
task2.class=com.xxx.TaskBbb
task2.daemon=true
task2.enable=false
1
2
3
4
5
6
7
8
9
10

In the above example, cron4j is what's called the configuration name or configName. You can name it as you like. This name will be used when creating the Cron4jPlugin object. If a name is not provided when creating the Cron4jPlugin object, the default value is "cron4j".

The configName is immediately followed by task1 and task2, which represent the names of two tasks in the current configuration. These names specify that subsequent configurations will start with them, for example, task1.cron, task2.cron all start with these task names.

task1.cron specifies the cron expression for the task, task1.class specifies the target Java class for the task that needs to implement the Runnable interface, task1.daemon specifies whether the scheduled task thread is a daemon thread, and task1.enable specifies whether the task is enabled or disabled. This last configuration is optional and can be omitted; if omitted, it defaults to enabled. The configuration for task2 has the same meaning as for task1; only the task names differ.

In summary, configName specifies taskName, multiple taskNames can be separated by commas, and each taskName specifies a specific task. Each specific task has four configurations: cron, class, daemon, and enable, each configuration starts with taskName.

Assuming the configuration file name is config.txt, the creation of Cron4jPlugin after the configuration can be as follows:

cp = new Cron4jPlugin("config.txt");
cp = new Cron4jPlugin("config.txt", "cron4j");
 
cp = new Cron4jPlugin(PropKit.use("config.txt"));
cp = new Cron4jPlugin(PropKit.use("config.txt"), "cron4j");
 
me.add(cp);
1
2
3
4
5
6
7

In the code above, the first four lines are four ways to create a Cron4jPlugin object using a configuration file. The first line only passes in the configuration file name, omitting the configName, so the default value is "cron4j". The second line is essentially the same as the first, except that it specifies its configName. The third and fourth lines are similar to the first two, except that they use PropKit for loading.

Please note: The configName mentioned here is the "cron4j" in the example configuration item cron4j=task1, task2. This configName is essentially the entry point that Cron4jPlugin looks for in the configuration.

Last Updated: 9/22/2023, 7:21:10 AM