javascript
使用Spring提供Quartz来实现定时任务
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;
?? ?}
}
?
就到這里,再見吧。
轉載于:https://www.cnblogs.com/xiandedanteng/p/3329562.html
總結
以上是生活随笔為你收集整理的使用Spring提供Quartz来实现定时任务的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android工程建立到最后一步提示un
- 下一篇: gradle idea java ssm