最近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>
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()方法。
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();
}
//省略部分代码
}
org.jcrontab.data.datasource = org.jcrontab.data.GenericSQLSource
org.jcrontab.data.GenericSQLSource.driver = org.gjt.mm.mysql.Driver
org.jcrontab.data.GenericSQLSource.url = jdbc:mysql://127.0.0.1/blog_db
org.jcrontab.data.GenericSQLSource.username = root
org.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);
}
}
最后,项目是把任务的信息存在数据库的,我们还没有在数据设计表结构,由配置文件可以知道,对数据库的操作的类是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就先告一段落。