Java – scheduled tasks in jetty

I want to write a simple groovlet that runs a task periodically and uses the jetty container What is the easiest way to accomplish this task? I think quartz should be used, but I'm not sure how it integrates with jetty Do I need to create a control panel to start and stop tasks? Is there any simple example I can take a look at?

Solution

You must create a job configuration file "jobconf. XML" or a property file, where the job must be configured You must add this file to your application's classpath or jetty

You must add quartzinitializer to your web XML and servlet parameters, as follows:

<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>

Usage scheduler in and 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();
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>