jcrontab学习理解笔记

来源:互联网 发布:华为acl应用到端口 编辑:程序博客网 时间:2024/06/02 05:34

最近Web项目需要后台,去网上了解了下,选择了jcrontab。关于jcrontab的背景和其他方面就不多说了,进入主题。下面用我自己的思路来整合并了解jcrontab,jcrontab支持不同的数据源:

  •  普通文件
  •  数据库
  •  XML文件

因为我的项目需要用到的是数据库,所以下面以数据库为例子讲解。


首先,我的目的是Web项目整合jcrontab。jcrontab自己是一个后台,是独立运行的,就像一个一个经理级别的线程管理所有的任务一样,但是这个经理线程也是需要启动,并且是随着web容器的运行而启动,web项目的配置一般都放在web.xml(框架类虽然有自己的配置文件,但是启动类都是在web.xml里面配置)。所以,jcrontab的启动类也需要在web.xml里面配置。


在web.xml配置下面的代码

<servlet>      <servlet-name>LoadOnStartupServlet</servlet-name>      <servlet-class>org.jcrontab.web.loadCrontabServlet</servlet-class>      <init-param>          <param-name>PROPERTIES_FILE</param-name>          <param-value>D:/jcrontab.properties</param-value>      </init-param>      <load-on-startup>1</load-on-startup>  </servlet>  <servlet-mapping>      <servlet-name>LoadOnStartupServlet</servlet-name>      <url-pattern>/Startup</url-pattern>  </servlet-mapping>


下面解释这里面的元素,org.jcrontab.web.loadCrontabServlet是jcrontab的一个servlet,用来启动管理线程的

public class loadCrontabServlet extends HttpServlet {......}


load-on-start 大家看名字就知道含义了,web容器启动的时候就加载此类,加载的时候肯定会实例化。因为loadCrontabServlet是servlet,肯定会继承Servlet的方法,如下

public void init(ServletConfig config) throws ServletException {super.init(config);try {System.out.print("Working?...");process();System.out.println("OK");} catch (Exception e) {throw new ServletException(e);}}
实例化启动类的时候,会调用Servlet的init()方法。


这里的PROPERTIES_FILE是jcrontab的配置文件的位置,在这个配置文件里面配置项目使用的哪一种方式来记录日程

public void process() {String propz = "jcrontab.properties";String props = getServletConfig().getInitParameter("PROPERTIES_FILE");if (props == null)props = propz;Properties propObj = new Properties();try {InputStream input = createPropertiesStream(props);propObj.load(input);} catch (IOException ioe) {ioe.printStackTrace();}                //省略部分代码}



然后下面来配置jcrontab.properties,如下

org.jcrontab.data.datasource = org.jcrontab.data.GenericSQLSourceorg.jcrontab.data.GenericSQLSource.driver = org.gjt.mm.mysql.Driverorg.jcrontab.data.GenericSQLSource.url = jdbc:mysql://127.0.0.1/blog_dborg.jcrontab.data.GenericSQLSource.username = rootorg.jcrontab.data.GenericSQLSource.password = root

相信大家对后面四个都容易懂,经典的数据库连接,对于第一个键值对,我贴出代码,相信大家就懂了

private DataFactory() {if (dao == null)try {dao = ((DataSource) Class.forName(Crontab.getInstance().getProperty("org.jcrontab.data.datasource")).newInstance()).getInstance();} catch (Exception e) {Log.error(e.toString(), e);}}


jcrontab通过jcrontab.properties文件来拿到存储的类,也就是通过配置文件拿到类的路径,然后反射实例化。org.jcrontab.data.datasource就是指定使用哪个类来拿任务。


最后,项目是把任务的信息存在数据库的,我们还没有在数据设计表结构,由配置文件可以知道,对数据库的操作的类是org.jcrontab.data.GenericSQLSource,代码如下

public static String queryAll = "SELECT id, second, minute, hour, dayofmonth,  month, dayofweek,  year, task, extrainfo, businessDays  FROM events";public static String querySearching = "SELECT id, second, minute, hour,  dayofmonth, month, dayofweek,  year, task, extrainfo, businessDays  FROM events WHERE task = ? ";public static String queryStoring = "INSERT INTO events( id, second, minute, hour, dayofmonth, month, dayofweek, year,  task, extrainfo, businessDays)  VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";public static String queryRemoving = "DELETE FROM events WHERE  id = ? ";public static String nextSequence = "SELECT MAX(id) id FROM EVENTS ";

看到这段SQL,也就知道每个字段的含义,jcrontab的名字设计得很好,一看就知道是什么意思了,整理一下SQL


SELECT id,        second,        minute,        hour,         dayofmonth,        month,        dayofweek,         year,        task,        extrainfo,        businessDays  FROM events WHERE task = ? 


整合jcrontab就先告一段落。




0 0
原创粉丝点击