Za periodicno izvrsavanje nekog posla mozete koristiti quartz scheduler
http://www.quartz-scheduler.org/ (http://www.quartz-scheduler.org/)
Ako pravite web aplikaciju potrebno je u web.xml dodati jedan servlet:
<servlet>
<display-name>Quartz Initializer Servlet</display-name>
<servlet-name>quartz</servlet-name>
<servlet-class>org.quartz.ee.servlet.QuartzInitializerServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
Servlet se konfigurise preko parametara navedenih u quartz.properties fajlu koji je potrebno da se nalazi u class pathu:
#====================================================================
# Configure Main Scheduler Properties
#====================================================================
org.quartz.scheduler.instanceName = TestScheduler
org.quartz.scheduler.instanceId = AUTO
#====================================================================
# Configure ThreadPool
#====================================================================
org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 3
org.quartz.threadPool.threadPriority = 5
#====================================================================
# Configure JobStore
#====================================================================
org.quartz.jobStore.misfireThreshold = 60000
org.quartz.jobStore.class = org.quartz.simpl.RAMJobStore
#org.quartz.jobStore.class = org.quartz.impl.jdbcjobstore.JobStoreTX
#org.quartz.jobStore.driverDelegateClass = org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
#org.quartz.jobStore.useProperties = false
#org.quartz.jobStore.dataSource = myDS
#org.quartz.jobStore.tablePrefix = QRTZ_
#org.quartz.jobStore.isClustered = false
#====================================================================
# Configure Datasources
#====================================================================
#org.quartz.dataSource.myDS.driver = org.postgresql.Driver
#org.quartz.dataSource.myDS.URL = jdbc:postgresql://localhost/dev
#org.quartz.dataSource.myDS.user = jhouse
#org.quartz.dataSource.myDS.password =
#org.quartz.dataSource.myDS.maxConnections = 5
#====================================================================
# Configure Plugins
#====================================================================
org.quartz.plugin.triggHistory.class = org.quartz.plugins.history.LoggingJobHistoryPlugin
org.quartz.plugin.jobInitializer.class = org.quartz.plugins.xml.JobInitializationPlugin
org.quartz.plugin.jobInitializer.fileNames = quartz-jobs.xml
org.quartz.plugin.jobInitializer.overWriteExistingJobs = true
org.quartz.plugin.jobInitializer.failOnFileNotFound = true
org.quartz.plugin.jobInitializer.scanInterval = 10
org.quartz.plugin.jobInitializer.wrapInUserTransaction = false
Job-ovi se definisu u quartz-jobs.xml-u
<?xml version='1.0' encoding='utf-8'?>
<quartz xmlns="http://www.opensymphony.com/quartz/JobSchedulingData"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opensymphony.com/quartz/JobSchedulingData http://www.quartz-scheduler.org/xml/job_scheduling_data_1_5.xsd"
version="1.5">
<job>
<job-detail>
<name>HelloWorld</name>
<group>helloWorld</group>
<description>HelloWorld example</description>
<job-class>com.test.quartz.HelloJob</job-class>
<volatility>false</volatility>
<durability>false</durability>
<recover>false</recover>
</job-detail>
<trigger>
<cron>
<name>HelloWorld</name>
<group>helloWorld</group>
<description>HelloWorld example</description>
<job-name>HelloWorld</job-name>
<job-group>helloWorld</job-group>
<cron-expression>0/5 * * ? * *</cron-expression>
<!-- every 5 seconds... -->
</cron>
</trigger>
</job>
</quartz>
Detaljnije informacije kako se konfigurise cron-expression mozete naci na:
http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html (http://www.quartz-scheduler.org/docs/tutorials/crontrigger.html)
Svaki Job, definisan u quartz-jobs.xml-u mora da implementira interface Job, primer HelloWorlda:
package com.test.quartz;
import java.util.Date;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
public class HelloJob implements Job
{
private static Log log = LogFactory.getLog(HelloJob.class);
public HelloJob()
{
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
log.info("Hello World! - " + new Date());
System.out.println("Hello World! - " + new Date());
}
}