Scheduled Jobs in Jetty

I would like to write a simple Groovlet that runs the task periodically, and I use the Jetty container. What is the easiest way to complete 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? Are there any simple examples that I can look at to get started?

+3
source share
3 answers

You need to create the job configuration file "jobconf.xml" or the properties file in which the jobs should be configured. This file must be added to the application class path or pier.

QuartzInitializer web.xml , :

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

:

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();
}
+2

, Quartz, Timer TimerTask, JDK. , , .

, , , JMX bean, JMX .

+5

Create a Quartz Scheduler and place it in the application context. Any Groovlet will have access to it and will be able to send new tasks and manage running ones.

0
source

Source: https://habr.com/ru/post/1791259/


All Articles