基于maven发送邮件系列(3)---maven自带的quartz实现定时发送邮件

来源:互联网 发布:java web文件管理系统 编辑:程序博客网 时间:2024/06/03 21:07

参考别人的代码,实现自己的功能,只是为了方便查找

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 {@Overridepublic 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 blocke.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);}}


原创粉丝点击