参考别人的代码,实现自己的功能,只是为了方便查找
1还是基于(1)的基础上实现的
package com.test.mavenMethod;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.acount.email.AcountEmailException;
import com.test.acount.email.AcountEmailService;
public class MailJob implements Job {
@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException {
ClassPathXmlApplicationContext cxa = new ClassPathXmlApplicationContext(
"acount-email.xml");
AcountEmailService acountEmailService = (AcountEmailService) cxa
.getBean("acountEmailService");
String html = "test hahha ";
try {
acountEmailService.sendEmail("XXX@XXX", "hello world",
html);
} catch (AcountEmailException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2每隔58秒发一次,学一下表达式还是有必要的
package com.test.mavenMethod;
import java.util.Random;
import org.quartz.CronScheduleBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
public class CronTriggerExample {
public static void main(String[] args) throws Exception {
JobDetail job = JobBuilder.newJob(MailJob.class)
.withIdentity("dummyJobName", "group1").build();
Random rand = new Random(System.currentTimeMillis());
//int secDelta = rand.nextInt(3);
int secDelta = rand.nextInt(58);
Trigger trigger = TriggerBuilder
.newTrigger()
.withIdentity("dummyTriggerName", "group1")
.withSchedule(
CronScheduleBuilder.cronSchedule("0/" + secDelta + " * * * * ?")).build();
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
//CronScheduleBuilder.cronSchedule(secDelta + " 0 9 ? * 6")).build();
scheduler.start();
scheduler.scheduleJob(job, trigger);
}
}