功能描述:刚开始接触Quartz试着用Quartz整合spring实现每隔一分钟发送一封邮件连续发送次
核心jar:
邮件发送commonsemailjar mailjar(必须的)
quartzquartzalljar quartzall/lib/下所有jar
springspringcontextsupportajr(必须的)
只贴出核心代码:
Email发送使用apache commonsemail跟mail
package monsemail;
import javautilArrayList;
import javautilDate;
import javautilList;
import javaxmailinternetInternetAddress;
import monsmailSimpleEmail;
/** *//**
* 功能描述:此代码主要实现邮件发送功能
* @author coder
*
*/
public class SendSimplEmail {
public static void sendMail()throws Exception{
List<InternetAddress> list=new ArrayList<InternetAddress>();
listadd(new InternetAddress());
listadd(new InternetAddress());
SimpleEmail email=new SimpleEmail();
emailsetFrom();
emailsetCharset(utf);
emailsetSentDate(new Date());
emailsetSubject(测试Quartz);
emailsetHostName();
emailsetAuthentication(xxxx xxxx);
emailsetTo(list);
emailsetContent(<h>Hello把凤姐许配给你你看咋样?</h> text/html;charset=utf);
emailsend();
}
}
定义调度工作任务继承自orgspringframeworkschedulingquartzQuartzJobBean次类在springcontextsupportjar中
package comsshquantz;
import orgquartzJobExecutionContext;
import orgquartzJobExecutionException;
import orgspringframeworkschedulingquartzQuartzJobBean;
import monsemailSendSimplEmail;
/** *//**
* 发送email任务
* @author coder
*
*/
public class SendEmailJob extends QuartzJobBean{
@Override
protected void executeInternal(JobExecutionContext context)
throws JobExecutionException {
try {
//调用邮件发送代码
SendSimplEmailsendMail();
} catch (Exception e) {
eprintStackTrace();
}
}
}
spring核心配置文件
<! 定义调度工作任务 >
<bean id=quantzjobBean class=orgspringframeworkschedulingquartzJobDetailBean>
<property name=jobClass>
<! 实现了orgspringframeworkschedulingquartzQuartzJobBean的JobBean >
<value>comsshquantzSendEmailJob</value>
</property>
<! 调用业务逻辑 >
<!
<property name=jobDataAsMap>
<map>
<entry key=biz>
<ref bean=users/>
</entry>
</map>
</property>
>
</bean>
<! 触发任务条件 >
<bean id=simpletriggerbean class=orgspringframeworkschedulingquartzSimpleTriggerBean>
<property name=jobDetail ref=quantzjobBean></property>
<! 延迟一分钟启动 >
<property name=startDelay>
<value></value>
</property>
<! 每隔分钟调用一次 >
<property name=repeatInterval>
<value></value>
</property>
<! 执行次 >
<property name=repeatCount>
<value></value>
</property>
</bean>
<! 启动调度 >
<bean id=startQuartz class=orgspringframeworkschedulingquartzSchedulerFactoryBean>
<property name=triggers>
<list>
<ref bean=simpletriggerbean/>
</list>
</property>
</bean>