http://terracotta.org/services到官方下载包‘
java执行类
public class TestT{
public void myTest(){
System.out.println("fcc...."+DateUtils.getDataStrByPattern("yyyy-MM-dd HH:mm:ss"));
}
}
spring配置---spring-quartz lib 1.X
<bean id="testT" class="test.TestT"></bean>
<bean id="jobDetailt" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject">
<ref bean="testT"/>
</property>
<property name="targetMethod">
<value>myTest</value>
</property>
</bean>
<bean id="jobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="jobDetailt"/>
</property>
<property name="cronExpression">
<value>0 0/1 * * * ?</value>
</property>
</bean>
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="jobTrigger"/>
</list>
</property>
</bean>
如果报如下错误,解决方案,加入包jta.jar
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.scheduling.quartz.SchedulerFactoryBean#0' defined in ServletContext resource [/WEB-INF/context/applicationContext-service.xml]: Invocation of init method failed; nested exception is java.lang.NoClassDefFoundError: javax/transaction/UserTransaction
api doc
Interface CronTrigger
"0 0 12 * * ?" | Fire at 12pm (noon) every day | |
"0 15 10 ? * *" | Fire at 10:15am every day | |
"0 15 10 * * ?" | Fire at 10:15am every day | |
"0 15 10 * * ? *" | Fire at 10:15am every day | |
"0 15 10 * * ? 2005" | Fire at 10:15am every day during the year 2005 | |
"0 * 14 * * ?" | Fire every minute starting at 2pm and ending at 2:59pm, every day | |
"0 0/5 14 * * ?" | Fire every 5 minutes starting at 2pm and ending at 2:55pm, every day | |
"0 0/5 14,18 * * ?" | Fire every 5 minutes starting at 2pm and ending at 2:55pm, AND fire every 5 minutes starting at 6pm and ending at 6:55pm, every day | |
"0 0-5 14 * * ?" | Fire every minute starting at 2pm and ending at 2:05pm, every day | |
"0 10,44 14 ? 3 WED" | Fire at 2:10pm and at 2:44pm every Wednesday in the month of March. | |
"0 15 10 ? * MON-FRI" | Fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday | |
"0 15 10 15 * ?" | Fire at 10:15am on the 15th day of every month | |
"0 15 10 L * ?" | Fire at 10:15am on the last day of every month | |
"0 15 10 ? * 6L" | Fire at 10:15am on the last Friday of every month | |
"0 15 10 ? * 6L" | Fire at 10:15am on the last Friday of every month | |
"0 15 10 ? * 6L 2002-2005" | Fire at 10:15am on every last Friday of every month during the years 2002, 2003, 2004 and 2005 | |
"0 15 10 ? * 6#3" | Fire at 10:15am on the third Friday of every month |
----spring-quartz2.X 动态设置时间
package spring;
import static org.quartz.CronScheduleBuilder.cronSchedule;
import static org.quartz.JobBuilder.newJob;
import static org.quartz.TriggerBuilder.newTrigger;
import java.util.Date;
import org.quartz.CronTrigger;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerFactory;
import org.quartz.SchedulerMetaData;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This Example will demonstrate all of the basics of scheduling capabilities of
* Quartz using Cron Triggers.
*
* @author Bill Kratzer
*/
public class CronTriggerExample {
public void run() throws Exception {
Logger log = LoggerFactory.getLogger(CronTriggerExample.class);
log.info("------- Initializing -------------------");
// First we must get a reference to a scheduler
SchedulerFactory sf = new StdSchedulerFactory();
Scheduler sched = sf.getScheduler();
// jobs can be scheduled before sched.start() has been called
// job 1 will run every 20 seconds
JobDetail job = newJob(SimpleJob.class)
.withIdentity("job1", "group1")
.build();
CronTrigger trigger = newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0/20 * * * * ?"))
.build();
Date ft = sched.scheduleJob(job, trigger);
log.info(job.getKey() + " has been scheduled to run at: " + ft
+ " and repeat based on expression: "
+ trigger.getCronExpression());
log.info("------- Starting Scheduler ----------------");
// All of the jobs have been added to the scheduler, but none of the
// jobs
// will run until the scheduler has been started
sched.start();
log.info("------- Started Scheduler -----------------");
log.info("------- Waiting one minutes... ------------");
try {
// wait five minutes to show jobs
Thread.sleep(60L * 1000L);
// executing...
} catch (Exception e) {
}
//modify new trigger run every 10 seconds
sched.rescheduleJob(trigger.getKey(), newTrigger()
.withIdentity("trigger1", "group1")
.withSchedule(cronSchedule("0/10 * * * * ?"))
.build() );
//sched.deleteJob(trigger.getJobKey());
try {
// wait one minutes to show jobs
Thread.sleep(60L * 1000L);
// executing...
} catch (Exception e) {
}
sched.shutdown(true);
log.info("------- Shutdown Complete -----------------");
SchedulerMetaData metaData = sched.getMetaData();
log.info("Executed " + metaData.getNumberOfJobsExecuted() + " jobs.");
log.info("------- over -----------------");
}
public static void main(String[] args) throws Exception {
CronTriggerExample example = new CronTriggerExample();
example.run();
}
}
package spring;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobKey;
/**
* <p>
* This is just a simple job that gets fired off many times by example 1
* </p>
*
* @author Bill Kratzer
*/
public class SimpleJob implements Job {
private static Logger _log = LoggerFactory.getLogger(SimpleJob.class);
/**
* Quartz requires a public empty constructor so that the
* scheduler can instantiate the class whenever it needs.
*/
public SimpleJob() {
}
/**
* <p>
* Called by the <code>{@link org.quartz.Scheduler}</code> when a
* <code>{@link org.quartz.Trigger}</code> fires that is associated with
* the <code>Job</code>.
* </p>
*
* @throws JobExecutionException
* if there is an exception while executing the job.
*/
public void execute(JobExecutionContext context)
throws JobExecutionException {
// This job simply prints out its job name and the
// date and time that it is running
JobKey jobKey = context.getJobDetail().getKey();
_log.info("SimpleJob says: " + jobKey + " executing at " + new Date());
}
}