Spring integrates Quartz to send regular emails

Source: Internet
Author: User

Function Description: I started to contact Quartz and tried to use Quartz to integrate spring to send an email 10 times in a row every minute.
Core jar:
Mail: commons-email-1.2.jar mail. jar (required)
Quartz: quartz-all-1.8.3.jar quartz-all-1.8.3/lib/all jar
Spring: spring-context-support.ajr (required)
Paste only the core code:
Email sending: Use apache commons-email and mail
Package com. ssh. commonsemail;

Import java. util. ArrayList;
Import java. util. Date;
Import java. util. List;

Import javax. mail. internet. InternetAddress;

Import org. apache. commons. mail. SimpleEmail;

/***//**
* Function Description: This code mainly implements the mail sending function.
* @ Author coder
*
*/
Public class SendSimplEmail {


Public static void sendMail () throws Exception {
List <InternetAddress> list = new ArrayList <InternetAddress> ();
List. add (new InternetAddress ("313698683@qq.com "));
List. add (new InternetAddress ("184675420@qq.com "));
SimpleEmail email = new SimpleEmail ();
Email. setFrom ("184675420@163.com ");
Email. setCharset ("UTF-8 ");
Email. setSentDate (new Date ());
Email. setSubject ("test Quartz ");
Email. setHostName ("smtp.163.com ");
Email. setAuthentication ("xxxx", "xxxx ");
Email. setTo (list );
Email. setContent ("Email. send ();
}


}
Define scheduling job: Inherit from org. springframework. scheduling. quartz. QuartzJobBean times class in spring-context-support.jar
Package com. ssh. quantz;

Import org. quartz. JobExecutionContext;
Import org. quartz. JobExecutionException;
Import org. springframework. scheduling. quartz. QuartzJobBean;

Import com. ssh. commonsemail. SendSimplEmail;

/***//**
* Send an email task
* @ Author coder
*
*/
Public class SendEmailJob extends QuartzJobBean {

@ Override
Protected void executeInternal (JobExecutionContext context)
Throws JobExecutionException {
Try {
// Call the email sending code
SendSimplEmail. sendMail ();
} Catch (Exception e ){
E. printStackTrace ();
}
}

}
Spring core configuration file
<! -- Define a scheduling task -->
<Bean id = "quantzjobBean" class = "org. springframework. scheduling. quartz. JobDetailBean">
<Property name = "jobClass">
<! -- Implements JobBean of org. springframework. scheduling. quartz. QuartzJobBean -->
<Value> com. ssh. quantz. SendEmailJob </value>
</Property>
<! -- Call business logic -->
<! --
<Property name = "jobDataAsMap">
<Map>
<Entry key = "biz">
<Ref bean = "users"/>
</Entry>
</Map>
</Property>
-->
</Bean>

<! -- Trigger task condition -->
<Bean id = "simpletriggerbean" class = "org. springframework. scheduling. quartz. SimpleTriggerBean">
<Property name = "jobDetail" ref = "quantzjobBean"> </property>
<! -- One-minute delay -->
<Property name = "startDelay">
<Value> 60000 </value>
</Property>
<! -- Call every 2 minutes -->
<Property name = "repeatInterval">
<Value> 60000 </value>
</Property>
<! -- Execute 10 times -->
<Property name = "repeatCount">
<Value> 10 </value>
</Property>

</Bean>

<! -- Start scheduling -->
<Bean id = "startQuartz" class = "org. springframework. scheduling. quartz. SchedulerFactoryBean">
<Property name = "triggers">
<List>
<Ref bean = "simpletriggerbean"/>
</List>
</Property>
</Bean>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.