JCrontab的一个简单的案例
2013-06-13 15:43
281 查看
JCrontab的一个简单的案例
最近学了一下JCrontab,一下是关于JCrontab的一个简单实例,供大家学习参考,如果有错误还请大家及时指出,谢谢大家。
1、普通文件持久化
1. 首先要下载JCrontab的相关jar包,Jcrontab-2.0-RC0.jar。
2. 在web.xml文件中配置JCrontab
3. jcrontab.properties
注:想知道更多的配置信息,可以参考JCrontab jar包中的jcrontab.properties
4. crontab文件(描述了任务的调度安排)
注:想知道更多的配置信息,可以参考JCrontab jar包中的crontab文件
5下面是CrontabMainTest和CrontabRunTest的代码
2、xml文件持久化
1.导入相关的jar包:Jcrontab-2.0-RC0.jar,xerces.jar
2.和上面“普通文件持久化”的2 相同
3.Jcrontab.properties
5. crontab.xml配置文件(描述了任务的调度安排)
每五分钟执行一次Crontab1的run方法
6 Crontab1的代码
3、配置文件中的绝对路径改成相对路径
以上两种配置都用的是Jcrontab-2.0-RC0.jar包中的org.jcrontab.web.loadCrontabServlet,配置文件都用的是绝对路径,这样每次一定项目到别的目录是就要更改配置文件,因此我们可以自定义一个servlet来替换org.jcrontab.web.loadCrontabServlet加载JCrontab(方法就是仿照loadCrontabServlet)。
复制源代码,并在添加红色框中的代码,如下图:
相对路径加载properties文件的代码
String propz = "/WEB-INF/jcrontab.properties";
/*String path = getServletConfig().getServletContext().getRealPath(".");
System.out.println("Real Path: ******" + path);*/
String props = getServletConfig().getInitParameter("PROPERTIES_FILE");
if (props == null) {
props = propz;
}
props = this.getServletContext().getRealPath(props);
相对路径加载properties文件里指定的路径的代码
datafile = this.getServletContext().getRealPath(datafile);
propObj.setProperty("org.jcrontab.data.file", datafile);
dd = propObj.getProperty("org.jcrontab.data.file");
这是LoadCrontabServlet的员代码,供大家参考
最近学了一下JCrontab,一下是关于JCrontab的一个简单实例,供大家学习参考,如果有错误还请大家及时指出,谢谢大家。
1、普通文件持久化
1. 首先要下载JCrontab的相关jar包,Jcrontab-2.0-RC0.jar。
2. 在web.xml文件中配置JCrontab
<servlet> <servlet-name>LoadOnStartupServlet</servlet-name> <servlet-class>org.jcrontab.web.loadCrontabServlet</servlet-class> <init-param> <param-name>PROPERTIES_FILE</param-name> <!--此处路径是绝对路径 --> <param-value>C:\Users\pc\Workspaces\MyEclipse 8.5\jcrontab\src\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> |
#指定crontab文件的地址 org.jcrontab.data.file =C:/Users/pc/Workspaces/MyEclipse8.5/jcrontab/src/crontab #配置持久化文件为org.jcrontab.data.FileSource,也就是普通文件 org.jcrontab.data.datasource = org.jcrontab.data.FileSource |
4. crontab文件(描述了任务的调度安排)
# Tasks planification configuration file. # IMPORTANT: All the index begin in 0, except day of month and # month that begin in 1. # minute 0-59 # hour 0-23 # day of month 1-31 # month 1-12 # day of week 0-6 (0 is Sunday) #每两分钟执行一次 CrontabMainTest的 main方法 */2 * * * * com.jrontab.crontab.CrontabMainTest #每一分钟执行一次 CrontabRunTest的 run方法,并传入参数Hello World * * * * * com.jrontab.crontab.CrontabRunTest#run Hello World |
5下面是CrontabMainTest和CrontabRunTest的代码
public class CrontabMainTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("hello world from main()"); } } public class CrontabRunTest implements Runnable { private static String[] args; public CrontabRunTest(String[] args) { this.args=args; } public void run() { if(args.length>0&&args!=null){ for(String str:args){ System.out.println(str+"---->"); } } } } |
1.导入相关的jar包:Jcrontab-2.0-RC0.jar,xerces.jar
2.和上面“普通文件持久化”的2 相同
3.Jcrontab.properties
#指定xml配置文件的地址(绝对路径) org.jcrontab.data.file=C:/Users/pc/Workspaces/MyEclipse8.5/jcrontab/WebRoot/WEB-INF/crontab.xml #配置xml解析器 org.xml.sax.driver=org.apache.xerces.parsers.SAXParser #指定持久化文件为xml文件 org.jcrontab.data.datasource=org.jcrontab.data.XMLSource |
每五分钟执行一次Crontab1的run方法
<?xmlversion="1.0"encoding="utf-8"?> <crontab> <crontabentryid="2014"> <seconds>0,5,10,15,20,25,30,35,40,45,50,55</seconds> <minutes>*</minutes> <hours>*</hours> <daysofmonth>*</daysofmonth> <months>*</months> <daysofweek>*</daysofweek> <years>*</years> <bussinesdays>true</bussinesdays> <startDate></startDate> <endDate></endDate> <class>com.jrontab.crontab.Crontab1</class> <method>run</method> <parameters></parameters> <description></description> </crontabentry> </crontab> |
public class Crontab1 { public static void run(String[] args) { System.out.println("hello world!!!"); } } |
以上两种配置都用的是Jcrontab-2.0-RC0.jar包中的org.jcrontab.web.loadCrontabServlet,配置文件都用的是绝对路径,这样每次一定项目到别的目录是就要更改配置文件,因此我们可以自定义一个servlet来替换org.jcrontab.web.loadCrontabServlet加载JCrontab(方法就是仿照loadCrontabServlet)。
复制源代码,并在添加红色框中的代码,如下图:
相对路径加载properties文件的代码
String propz = "/WEB-INF/jcrontab.properties";
/*String path = getServletConfig().getServletContext().getRealPath(".");
System.out.println("Real Path: ******" + path);*/
String props = getServletConfig().getInitParameter("PROPERTIES_FILE");
if (props == null) {
props = propz;
}
props = this.getServletContext().getRealPath(props);
相对路径加载properties文件里指定的路径的代码
datafile = this.getServletContext().getRealPath(datafile);
propObj.setProperty("org.jcrontab.data.file", datafile);
dd = propObj.getProperty("org.jcrontab.data.file");
这是LoadCrontabServlet的员代码,供大家参考
public class loadCrontabServlet extends HttpServlet { private Crontabcrontab =null; /** Refer to Servlet Javadoc * This method is invoked by the Servlet container * When the app-server starts. * @param config The ServletConfig */ 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); } } protected InputStream createPropertiesStream(String name) throws IOException { return new FileInputStream(name); } /** * This method starts the Crontab and lets the system * Continue without wasting more resources. * This method can receive the config File as a variable in web.xml */ public void process() { String propz = "jcrontab.properties"; //String path = getServletConfig().getServletContext() // .getRealPath("."); //System.out.println("Real Path: " + path); String props = getServletConfig() .getInitParameter("PROPERTIES_FILE"); if (props ==null) { props = propz; } // Load the servlet config parameters // and override the properties Properties propObj = new Properties(); try { InputStream input = createPropertiesStream(props); propObj.load(input); } catch (IOException ioe) { ioe.printStackTrace(); } ServletConfig c = getServletConfig(); Enumeration keys = c.getInitParameterNames(); while (keys.hasMoreElements()) { String key = (String) keys.nextElement(); propObj.setProperty(key, c.getInitParameter(key)); } crontab = Crontab.getInstance(); try { ShutdownHook(); crontab.init(propObj); } catch (Exception e) { Log.error(e.toString(), e); } } /** * This method seths a ShutdownHook to the system * This traps the CTRL+C or kill signal and shutdows * Correctly the system. * @throws Exception */ public void ShutdownHook() throws Exception { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { doStop(); } }); } public void destroy() { doStop(); } public void doStop() { Log.info("Shutting down..."); // stops the system in 100 miliseconds :-) crontab.uninit(100); Log.info("Stoped"); } } } catch (Exception e) { Log.error(e.toString(), e); } } /** * This method seths a ShutdownHook to the system * This traps the CTRL+C or kill signal and shutdows * Correctly the system. * @throws Exception */ public void ShutdownHook() throws Exception { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { doStop(); } }); } public void destroy() { doStop(); } public void doStop() { Log.info("Shutting down..."); // stops the system in 100 miliseconds :-) crontab.uninit(100); Log.info("Stoped"); } } 此时可以配置web.xml中的servlet为 <servlet-class>com.jrontab.servlet.JcrontabServlet</servlet-class> 此案例的代码已上传csdn,下载地址:git@code.csdn.net:jijijiujiu123/jcrontab.git |
相关文章推荐
- JCrontab的一个简单的案例
- JCrontab的一个简单的案例
- 一个简单创业案例带来的启示
- 一个简单创业案例带来的启示
- 一个案例教你简单地玩转ViewPager(三)之带指示点的图片自动轮播效果
- 一个简单的通过上下左右键移动div的案例--可用于游戏的用户操作界面
- 3小时搞定一个简单的MIS系统案例Northwind,有视频、有源代码下载、有真相
- 《植物大战僵尸》一个简单的创新案例
- 适合selenium rc初学者的一个简单java测试案例
- [第八季]1.Java Web开发概述开发环境及一个简单案例(一)
- JQuery概述和一个简单案例
- 一个简单的动态内表alv案例
- flume 简单案例 将一个节点的中一个日志文件的动态变化 在 另一个节点的终端输出
- 一个简单创业案例带来的启示
- 一个简单轮播案例
- 一个基于TensorFlow的简单故事生成案例:带你了解LSTM
- 一个简单的java文件反编译案例
- 源代码:使用Antlr+Stringtemplate生成method chaining,一个不太简单的案例
- 3ff0 一个简单的执行程序的GNU automake自动生成Makefile的方法及案例
- 【Node.js-8】一个简单的登录案例