java – Jetty中的计划任务

我想编写一个简单的Groovlet,它定期运行一个任务并使用Jetty容器.完成此任务的最简单方法是什么?我认为应该使用Quartz,但我不确定它是如何与Jetty集成的.我是否需要创建一个控制面板来启动和停止任务?有什么简单的例子我可以看一下开始吗?

解决方法:

您必须创建作业配置文件“jobconf.xml”或属性文件,其中必须配置作业.必须将此文件添加到应用程序的类路径或jetty中.

您必须将QuartzInitializer添加到您的web.xml和servlet参数中,如下所示:

<web-app>
<servlet>
<servlet-name>QuartzInitializer</servlet-name>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>config-file</param-name>
<param-value>quartz.properties</param-value>
</init-param>
<init-param>
<param-name>shutdown-on-unload</param-name>
<param-value>true</param-value>
</init-param>

<init-param>
<param-name>start-scheduler-on-load</param-name>
<param-value>true</param-value>
</init-param>

</servlet>

<servlet>
<servlet-name>first</servlet-name>
<servlet-class>com.v2sol.StartQuartz</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>first</servlet-name>
<url-pattern>/fst</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>one</servlet-name>
<servlet-class>com.v2sol.ExcelDBServlet</servlet-class>
<init-param>
<param-name>cronExpr</param-name>
<param-value>0,30 * * ? * MON-FRI</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/excel</url-pattern>
</servlet-mapping>


</web-app>

和servlet中的使用调度程序:

System.out.println("Initializing Scheduler PlugIn for Jobs!");
super.init(config);
ServletContext ctx = config.getServletContext();
Scheduler scheduler = null;
StdSchedulerFactory factory = (StdSchedulerFactory) 
ctx.getAttribute(QuartzInitializerServlet.QUARTZ_FACTORY_KEY);  

try {   
scheduler = factory.getScheduler();
JobDetail jd = new JobDetail("job1", "group1",ExcelJob.class);  
CronTrigger cronTrigger = new CronTrigger("trigger1","group1");
String cronExpr = null;
cronExpr = getInitParameter("cronExpr");
System.out.println(cronExpr);   
cronTrigger.setCronExpression(cronExpr);
scheduler.scheduleJob(jd, cronTrigger);
System.out.println("Job scheduled now ..");

} catch (Exception e){
e.printStackTrace();
}
上一篇:spring – 如何在Grails电子邮件服务中定制“从地址”


下一篇:android – Grails save()域对象实际上是一个Select吗?