Spring功能越来越多了,用起来还很舒服方便,Quartz实现的定时任务就是一个。
首先是配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- schedulerFactory -->
<bean id="schedulerFactory" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<!-- 有多少定时任务在这里写几个 -->
<ref local="cronTriggerClaim" />
</list>
</property>
</bean>
<!-- 1.AutoClaimReminderMailService 第一个定时任务-->
<bean id="cronTriggerClaim" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="jobDetailClaim" />
<property name="cronExpression">
<value>0 10 13 ? * MON</value> <!-- 这里设定时间,周一的13点10分00秒 开始-->
</property>
</bean>
<bean id="jobDetailClaim"
class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<property name="targetObject" ref="claimScheduler" /> <!-- 这里找下面的bean-->
<property name="targetMethod" value="sendRemiderMail" /> <!-- 这里设定定时任务的具体方法-->
</bean>
<!-- 这个类就是自己写的了,注意要实现上面提到的sendRemiderMail方法,其它可自便-->
<bean id="claimScheduler" class="com.ibm.XXX.service.auto.AutoClaimReminderMailService">
<property name="smtpServer"><value>smtp.163.com</value></property>
<property name="smtpUsername"><value>unknown@163.com</value></property>
<property name="smtpPassword"><value>puzzle</value></property>
<property name="fromMailAddress"><value>unknown@163.com</value></property>
<property name="toMailAddress"><value>unknown1@163.comm,unknown2@163.com</value></property>
<property name="ccMailAddress"><value>unknow3@163.com,unknown4@163.com</value></property>
<property name="bccMailAddress"><value>unknown5@163.com,unknown6@163.com</value></property>
</bean>
</beans>
下面是com.ibm.XXX.service.auto.AutoClaimReminderMailService类:
public class AutoClaimReminderMailService{
private static Logger logger = Logger.getLogger(AutoClaimReminderMailService.class);
public void sendRemiderMail() {
String title="[Need Your Action!]Claim Reminder";
StringBuilder sb=new StringBuilder();
sb.append("<p>Hi Guys:</p>");
sb.append("<p><B>Here is the claim reminder bell kindly for your action, pls submit your labor claim in ILC/Cats within today for this week, thanks your cooperation!</B></p>");
sendMail(title,sb.toString());
}
protected String smtpServer;
protected String smtpUsername;
protected String smtpPassword;
protected String fromMailAddress;
protected String toMailAddress;
protected String ccMailAddress;
protected String bccMailAddress;
/**
* 无参构造函数
*/
public AutoClaimReminderMailService(){
}
/**
* 发送邮件的关键函数
*
* @param title
* @param content
* @return
* @throws Exception
*/
protected boolean sendMail(String title,String content) throws Exception{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpServer);
// 获得邮件会话对象
Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(smtpUsername, smtpPassword));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人
mimeMessage.setRecipients(Message.RecipientType.TO, getInternetAddressArr(toMailAddress));// To收件人
mimeMessage.setRecipients(Message.RecipientType.CC, getInternetAddressArr(ccMailAddress));// Cc收件人
mimeMessage.setRecipients(Message.RecipientType.BCC, getInternetAddressArr(bccMailAddress));// Bcc收件人
mimeMessage.setSubject(title);
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=utf8"));// 网页格式
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);// 设置邮件内容对象
Transport.send(mimeMessage);// 发送邮件
return true;
}
protected InternetAddress[] getInternetAddressArr(String mialAddr) throws Exception{
String[] arr=mialAddr.split(",");
InternetAddress[] retval=new InternetAddress[arr.length];
for(int i=0;i<arr.length;i++){
retval[i]=new InternetAddress(arr[i]);
}
return retval;
}
}
就到这里,再见吧。
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/xiandedanteng/p/3329562.html,如需转载请自行联系原作者