Use declarative handling of software configurations as much as possible, followed by programmatic approaches. In the previous article, "Quartz Framework QuickStart (i)", if we were to change the execution time and frequency after the Job started, we had to modify the source code to recompile. This approach is only applicable to small example programs, but for a large and complex system, this becomes a problem. So, if you can deploy the quart Job declaratively, and it is also required, you should choose this way every time.
· Configuration quartz.properties file
The file quartz.properties defines the runtime behavior of the quartz application and includes many properties that control the operation of the quartz. This file should be placed in the path referred to by classpath, such as our Java project, and put it together with the jobs.xml described below in the project root directory is. If you're not sure, check out the. classpath file, which is configured with the classpath of your project.
Let's take a look at the most basic quartz.properties file and discuss some of these settings. Here is a trimmed version of the Quartz.propertis file
We added the property org.quartz.plugin.jobInitializer.fileName and set the property value to the file name we want. This file name should be visible to ClassLoader, that is, under Classpath.
When Quartz is started, read the Quartz.properties file, and then initialize the plug-in. It will pass all the properties configured above to the plugin, and the plugin will be notified to search for different files.
The following is the directory scan example of the JOB definition XML file. As shown in the previous example, here we use a declarative approach to configure the Job and Trigger information
The format of <start-time> in Jobs.xml is: where T separates the date and time, the default time zone
Or: Where +08:00 means East eight < Start-time > 2008-09-03t14:43:00 </start-time >
< Start-time > 2008-09-03t14:43:00+08:00 </start-time >
The <job> element describes a job to register with the scheduler, which is equivalent to using the Schedulejob () method in the previous section. The two elements that you see in <job-detail> and <trigger> are the parameters that we pass to the method Schedulerjob () in the code. The front is essentially the same as here, but now is a more popular way of declaring it. The <trigger> element is also very intuitive: it uses the same properties as before, but it's easier to build a simpletrigger. So it's just a different (arguably and better) way of doing the same thing in the last piece of code. Obviously, you can also support more than one Job. In the previous code, we programmed the way we do, and we can support it in a declarative way. Finally, let's look at how the original code was simplified:
<? XML version= ' 1.0 ' encoding= ' utf-8 '?>
< quartz xmlns = "http://www.opensymphony.com/quartz/JobSchedulingData"
Xmlns:xsi = "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation = "Http://www.opensymphony.com/quartz/JobSchedulingData
Http://www.opensymphony.com/quartz/xml/job_scheduling_data_1_5.xsd "
Version = "1.5" >
< job >
< Job-detail >
< name > ScanDirectory1 </name >
< group > DEFAULT </Group >
< description >
A job that scans a directory for files
</Description >
< Job-class >
Com.vista.quartz.ScanDirectoryJob
</Job-class >
< volatility > False </volatility >
< durability > False </Durability >
< recover > False </recover >
< Job-data-map Allows-transient-data = "true" >
< entry >
< key > Scan_dir </key >
< value > d:/dyk/java/tomcat/conf </value >
</Entry >
</Job-data-map >
</job-detail >
< trigger >
< simple >
< name > scanTrigger1 </name >
< group > DEFAULT </Group >
< Job-name > ScanDirectory1 </job-name >
< Job-group > DEFAULT </job-group >
< Start-time > 2008-09-03t15:00:10 </start-time >
<!--repeat indefinitely every seconds--
< Repeat-count >-1 </repeat-count >
< Repeat-interval > 10000 </repeat-interval >
</Simple >
</Trigger >
</Job >
< job >
< Job-detail >
< name > ScanDirectory2 </name >
< group > DEFAULT </Group >
< description >
A job that scans a directory for files
</Description >
< Job-class >
Com.vista.quartz.ScanDirectoryJob
</Job-class >
< volatility > False </volatility >
< durability > False </Durability >
< recover > False </recover >
< Job-data-map Allows-transient-data = "true" >
< entry >
< key > Scan_dir </key >
< value > D:/dyk/java/tomcat/webapps/mytest/web-inf </value >
</Entry >
</Job-data-map >
</job-detail >
< trigger >
< simple >
< name > scanTrigger2 </name >
< group > DEFAULT </Group >
< Job-name > ScanDirectory2 </job-name >
< Job-group > DEFAULT </job-group >
< Start-time > 2008-09-03t15:00:20 </start-time >
<!--repeat indefinitely every-seconds
< Repeat-count >-1 </repeat-count >
< Repeat-interval > 15000 </repeat-interval >
</Simple >
</Trigger >
</Job >
</Quartz >
Package Com.vista.quartz;
Import Java.util.Date; &nb