1 quartz介绍和下载
官网:http://www.quartz-scheduler.org/
Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用。Quartz可以用来创建简单或为运行十个,百个,甚至是好几万个Jobs这样复杂的程序。Jobs可以做成标准的Java组件或 EJBs。Quartz的最新版本为Quartz 2.2.3
下载压缩包解压:
2 Quartz使用
第一步:创建maven工程,导入spring和quartz相关依赖
第二步:创建作业类
public class MailJob {
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() {
//TODO 作业方法
}
public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
}
第三步:在spring配置文件中配置作业类
<!-- 注册自定义作业类 -->
<bean id="myJob" class="com.itheima.jobs.MailJob">
<property name="username" value="[email protected]"/>
<property name="password" value="987654321wyx"/>
<property name="smtpServer" value="smtp.163.com"/>
</bean>
第四步:在spring配置文件中配置JobDetail
<!-- quartz任务详情 -->
<bean id="jobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
<!-- 注入目标对象 -->
<property name="targetObject" ref="myJob"/>
<!-- 注入目标方法 -->
<property name="targetMethod" value="execute"/>
</bean>
第五步:在spring配置文件中配置触发器
<!-- 配置触发器 -->
<bean id="myTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="jobDetail"/>
<property name="cronExpression" value="0/5 * 12 * * ?"/>
</bean>
第六步:在spring配置文件中配置scheduler
<bean id="schedulerFactoryBean" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="myTrigger"/>
</list>
</property>
</bean>
/** * 发送邮件的作业 * @author zhaoqx * */public class MailJob {@Resourceprivate IWorkbillDao 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 test(){System.out.println("run方法执行了");}public void execute() {System.out.println("要发邮件了。。。");try {//查询工单类型为新单的所有工单List<Workbill> list = workbillDao.findAll();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());
// 构建授权信息,用于进行SMTP进行身份验证Authenticator authenticator = new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {// 用户名、密码String userName = mailProps.getProperty("mail.username");String password = mailProps.getProperty("mail.password");return new PasswordAuthentication(userName, password);}};// 使用环境属性和授权信息,创建邮件会话Session mailSession = Session.getInstance(mailProps, authenticator);for(Workbill workbill : list){// 创建邮件消息MimeMessage message = new MimeMessage(mailSession);// 设置发件人InternetAddress from = new InternetAddress(mailProps.getProperty("mail.username"));message.setFrom(from);// 设置收件人InternetAddress to = new InternetAddress("[email protected]");message.setRecipient(RecipientType.TO, to);// 设置邮件标题message.setSubject("系统邮件:新单通知");// 设置邮件的内容体message.setContent(workbill.toString(), "text/html;charset=UTF-8");// 发送邮件Transport.send(message);}}} catch (Exception ex) {ex.printStackTrace();}}
public String getSmtpServer() {return smtpServer;}
public void setSmtpServer(String smtpServer) {this.smtpServer = smtpServer;}}