ECM
ECM
总版主
总版主
  • 社区居民
  • 忠实会员
  • 原创写手
阅读:753回复:0

JSF 2 + Quartz scheduling library

楼主#
更多 发布于:2016-05-16 10:53
I have a web application thats using JSF 2. In this application I am using a charting library which is getting data from an xml file, the application updates the xml file, when someone accesses the site, because of jsf 2 Action. Now I want to implement the Quartz library the open source scheduling library, to update the xml file and not rely on the users action, but I have no idea how to call an Action from Quartz using JSF 2. Thanks in advance guys.
answer(解决方案):

Generally speaking , you should implement your scheduled logic , define when it will run , and initialize your scheduled jobs when the application server starts. Implement scheduled logic Your scheduled class should implement org.quartz.Job interface and override its execute() which contains the logic of your scheduled job. In your case , it is the method to update the XML file. You should make this method does not have any dependencies on JSF such that it can be called outside the JSF . public class MyScheduledJob implements Job {

   public void execute(JobExecutionContext context) throws JobExecutionException {
          updateXML();
   }
}
 Initialize and start Quartz Quartz provides a ServletContextListener called QuartzInitializerListener that allows you to initialize and start Quartz when the application server starts . Add this listener to your web.xml <listener>
     <listener-class> org.quartz.ee.servlet.QuartzInitializerListener</listener-class>
</listener>
 By default , it will look for a file called quartz.properties in the classpath to initialize Quartz . You can refer this for the more info about configurable options available in quartz.properties Define which Job will run at which time You can define it in a XML file (Its schema definition can be found here ) and configure XMLSchedulingDataProcessorPlugin in quartz.properties to load this XML when Quartz is initialized. For example , in the quartz.properties org.quartz.plugin.jobInitializer.class =org.quartz.plugins.xml.XMLSchedulingDataProcessorPlugin org.quartz.plugin.jobInitializer.fileNames = quartz-config.xml org.quartz.plugin.jobInitializer.failOnFileNotFound = true Then in the quartz-config.xml <?xml version="1.0" encoding="UTF-8"?>

    <job-scheduling-data
        xmlns="http://www.quartz-scheduler.org/xml/JobSchedulingData"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.quartz-scheduler.org/xml/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_8.xsd"
        version="1.8">

        <schedule>
            <job>
                <name>MyScheduledJob</name>
                <group>MyScheduledGroup</group>
                <description>Job to update XML </description>
                <job-class>com.xxxx.xxxx.xxxx.MyScheduledJob </job-class>
            </job>

            <trigger>
                    <cron>
                            <name>midNightTrigger</name>
                            <job-name>MyScheduledJob</job-name>
                            <job-group>MyScheduledGroup</job-group>
                            <!-- It will run every night at 3:30 am -->
                            <cron-expression>0 30 3 * * ?</cron-expression>  
                    </cron>
              </trigger>
        </schedule>
    </job-scheduling-data>
 All the above is for Quartz 'a latest version 2.1 . You can check out the sample codes and tutorials from Quartz for more info.

 

 

 

 

 

 

 

异常中心网是一家专门收集整理程序员编程过程中遇到的常见异常(exception)以及各种异常问答中心的网站。异常中心网旨在,减少程序员在编码遇到异常,处理各种异常时间和痛苦,让程序员能更愉快的、快速的定位异常并查找对应的异常解决方案。异常中心网诚心打造最完美的编程社区为程序员用户服务,努力成为最好的程序员乐园程序员社区程序异常中心程序bug中心异常问答中心

 

喜欢0 评分0
游客

返回顶部