step:
1. Guide package (bos-parent --> pom.xml)
<!-- Introduce quartz dependency -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz-jobs</artifactId>
<version>2.2.3</version>
</dependency>
<!-- Introduce java-mail dependency-->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
2. Create a job class for sending emails
package cn.itcast.bos.quartz.jobs;
import java.util.List;
import java.util.Properties;
import javax.annotation.Resource;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.xushuai.bos.dao.WorkbillDao;
import com.xushuai.bos.entity.Workbill;
/**
* Jobs to send emails
* @author zhaoqx
*
*/
public class MailJob {
@Resource
private WorkbillDao workbillDao;
private String username;
private String password;
private String smtpServer;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public void execute() {
System.out.println("I'm going to send an email...");
try {
//Query all work orders whose work order type is new
List<Workbill> list = workbillDao.findNewWorkbills();
if(null != list && list.size() > 0){
final Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", this.getSmtpServer());
mailProps.put("mail.smtp.auth", "true");
mailProps.put("mail.username", this.getUsername());
mailProps.put("mail.password", this.getPassword());
// Build authorization information for SMTP authentication
Authenticator authenticator = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
// Username Password
String userName = mailProps.getProperty("mail.username");
String password = mailProps.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
// Create a mail session using environment properties and authorization information
Session mailSession = Session.getInstance(mailProps, authenticator);
for(Workbill workbill : list){
// create email message
MimeMessage message = new MimeMessage(mailSession);
// set sender
InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));
message.setFrom(from);
// set recipient
InternetAddress to = new InternetAddress("[email protected]");//You need to fill in the specific recipient email
message.setRecipient(RecipientType.TO, to);
// set the email header
message.setSubject("System Mail: New Order Notification");
// Set the body of the email
message.setContent(workbill.toString(), "text/html;charset=UTF-8");
// send email
Transport.send(message);
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
}
3. Configure the spring configuration file (new configuration):
<!-- Configure custom work class -->
<bean id="myJob" class="cn.itcast.bos.quartz.jobs.MailJob">
<property name="username" value="xxxxxxxxxxxx"/>
<property name="password" value="xxxxxxxxxxxxxx"/>
<property name="smtpServer" value="smtp.163.com"/>
</bean>
<!-- Configuration job details class: org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- Inject custom work class -->
<property name="targetObject" ref="myJob"/>
<!-- The method of injecting the custom work class that needs to be performed -->
<property name="targetMethod" value="execute"/>
</bean>
<!-- Configure triggers: org.springframework.scheduling.quartz.CronTriggerFactoryBean -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<!-- inject job details object -->
<property name="jobDetail" ref="jobDetail"/>
<!-- Inject Cron expression: cronExpression -->
<property name="cronExpression" value="0 0/1 * * * ? *" />
</bean>
<!-- Configure scheduling factory: org.springframework.scheduling.quartz.SchedulerFactoryBean -->
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<!-- inject trigger -->
<property name="triggers" >
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
WorkbillDao new method:
/**
* Query work orders whose work order status is 'new order'
* @return
*/
List<Workbill> findNewWorkbills();
Dao layer implementation:
@Override
public List<Workbill> findNewWorkbills() {
String hql = "FROM Workbill WHERE type = '"+Workbill.TYPE_NEW+"'";
return (List<Workbill>) this.getHibernateTemplate().find(hql);
}
Note: Sending emails is not required by the system, just to practice quartz. In addition, remove the associated attribute in the toString() method of Workbill, otherwise there will be a no session exception. Problems caused by lazy loading.